using ExamTemplate.Services; using Microsoft.AspNetCore.Mvc; using ExamTemplate.Web.Models.User; using AutoMapper; using ExamTemplate.Services.Models; using System.Threading.Tasks; namespace ExamTemplate.Web.Controllers { public class UserController : Controller { private readonly IMapper _autoMapper; private readonly UserService _userService; public UserController(IMapper autoMapper, UserService userService) { this._autoMapper = autoMapper; this._userService = userService; } [HttpGet] [Route("/Register")] public IActionResult Register() { return View(); } [HttpPost] [Route("/Register")] public async Task Register(RegisterUserViewModel registerUserViewModel) { if (!ModelState.IsValid) return View(registerUserViewModel); RegisterUserServiceModel registerUserServiceModel = this._autoMapper.Map(registerUserViewModel); bool result = await this._userService.RegisterUserAsync(registerUserServiceModel); if (result) return RedirectToAction("Index", "Home"); else return View(); } [HttpGet] [Route("/Login")] public IActionResult Login() { return View(); } [HttpPost] [Route("/Login")] public async Task Login(LoginUserViewModel loginUserViewModel) { if (!ModelState.IsValid) return View(loginUserViewModel); LoginUserServiceModel loginUserServiceModel = this._autoMapper.Map(loginUserViewModel); bool result = await this._userService.LoginUserAsync(loginUserServiceModel); if (result) return RedirectToAction("Index", "Home"); else return View(); } [HttpPost] public async Task Logout() { await this._userService.LogoutAsync(); return RedirectToAction("Login"); } [HttpGet] [Route("/Profile/{username}")] public async Task Profile(string username) { 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 }); } } } }