From 5f2abdf275c3765290ee7d6728842cba6d3af184 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 7 May 2021 15:27:34 +0300 Subject: Implemented editing user --- ExamTemplate/Web/Configurations/UserMappings.cs | 2 ++ ExamTemplate/Web/Controllers/UserController.cs | 38 +++++++++++++++++++++-- ExamTemplate/Web/Models/User/EditUserViewModel.cs | 9 ++++++ ExamTemplate/Web/Views/User/EditProfile.cshtml | 19 ++++++++++++ ExamTemplate/Web/Views/User/Profile.cshtml | 14 +++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 ExamTemplate/Web/Models/User/EditUserViewModel.cs create mode 100644 ExamTemplate/Web/Views/User/EditProfile.cshtml (limited to 'ExamTemplate/Web') diff --git a/ExamTemplate/Web/Configurations/UserMappings.cs b/ExamTemplate/Web/Configurations/UserMappings.cs index 6a98477..1bba602 100644 --- a/ExamTemplate/Web/Configurations/UserMappings.cs +++ b/ExamTemplate/Web/Configurations/UserMappings.cs @@ -11,6 +11,8 @@ namespace ExamTemplate.Services.Configurations CreateMap(); CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/ExamTemplate/Web/Controllers/UserController.cs b/ExamTemplate/Web/Controllers/UserController.cs index 4a9cf31..2e5978b 100644 --- a/ExamTemplate/Web/Controllers/UserController.cs +++ b/ExamTemplate/Web/Controllers/UserController.cs @@ -81,13 +81,47 @@ namespace ExamTemplate.Web.Controllers UserServiceModel userServiceModel = await this._userService.GetUserByUsernameAsync(username); if (userServiceModel == default(UserServiceModel)) - { return RedirectToAction("Login"); - } UserViewModel userViewModel = this._autoMapper.Map(userServiceModel); return View(userViewModel); } + + [HttpGet] + [Route("/EditProfile")] + public async Task EditProfile() + { + UserServiceModel userServiceModel = await this._userService.GetUserByClaimsAsync(this.HttpContext.User); + + if (userServiceModel == default(UserServiceModel)) + return RedirectToAction("Login"); + + EditUserViewModel editUserViewModel = this._autoMapper.Map(userServiceModel); + + return View(editUserViewModel); + } + + [HttpPost] + [Route("/EditProfile")] + public async Task EditProfile(EditUserViewModel editUserViewModel) + { + if (!this._userService.IsSignedIn(HttpContext.User)) + return RedirectToAction("Login"); + + EditUserServiceModel editUserServiceModel = this._autoMapper.Map(editUserViewModel); + bool result = await this._userService.EditUserAsync(HttpContext.User, editUserServiceModel); + + if (result) + { + await this._userService.LogoutAsync(); + return RedirectToAction("Profile", new { username = editUserViewModel.Username }); + } + else + { + UserServiceModel userServiceModel = await this._userService.GetUserByClaimsAsync(HttpContext.User); + return RedirectToAction("Profile", new { username = userServiceModel.Username }); + } + } } } diff --git a/ExamTemplate/Web/Models/User/EditUserViewModel.cs b/ExamTemplate/Web/Models/User/EditUserViewModel.cs new file mode 100644 index 0000000..baae751 --- /dev/null +++ b/ExamTemplate/Web/Models/User/EditUserViewModel.cs @@ -0,0 +1,9 @@ +namespace ExamTemplate.Web.Models.User +{ + public class EditUserViewModel + { + public string FirstName { get; set; } + public string LastName { get; set; } + public string Username { get; set; } + } +} diff --git a/ExamTemplate/Web/Views/User/EditProfile.cshtml b/ExamTemplate/Web/Views/User/EditProfile.cshtml new file mode 100644 index 0000000..fef77f1 --- /dev/null +++ b/ExamTemplate/Web/Views/User/EditProfile.cshtml @@ -0,0 +1,19 @@ +@model EditUserViewModel +@{ + ViewData["Title"] = "Edit Profile"; +} + +

+

+ + + + + + + + + + +
+

diff --git a/ExamTemplate/Web/Views/User/Profile.cshtml b/ExamTemplate/Web/Views/User/Profile.cshtml index 279de82..7883f2b 100644 --- a/ExamTemplate/Web/Views/User/Profile.cshtml +++ b/ExamTemplate/Web/Views/User/Profile.cshtml @@ -1,3 +1,8 @@ +@using Microsoft.AspNetCore.Identity + +@inject SignInManager SignInManager +@inject UserManager UserManager + @model UserViewModel @{ ViewData["Title"] = Model.Username + "'s Profile"; @@ -10,4 +15,13 @@
@Model.Username
+ @if (SignInManager.IsSignedIn(User)) + { + @if(UserManager.GetUserName(User) == Model.Username) + { +
+ +
+ } + }

-- cgit v1.2.3