diff --git a/RelationshipAnalysis/Controllers/AdminController.cs b/RelationshipAnalysis/Controllers/AdminController.cs index 3231480..1ad22bb 100644 --- a/RelationshipAnalysis/Controllers/AdminController.cs +++ b/RelationshipAnalysis/Controllers/AdminController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authorization; +using System.Security.Claims; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RelationshipAnalysis.Dto; using RelationshipAnalysis.Enums; @@ -8,7 +9,6 @@ namespace RelationshipAnalysis.Controllers; - [Authorize(Roles = nameof(RoleTypes.Admin))] [ApiController] [Route("api/[controller]/[action]")] @@ -23,7 +23,6 @@ public class AdminController( IUserPasswordService passwordService, IUserReceiver userReceiver) : ControllerBase { - [HttpGet("{id:int}")] public async Task GetUser(int id) { @@ -31,29 +30,22 @@ public async Task GetUser(int id) var result = userInfoService.GetUser(user); return StatusCode((int)result.StatusCode, result.Data); } - - [HttpGet("{page:int}/{size:int}")] - public IActionResult GetAllUser(int page, int size) + + [HttpGet] + public IActionResult GetAllUser([FromQuery] int page, [FromQuery] int size) { var users = userReceiver.ReceiveAllUser(page, size); var result = allUserService.GetAllUser(users); return StatusCode((int)result.StatusCode, result.Data); } - - [HttpGet()] - public IActionResult GetAllUserCount() - { - var usersCount = userReceiver.ReceiveAllUserCount(); - return Ok(usersCount); - } - + [HttpGet] public IActionResult GetAllRoles() { var roles = roleReceiver.ReceiveAllRoles(); return Ok(roles); } - + [HttpPut("{id:int}")] public async Task UpdateUser(int id, UserUpdateInfoDto userUpdateInfoDto) { @@ -61,7 +53,7 @@ public async Task UpdateUser(int id, UserUpdateInfoDto userUpdate var result = await userUpdateInfoService.UpdateUserAsync(user, userUpdateInfoDto, Response); return StatusCode((int)result.StatusCode, result.Data); } - + [HttpPut("{id:int}")] public async Task UpdatePassword(int id, UserPasswordInfoDto passwordInfoDto) { @@ -69,22 +61,28 @@ public async Task UpdatePassword(int id, UserPasswordInfoDto pass var result = await passwordService.UpdatePasswordAsync(user, passwordInfoDto); return StatusCode((int)result.StatusCode, result.Data); } - + [HttpDelete("{id:int}")] public async Task DeleteUser(int id) { + var currentUser = await userReceiver.ReceiveUserAsync(User); var user = await userReceiver.ReceiveUserAsync(id); + if (id == currentUser.Id) + { + return StatusCode((int)StatusCodeType.BadRequest, Resources.DeleteAccountAccessErrorMessage); + } + var result = await userDeleteService.DeleteUser(user); return StatusCode((int)result.StatusCode, result.Data); } - + [HttpPost] public async Task CreateUser(CreateUserDto createUserDto) { var result = await userCreateService.CreateUser(createUserDto); return StatusCode((int)result.StatusCode, result.Data); } - + [HttpPut("{id:int}")] public async Task UpdateRoles(int id, List newRoles) { diff --git a/RelationshipAnalysis/Dto/GetAllUsersDto.cs b/RelationshipAnalysis/Dto/GetAllUsersDto.cs new file mode 100644 index 0000000..c73a4ed --- /dev/null +++ b/RelationshipAnalysis/Dto/GetAllUsersDto.cs @@ -0,0 +1,7 @@ +namespace RelationshipAnalysis.Dto; + +public class GetAllUsersDto +{ + public List Users { get; init; } + public int AllUserCount { get; init; } +} \ No newline at end of file diff --git a/RelationshipAnalysis/Resources.Designer.cs b/RelationshipAnalysis/Resources.Designer.cs index f3221bd..441795f 100644 --- a/RelationshipAnalysis/Resources.Designer.cs +++ b/RelationshipAnalysis/Resources.Designer.cs @@ -152,5 +152,11 @@ internal static string SuccessfulLoginMessage { return ResourceManager.GetString("SuccessfulLoginMessage", resourceCulture); } } + internal static string DeleteAccountAccessErrorMessage { + get { + return ResourceManager.GetString("DeleteAccountAccessErrorMessage", resourceCulture); + } + } + } } diff --git a/RelationshipAnalysis/Resources.resx b/RelationshipAnalysis/Resources.resx index 6031347..875b072 100644 --- a/RelationshipAnalysis/Resources.resx +++ b/RelationshipAnalysis/Resources.resx @@ -72,4 +72,7 @@ Login was successful! + + You cant delete your account! + \ No newline at end of file diff --git a/RelationshipAnalysis/Services/AdminPanelServices/Abstraction/IAllUserService.cs b/RelationshipAnalysis/Services/AdminPanelServices/Abstraction/IAllUserService.cs index ad38a37..6744c9c 100644 --- a/RelationshipAnalysis/Services/AdminPanelServices/Abstraction/IAllUserService.cs +++ b/RelationshipAnalysis/Services/AdminPanelServices/Abstraction/IAllUserService.cs @@ -5,5 +5,6 @@ namespace RelationshipAnalysis.Services.AdminPanelServices.Abstraction; public interface IAllUserService { - ActionResponse> GetAllUser(List users); + ActionResponse GetAllUser(List users); + int ReceiveAllUserCount(); } \ No newline at end of file diff --git a/RelationshipAnalysis/Services/AdminPanelServices/AllUserService.cs b/RelationshipAnalysis/Services/AdminPanelServices/AllUserService.cs index 677ee20..b1e099b 100644 --- a/RelationshipAnalysis/Services/AdminPanelServices/AllUserService.cs +++ b/RelationshipAnalysis/Services/AdminPanelServices/AllUserService.cs @@ -1,5 +1,6 @@ using AutoMapper; using Microsoft.IdentityModel.Tokens; +using RelationshipAnalysis.Context; using RelationshipAnalysis.Dto; using RelationshipAnalysis.Enums; using RelationshipAnalysis.Models.Auth; @@ -7,21 +8,36 @@ namespace RelationshipAnalysis.Services.AdminPanelServices; -public class AllUserService(IMapper mapper, IRoleReceiver rolesReceiver) : IAllUserService +public class AllUserService(ApplicationDbContext context, IMapper mapper, IRoleReceiver rolesReceiver) : IAllUserService { - public ActionResponse> GetAllUser(List users) + public int ReceiveAllUserCount() + { + var users = context.Users.ToList(); + return users.Count; + } + public ActionResponse GetAllUser(List users) { if (users.IsNullOrEmpty()) { return NotFoundResult(); } - var userOutputs = GetAllUserOutputs(users); + var usersList = GetAllUsersList(users); + var result = GetAllUsersOutPut(usersList); + + return SuccessResult(result); + } - return SuccessResult(userOutputs); + private GetAllUsersDto GetAllUsersOutPut(List usersList) + { + return new GetAllUsersDto() + { + Users = usersList, + AllUserCount = ReceiveAllUserCount() + }; } - private List GetAllUserOutputs(List users) + private List GetAllUsersList(List users) { var userOutputs = new List(); foreach (var user in users) @@ -36,18 +52,18 @@ private List GetAllUserOutputs(List users) return userOutputs; } - private ActionResponse> SuccessResult(List users) + private ActionResponse SuccessResult(GetAllUsersDto outPut) { - return new ActionResponse>() + return new ActionResponse() { - Data = users, + Data = outPut, StatusCode = StatusCodeType.Success }; } - private ActionResponse> NotFoundResult() + private ActionResponse NotFoundResult() { - return new ActionResponse>() + return new ActionResponse() { StatusCode = StatusCodeType.NotFound }; diff --git a/RelationshipAnalysis/Services/UserPanelServices/Abstraction/IUserReceiver.cs b/RelationshipAnalysis/Services/UserPanelServices/Abstraction/IUserReceiver.cs index 6ba90ff..943fa15 100644 --- a/RelationshipAnalysis/Services/UserPanelServices/Abstraction/IUserReceiver.cs +++ b/RelationshipAnalysis/Services/UserPanelServices/Abstraction/IUserReceiver.cs @@ -8,5 +8,5 @@ public interface IUserReceiver Task ReceiveUserAsync(ClaimsPrincipal userClaims); Task ReceiveUserAsync(int id); List ReceiveAllUser(int page, int size); - int ReceiveAllUserCount(); + } \ No newline at end of file diff --git a/RelationshipAnalysis/Services/UserPanelServices/UserReceiver.cs b/RelationshipAnalysis/Services/UserPanelServices/UserReceiver.cs index 7d1a6fc..6f4791d 100644 --- a/RelationshipAnalysis/Services/UserPanelServices/UserReceiver.cs +++ b/RelationshipAnalysis/Services/UserPanelServices/UserReceiver.cs @@ -26,10 +26,4 @@ public List ReceiveAllUser(int page, int size) var users = context.Users.Skip(page * size).Take(size).ToList(); return users; } - - public int ReceiveAllUserCount() - { - var users = context.Users.ToList(); - return users.Count; - } } \ No newline at end of file