Implement user creation, editing and deletion via admin panel
This commit is contained in:
parent
7716da6799
commit
27c9af7400
9 changed files with 204 additions and 5 deletions
|
@ -6,6 +6,8 @@ namespace NG_2023_Kanban.BusinessLayer.Interfaces
|
|||
{
|
||||
Task<UserModel> GetAsync(int id);
|
||||
Task<ICollection<UserModel>> GetAllAsync();
|
||||
Task DeleteAsync(int id);
|
||||
Task UpdateAsync(int id, UserModel user);
|
||||
Task<UserModel?> LoginAsync(UserModel user);
|
||||
Task<UserModel> RegisterAsync(UserModel user);
|
||||
}
|
||||
|
|
|
@ -28,6 +28,17 @@ namespace NG_2023_Kanban.BusinessLayer.Services
|
|||
return _mapper.Map<ICollection<UserModel>>(await _userRepository.GetAllAsync());
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(int id, UserModel user)
|
||||
{
|
||||
var entity = _mapper.Map<User>(user);
|
||||
await _userRepository.UpdateAsync(id, entity);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
await _userRepository.DeleteAsync(id);
|
||||
}
|
||||
|
||||
public async Task<UserModel?> LoginAsync(UserModel user)
|
||||
{
|
||||
var data = await _userRepository.FindAsync(x => x.Username == user.Username && x.Password == user.Password);
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace NG_2023_Kanban.DataLayer.Interfaces
|
|||
Task<T> GetAsync(int id);
|
||||
Task<ICollection<T>> FindAsync(Func<T, Boolean> predicate);
|
||||
Task CreateAsync(T entity);
|
||||
Task UpdateAsync(T entity);
|
||||
Task UpdateAsync(int id, T entity);
|
||||
Task DeleteAsync(int id);
|
||||
Task DeleteAsync(T entity);
|
||||
}
|
||||
|
|
|
@ -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<T> : IRepository<T> where T : BaseEntity
|
|||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(T entity)
|
||||
public async Task UpdateAsync(int id, T entity)
|
||||
{
|
||||
_context.Set<T>().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<T>().Update(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,101 @@ public class AdminController : Controller
|
|||
return Redirect("/Auth/Login");
|
||||
}
|
||||
|
||||
public async Task<IActionResult> EditUser(int id)
|
||||
{
|
||||
var currentAccount = HttpContext.Session.GetInt32("Account");
|
||||
if (currentAccount.HasValue)
|
||||
{
|
||||
var account = _mapper.Map<UserDto>(await _userService.GetAsync(currentAccount.Value));
|
||||
if (account.Role < (int)Roles.Administrator)
|
||||
return StatusCode(StatusCodes.Status403Forbidden);
|
||||
ViewData["Account"] = account;
|
||||
ViewData["EditedAccount"] = _mapper.Map<UserDto>(await _userService.GetAsync(id));
|
||||
return View();
|
||||
}
|
||||
return Redirect("/Auth/Login");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> EditUser(int id, UserDto user)
|
||||
{
|
||||
var currentAccount = HttpContext.Session.GetInt32("Account");
|
||||
if (currentAccount.HasValue)
|
||||
{
|
||||
var account = _mapper.Map<UserDto>(await _userService.GetAsync(currentAccount.Value));
|
||||
if (account.Role < (int)Roles.Administrator)
|
||||
return StatusCode(StatusCodes.Status403Forbidden);
|
||||
ViewData["Account"] = account;
|
||||
ViewData["EditedAccount"] = _mapper.Map<UserDto>(await _userService.GetAsync(id));
|
||||
//try
|
||||
//{
|
||||
user.Id = id;
|
||||
var model = _mapper.Map<UserModel>(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<IActionResult> CreateUser()
|
||||
{
|
||||
var currentAccount = HttpContext.Session.GetInt32("Account");
|
||||
if (currentAccount.HasValue)
|
||||
{
|
||||
var account = _mapper.Map<UserDto>(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<IActionResult> CreateUser(UserDto user)
|
||||
{
|
||||
var currentAccount = HttpContext.Session.GetInt32("Account");
|
||||
if (currentAccount.HasValue)
|
||||
{
|
||||
var account = _mapper.Map<UserDto>(await _userService.GetAsync(currentAccount.Value));
|
||||
if (account.Role < (int)Roles.Administrator)
|
||||
return StatusCode(StatusCodes.Status403Forbidden);
|
||||
ViewData["Account"] = account;
|
||||
try
|
||||
{
|
||||
var model = _mapper.Map<UserModel>(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<IActionResult> DeleteUser(int id)
|
||||
{
|
||||
var currentAccount = HttpContext.Session.GetInt32("Account");
|
||||
if (currentAccount.HasValue)
|
||||
{
|
||||
var account = _mapper.Map<UserDto>(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()
|
||||
{
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
@{
|
||||
foreach (var board in boards)
|
||||
{
|
||||
<p>@board.Name</p>
|
||||
<p>@board.Name | <a href="/Admin/EditBoard/@board.Id">Edit</a>, <a href="/Admin/DeleteBoard/@board.Id">Delete</a></p>
|
||||
}
|
||||
<p><a href="/Admin/AddBoard">New</a></p>
|
||||
}
|
||||
</div>
|
||||
|
|
33
NG_2023_Kanban/Views/Admin/CreateUser.cshtml
Normal file
33
NG_2023_Kanban/Views/Admin/CreateUser.cshtml
Normal file
|
@ -0,0 +1,33 @@
|
|||
@using NG_2023_Kanban.Enums
|
||||
|
||||
@using NG_2023_Kanban.DTOs
|
||||
@{
|
||||
ViewData["Title"] = "Create user";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">@ViewData["Title"]</h1>
|
||||
@if (ViewData["Error"] != null)
|
||||
{
|
||||
<p>@ViewData["Error"]</p>
|
||||
}
|
||||
<form method="POST">
|
||||
<label for="fullName">Full name: </label><br>
|
||||
<input type="text" id="fullName" name="fullName" required><br>
|
||||
<label for="username">Username: </label><br>
|
||||
<input type="text" id="username" name="username" required><br>
|
||||
<label for="password">Password: </label><br>
|
||||
<input type="password" id="password" name="password" required><br>
|
||||
<label for="role">Role: </label><br>
|
||||
<select id="role" name="role" required>
|
||||
@{
|
||||
foreach (var role in Roles.GetNames(typeof(Roles)))
|
||||
{
|
||||
var roleNumber = (int)Enum.Parse(typeof(Roles), role);
|
||||
<option value="@roleNumber">@role</option>
|
||||
}
|
||||
}
|
||||
</select><br>
|
||||
<button id="submit" type="submit">Submit</button>
|
||||
</form>
|
||||
</div>
|
44
NG_2023_Kanban/Views/Admin/EditUser.cshtml
Normal file
44
NG_2023_Kanban/Views/Admin/EditUser.cshtml
Normal file
|
@ -0,0 +1,44 @@
|
|||
@using NG_2023_Kanban.Enums
|
||||
|
||||
@using NG_2023_Kanban.DTOs
|
||||
@{
|
||||
var user = ViewData["EditedAccount"] as UserDto;
|
||||
}
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Edit user";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">@ViewData["Title"]</h1>
|
||||
@if (ViewData["Error"] != null)
|
||||
{
|
||||
<p>@ViewData["Error"]</p>
|
||||
}
|
||||
<form method="POST">
|
||||
<label for="fullName">Full name: </label><br>
|
||||
<input type="text" id="fullName" name="fullName" value="@user.FullName" required><br>
|
||||
<label for="username">Username: </label><br>
|
||||
<input type="text" id="username" name="username" value="@user.Username" required><br>
|
||||
<label for="password">Password: </label><br>
|
||||
<input type="password" id="password" name="password"><br>
|
||||
<label for="role" >Role: </label><br>
|
||||
<select id="role" name="role" required>
|
||||
@{
|
||||
foreach (var role in Roles.GetNames(typeof(Roles)))
|
||||
{
|
||||
var roleNumber = (int)Enum.Parse(typeof(Roles), role);
|
||||
@if (roleNumber == user.Role)
|
||||
{
|
||||
<option value="@roleNumber" selected>@role</option>
|
||||
}
|
||||
else
|
||||
{
|
||||
<option value="@roleNumber">@role</option>
|
||||
}
|
||||
}
|
||||
}
|
||||
</select><br>
|
||||
<button id="submit" type="submit">Submit</button>
|
||||
</form>
|
||||
</div>
|
|
@ -12,7 +12,8 @@
|
|||
@{
|
||||
foreach (var user in users)
|
||||
{
|
||||
<p>@user.Username</p>
|
||||
<p>@user.Username | <a href="/Admin/EditUser/@user.Id">Edit</a>, <a href="/Admin/DeleteUser/@user.Id">Delete</a></p>
|
||||
}
|
||||
<p><a href="/Admin/CreateUser">New</a></p>
|
||||
}
|
||||
</div>
|
||||
|
|
Reference in a new issue