diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-05-16 05:17:37 +0000 |
|---|---|---|
| committer | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-05-16 05:17:37 +0000 |
| commit | ffd59fcdc11b8b57cab30be090d2775d2b6b1ba3 (patch) | |
| tree | 3e82cb2864aec05505509e1afc01c6843f0a64a9 /ExamTemplate/Web/ExamTemplate.Web/Controllers | |
| parent | 2ac139d0854c0f6d1b4cebc1487dd41128f00c60 (diff) | |
| parent | 564209043493c77fd51ac096b5a160bb20c46f12 (diff) | |
| download | it-kariera-exam-template-ffd59fcdc11b8b57cab30be090d2775d2b6b1ba3.tar it-kariera-exam-template-ffd59fcdc11b8b57cab30be090d2775d2b6b1ba3.tar.gz it-kariera-exam-template-ffd59fcdc11b8b57cab30be090d2775d2b6b1ba3.zip | |
Merge branch 'fuckoff' into 'master'
New structure
See merge request Syndamia/it-kariera-exam-template!2
Diffstat (limited to 'ExamTemplate/Web/ExamTemplate.Web/Controllers')
| -rw-r--r-- | ExamTemplate/Web/ExamTemplate.Web/Controllers/AccountController.cs | 146 | ||||
| -rw-r--r-- | ExamTemplate/Web/ExamTemplate.Web/Controllers/HomeController.cs | 33 |
2 files changed, 179 insertions, 0 deletions
diff --git a/ExamTemplate/Web/ExamTemplate.Web/Controllers/AccountController.cs b/ExamTemplate/Web/ExamTemplate.Web/Controllers/AccountController.cs new file mode 100644 index 0000000..2c2eb32 --- /dev/null +++ b/ExamTemplate/Web/ExamTemplate.Web/Controllers/AccountController.cs @@ -0,0 +1,146 @@ +using ExamTemplate.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; +using ExamTemplate.Web.Models.User; +using AutoMapper; +using ExamTemplate.Services.Models.User; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; + +namespace ExamTemplate.Web.Controllers +{ + [Authorize] + public class AccountController : Controller + { + private readonly IMapper _autoMapper; + private readonly IUserService _userService; + + public AccountController(IMapper autoMapper, IUserService userService) + { + this._autoMapper = autoMapper; + this._userService = userService; + } + + [HttpGet] + [AllowAnonymous] + public IActionResult Register() + { + return View(); + } + + [HttpPost] + [AllowAnonymous] + public async Task<IActionResult> Register(RegisterUserWebModel registerUserWebModel) + { + if (!ModelState.IsValid) + return View(registerUserWebModel); + + RegisterUserServiceModel registerUserServiceModel = this._autoMapper.Map<RegisterUserServiceModel>(registerUserWebModel); + + bool result = await this._userService.RegisterUserAsync(registerUserServiceModel); + + if (result) + return await this.Login(new LoginUserWebModel { + Username = registerUserServiceModel.Username, + Password = registerUserServiceModel.Password + }); + else + return View(registerUserWebModel); + } + + [HttpGet] + [AllowAnonymous] + public IActionResult Login() + { + return View(); + } + + [HttpPost] + [AllowAnonymous] + public async Task<IActionResult> Login(LoginUserWebModel loginUserWebModel) + { + if (!ModelState.IsValid) + return View(loginUserWebModel); + + LoginUserServiceModel loginUserServiceModel = this._autoMapper.Map<LoginUserServiceModel>(loginUserWebModel); + + bool result = await this._userService.LoginUserAsync(loginUserServiceModel); + + if (result) + return RedirectToAction("Index", "Home"); + else + return View(loginUserWebModel); + } + + [HttpPost] + public async Task<IActionResult> Logout() + { + await this._userService.LogoutAsync(); + + return RedirectToAction("Login"); + } + + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> Profile(string username) + { + UserServiceModel userServiceModel = await this._userService.GetUserByUsernameAsync(username); + + if (userServiceModel == default(UserServiceModel)) + return RedirectToAction("ErrorNotFound", "Home"); + + UserWebModel userWebModel = this._autoMapper.Map<UserWebModel>(userServiceModel); + + return View(userWebModel); + } + + [HttpGet] + public async Task<IActionResult> Edit() + { + UserServiceModel userServiceModel = await this._userService.GetUserByClaimsAsync(this.HttpContext.User); + + if (userServiceModel == default(UserServiceModel)) + return RedirectToAction("ErrorNotFound", "Home"); + + EditUserWebModel editUserWebModel = this._autoMapper.Map<EditUserWebModel>(userServiceModel); + + return View(editUserWebModel); + } + + [HttpPost] + public async Task<IActionResult> Edit(EditUserWebModel editUserWebModel) + { + if (!ModelState.IsValid) + return View(editUserWebModel); + + if (!this._userService.IsSignedIn(HttpContext.User)) + return RedirectToAction("Login"); + + UserServiceModel loggedInUser = await this._userService.GetUserByClaimsAsync(HttpContext.User); + + UserServiceModel userServiceModel = this._autoMapper.Map<UserServiceModel>(editUserWebModel); + bool result = await this._userService.EditUserAsync(HttpContext.User, userServiceModel); + + if (result) + { + if (loggedInUser.Username != editUserWebModel.Username) + await this._userService.LogoutAsync(); + + return RedirectToAction("Profile", new { username = editUserWebModel.Username }); + } + else + return RedirectToAction("Profile", new { username = loggedInUser.Username }); + } + + [HttpPost] + public async Task<IActionResult> Delete() + { + await this._userService.LogoutAsync(); + bool result = await this._userService.DeleteUserAsync(HttpContext.User); + + if (result) + return RedirectToAction("Login"); + else + return RedirectToAction("Index", "Home"); + } + } +} diff --git a/ExamTemplate/Web/ExamTemplate.Web/Controllers/HomeController.cs b/ExamTemplate/Web/ExamTemplate.Web/Controllers/HomeController.cs new file mode 100644 index 0000000..d9cfc45 --- /dev/null +++ b/ExamTemplate/Web/ExamTemplate.Web/Controllers/HomeController.cs @@ -0,0 +1,33 @@ +using System.Diagnostics;
+using ExamTemplate.Web.Models;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+
+namespace ExamTemplate.Web.Controllers
+{
+ public class HomeController : Controller
+ {
+ private readonly ILogger<HomeController> _logger;
+
+ public HomeController(ILogger<HomeController> logger)
+ {
+ _logger = logger;
+ }
+
+ public IActionResult Index()
+ {
+ return View();
+ }
+
+ [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
+ public IActionResult Error()
+ {
+ return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
+ }
+
+ public IActionResult ErrorNotFound()
+ {
+ return View();
+ }
+ }
+}
|
