diff --git a/src/WorkFlowCheck.API/Controllers/UserController.cs b/src/WorkFlowCheck.API/Controllers/UserController.cs index 2e5b7ab..963894f 100644 --- a/src/WorkFlowCheck.API/Controllers/UserController.cs +++ b/src/WorkFlowCheck.API/Controllers/UserController.cs @@ -72,7 +72,7 @@ namespace WorkFlowCheck.API.Controllers return retVal; } [HttpPost("UpdateUser")] - public async Task> UpdateUser([FromBody] UserDTO UserDTO) + public async Task> UpdateUser([FromBody] UserDTO userDTO) { var retVal = new ApiResponseDTO() { @@ -81,7 +81,7 @@ namespace WorkFlowCheck.API.Controllers try { - var result = await _userService.UpdateUserAsync(UserDTO); + var result = await _userService.UpdateUserAsync(userDTO); retVal.Data = result; } @@ -194,7 +194,7 @@ namespace WorkFlowCheck.API.Controllers return retVal; } [HttpPost("UpdateRole")] - public async Task> UpdateRole([FromBody] RoleDTO RoleDTO) + public async Task> UpdateRole([FromBody] RoleDTO roleDTO) { var retVal = new ApiResponseDTO() { @@ -203,7 +203,83 @@ namespace WorkFlowCheck.API.Controllers try { - var result = await _userService.UpdateRoleAsync(RoleDTO); + var result = await _userService.UpdateRoleAsync(roleDTO); + retVal.Data = result; + + } + catch (Exception ex) + { + retVal.IsSuccess = false; + Log.Error(ex.Message); + } + return retVal; + } + + + [HttpGet("GetUserRole/{id}")] + public async Task> GetUserRole(int id) + { + var retVal = new ApiResponseDTO() + { + IsSuccess = true, + }; + var RoleDTO = await _userService.GetUserRoleAsync(id); + + if (RoleDTO != null) + { + if (RoleDTO.Id == 0) + { + retVal.IsSuccess = false; + retVal.Errors.Add("No match!"); + retVal.Data = RoleDTO; + } + else + { + retVal.IsSuccess = true; + retVal.Data = RoleDTO; + } + } + else + { + retVal.IsSuccess = false; + retVal.Errors.Add("No data!"); + } + + return retVal; + } + [HttpGet("GetAllUserRoles")] + public async Task>> GetAllUserRoles() + { + var retVal = new ApiResponseDTO>() + { + IsSuccess = true, + }; + var RoleDTO = await _userService.GetAllUserRoleAsync(); + + if (RoleDTO != null) + { + retVal.IsSuccess = true; + retVal.Data = RoleDTO; + } + else + { + retVal.IsSuccess = false; + retVal.Errors.Add("No data!"); + } + + return retVal; + } + [HttpPost("UpdateUserRole")] + public async Task> UpdateUserRole([FromBody] UserRoleDTO userRoleDTO) + { + var retVal = new ApiResponseDTO() + { + IsSuccess = true + }; + + try + { + var result = await _userService.UpdateUserRoleAsync(userRoleDTO); retVal.Data = result; } diff --git a/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs b/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs index e6332d7..9dbae64 100644 --- a/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs +++ b/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs @@ -16,6 +16,9 @@ namespace WorkFlowCheck.BL.Mappings { CreateMap().ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.UserRoles.Select(ur => ur.Role))); CreateMap(); + CreateMap() + .ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.Role)) + .ForMember(dest => dest.UserDTO, opt => opt.MapFrom(src => src.User)); CreateMap() .ForMember(dest => dest.PasswordHash, opt => opt.MapFrom()); diff --git a/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs b/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs index 045bb22..3bc59e1 100644 --- a/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs +++ b/src/WorkFlowCheck.BL/Services/Interfaces/IUserService.cs @@ -20,7 +20,7 @@ namespace WorkFlowCheck.BL.Services.Interfaces Task GetUserRoleAsync(int Id); Task> GetAllUserRoleAsync(); - Task UpdateUserRoleAsync(RoleDTO roleDTO); + Task UpdateUserRoleAsync(UserRoleDTO userRoleDTO); diff --git a/src/WorkFlowCheck.BL/Services/UserService.cs b/src/WorkFlowCheck.BL/Services/UserService.cs index 1e6bb9d..8a22dc1 100644 --- a/src/WorkFlowCheck.BL/Services/UserService.cs +++ b/src/WorkFlowCheck.BL/Services/UserService.cs @@ -224,10 +224,118 @@ namespace WorkFlowCheck.BL.Services } return retVal; } - public async Task UpdateRoleAsync(RoleDTO roleDTO) => throw new NotImplementedException(); - public async Task GetUserRoleAsync(int Id) => throw new NotImplementedException(); - public Task> GetAllUserRoleAsync() => throw new NotImplementedException(); - public Task UpdateUserRoleAsync(RoleDTO roleDTO) => throw new NotImplementedException(); - + public async Task UpdateRoleAsync(RoleDTO roleDTO) + { + var retVal = new RoleDTO(); + try + { + if (roleDTO.Id == 0) + { + var role = new Role(); + role.RoleName = roleDTO.RoleName; + + _dbContext.Roles.Add(role); + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(role); + } + else + { + var role = await _dbContext.Roles.Where(w => w.Id == roleDTO.Id).FirstOrDefaultAsync(); + + if (role != null) + { + role.RoleName += roleDTO.RoleName; + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(role); + } + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + + return retVal; + } + + public async Task GetUserRoleAsync(int Id) + { + var retVal = new UserRoleDTO(); + + try + { + var userRole = await _dbContext.UserRoles.Where(w => w.Id == Id).FirstOrDefaultAsync(); + if (userRole != null) + { + retVal = _mapper.Map(userRole); + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + return retVal; + } + public async Task> GetAllUserRoleAsync() + { + var retVal = new List(); + try + { + var userRoles = await _dbContext.UserRoles + .Include(i => i.User) + .Include(i => i.Role) + .AsNoTracking() + .ToListAsync(); + if (userRoles != null) + { + retVal = _mapper.Map>(userRoles); + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + return retVal; + } + public async Task UpdateUserRoleAsync(UserRoleDTO userRoleDTO) + { + var retVal = new UserRoleDTO(); + try + { + if (userRoleDTO.Id == 0) + { + var userRole = new UserRole(); + userRole.UserId = userRoleDTO.UserId; + userRole.RoleId = userRoleDTO.RoleId; + + _dbContext.UserRoles.Add(userRole); + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(userRole); + } + else + { + var userRole = await _dbContext.UserRoles.Where(w => w.Id == userRoleDTO.Id).FirstOrDefaultAsync(); + + if (userRole != null) + { + userRole.UserId = userRoleDTO.UserId; + userRole.RoleId = userRoleDTO.RoleId; + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(userRole); + } + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + + return retVal; + } + } } diff --git a/src/WorkFlowCheck.Common/DTO/UserRoleDTO.cs b/src/WorkFlowCheck.Common/DTO/UserRoleDTO.cs index 81ca738..56ab73b 100644 --- a/src/WorkFlowCheck.Common/DTO/UserRoleDTO.cs +++ b/src/WorkFlowCheck.Common/DTO/UserRoleDTO.cs @@ -11,6 +11,9 @@ namespace WorkFlowCheck.Common.DTO { public int Id { get; set; } public int? RoleId { get; set; } = 0; + public RoleDTO RoleDTO { get; set; } public int UserId { get; set; } + public UserDTO UserDTO { get; set; } + } } diff --git a/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml b/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml index 0265bb5..9e378f2 100644 --- a/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml +++ b/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml @@ -1,4 +1,93 @@ @page @model WorkFlowCheck.Web.Pages.UserAndRole.UserRolePageModel @{ + ViewData["Title"] = "Felhasználók - szabályok"; +} +

@ViewData["Title"]

+ +
+
+ +
+ + + + + + + + + + + + + + + + + +
IDUser NameRole NameAction
IDUser NameRole NameAction
+
+ +@section Scripts { + } diff --git a/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml.cs b/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml.cs index 58828c2..b0e3381 100644 --- a/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/UserAndRole/UserRolePage.cshtml.cs @@ -1,12 +1,25 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using WorkFlowCheck.Web.Services.Interfaces; namespace WorkFlowCheck.Web.Pages.UserAndRole { public class UserRolePageModel : PageModel { + private readonly ILogger _logger; + private readonly IUserService _userService; + public UserRolePageModel(ILogger logger, IUserService userService) + { + _logger = logger; + _userService = userService; + } public void OnGet() { } + public async Task OnGetLoadUserRoles() + { + var results = await _userService.GetAllUserRoles(); + return new JsonResult(new { data = results }); + } } }