diff --git a/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs index 4e32dae..e660f2d 100644 --- a/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs +++ b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs @@ -6,6 +6,8 @@ namespace NG_2023_Kanban.BusinessLayer.Interfaces { Task GetAsync(int id); Task> GetAllAsync(); + Task DeleteAsync(int id); + Task UpdateAsync(int id, UserModel user); Task LoginAsync(UserModel user); Task RegisterAsync(UserModel user); } diff --git a/NG_2023_Kanban.BusinessLayer/Service/UserService.cs b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs index f375849..de9ac8c 100644 --- a/NG_2023_Kanban.BusinessLayer/Service/UserService.cs +++ b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs @@ -28,6 +28,17 @@ namespace NG_2023_Kanban.BusinessLayer.Services return _mapper.Map>(await _userRepository.GetAllAsync()); } + public async Task UpdateAsync(int id, UserModel user) + { + var entity = _mapper.Map(user); + await _userRepository.UpdateAsync(id, entity); + } + + public async Task DeleteAsync(int id) + { + await _userRepository.DeleteAsync(id); + } + public async Task LoginAsync(UserModel user) { var data = await _userRepository.FindAsync(x => x.Username == user.Username && x.Password == user.Password); diff --git a/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs b/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs index 281f955..ba34b9e 100644 --- a/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs +++ b/NG_2023_Kanban.DataLayer/Interfaces/IRepository.cs @@ -6,7 +6,7 @@ namespace NG_2023_Kanban.DataLayer.Interfaces Task GetAsync(int id); Task> FindAsync(Func predicate); Task CreateAsync(T entity); - Task UpdateAsync(T entity); + Task UpdateAsync(int id, T entity); Task DeleteAsync(int id); Task DeleteAsync(T entity); } diff --git a/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs index f0502ae..0bd0363 100644 --- a/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs +++ b/NG_2023_Kanban.DataLayer/Repositories/BaseRepository.cs @@ -2,6 +2,7 @@ using NG_2023_Kanban.DataLayer.Entities; using NG_2023_Kanban.DataLayer.Interfaces; using Microsoft.EntityFrameworkCore; +using System.Reflection; namespace NG_2023_Kanban.DataLayer.Repositories; @@ -30,9 +31,20 @@ public class BaseRepository : IRepository where T : BaseEntity await _context.SaveChangesAsync(); } - public async Task UpdateAsync(T entity) + public async Task UpdateAsync(int id, T entity) { - _context.Set().Update(entity); + var updated = await GetAsync(id); + foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties()) + { + string name = propertyInfo.Name; + var value = propertyInfo.GetValue(entity); + if (value != null) + { + PropertyInfo setProperty = updated.GetType().GetProperty(name); + setProperty.SetValue(updated, value); + } + } + //_context.Set().Update(entity); await _context.SaveChangesAsync(); } diff --git a/NG_2023_Kanban/Controllers/AdminController.cs b/NG_2023_Kanban/Controllers/AdminController.cs index b7d5bba..0eb7974 100644 --- a/NG_2023_Kanban/Controllers/AdminController.cs +++ b/NG_2023_Kanban/Controllers/AdminController.cs @@ -55,6 +55,101 @@ public class AdminController : Controller return Redirect("/Auth/Login"); } + public async Task EditUser(int id) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + ViewData["EditedAccount"] = _mapper.Map(await _userService.GetAsync(id)); + return View(); + } + return Redirect("/Auth/Login"); + } + + [HttpPost] + public async Task EditUser(int id, UserDto user) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + ViewData["EditedAccount"] = _mapper.Map(await _userService.GetAsync(id)); + //try + //{ + user.Id = id; + var model = _mapper.Map(user); + await _userService.UpdateAsync(id, model); + return Redirect("/Admin/Users"); + //} + //catch + //{ + // ViewData["Error"] = "This name is already taken."; + // return View(); + //} + } + return Redirect("/Auth/Login"); + } + + public async Task CreateUser() + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + return View(); + } + return Redirect("/Auth/Login"); + } + + [HttpPost] + public async Task CreateUser(UserDto user) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + try + { + var model = _mapper.Map(user); + await _userService.RegisterAsync(model); + return Redirect("/Admin/Users"); + } + catch + { + ViewData["Error"] = "This name is already taken."; + return View(); + } + } + return Redirect("/Auth/Login"); + } + + public async Task DeleteUser(int id) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + await _userService.DeleteAsync(id); + return Redirect("/Admin/Users"); + } + return Redirect("/Auth/Login"); + } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/NG_2023_Kanban/Views/Admin/Boards.cshtml b/NG_2023_Kanban/Views/Admin/Boards.cshtml index 9e69ef1..67bb0f2 100644 --- a/NG_2023_Kanban/Views/Admin/Boards.cshtml +++ b/NG_2023_Kanban/Views/Admin/Boards.cshtml @@ -12,7 +12,8 @@ @{ foreach (var board in boards) { -

@board.Name

+

@board.Name | Edit, Delete

} +

New

} diff --git a/NG_2023_Kanban/Views/Admin/CreateUser.cshtml b/NG_2023_Kanban/Views/Admin/CreateUser.cshtml new file mode 100644 index 0000000..207297d --- /dev/null +++ b/NG_2023_Kanban/Views/Admin/CreateUser.cshtml @@ -0,0 +1,33 @@ +@using NG_2023_Kanban.Enums + +@using NG_2023_Kanban.DTOs +@{ + ViewData["Title"] = "Create user"; +} + +
+

@ViewData["Title"]

+@if (ViewData["Error"] != null) +{ +

@ViewData["Error"]

+} +
+
+
+
+
+
+
+
+
+ +
+
diff --git a/NG_2023_Kanban/Views/Admin/EditUser.cshtml b/NG_2023_Kanban/Views/Admin/EditUser.cshtml new file mode 100644 index 0000000..c2a7bc3 --- /dev/null +++ b/NG_2023_Kanban/Views/Admin/EditUser.cshtml @@ -0,0 +1,44 @@ +@using NG_2023_Kanban.Enums + +@using NG_2023_Kanban.DTOs +@{ + var user = ViewData["EditedAccount"] as UserDto; + } + +@{ + ViewData["Title"] = "Edit user"; +} + +
+

@ViewData["Title"]

+@if (ViewData["Error"] != null) +{ +

@ViewData["Error"]

+} +
+
+
+
+
+
+
+
+
+ +
+
diff --git a/NG_2023_Kanban/Views/Admin/Users.cshtml b/NG_2023_Kanban/Views/Admin/Users.cshtml index 2b0a28a..a93eafe 100644 --- a/NG_2023_Kanban/Views/Admin/Users.cshtml +++ b/NG_2023_Kanban/Views/Admin/Users.cshtml @@ -12,7 +12,8 @@ @{ foreach (var user in users) { -

@user.Username

+

@user.Username | Edit, Delete

} +

New

}