Skip to content

Commit

Permalink
fix(AdminController): Fix admin panel bugs! (#21)
Browse files Browse the repository at this point in the history
* Fix From Query in GetAllUsers!

* Add AllUsersCount to GetAllUsers.

* Fix DeleteUser bug!
  • Loading branch information
msmahdinejad authored Aug 19, 2024
1 parent eefa3c9 commit a1b5f6c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 37 deletions.
36 changes: 17 additions & 19 deletions RelationshipAnalysis/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,7 +9,6 @@

namespace RelationshipAnalysis.Controllers;


[Authorize(Roles = nameof(RoleTypes.Admin))]
[ApiController]
[Route("api/[controller]/[action]")]
Expand All @@ -23,68 +23,66 @@ public class AdminController(
IUserPasswordService passwordService,
IUserReceiver userReceiver) : ControllerBase
{

[HttpGet("{id:int}")]
public async Task<IActionResult> GetUser(int id)
{
var user = await userReceiver.ReceiveUserAsync(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<IActionResult> UpdateUser(int id, UserUpdateInfoDto userUpdateInfoDto)
{
var user = await userReceiver.ReceiveUserAsync(id);
var result = await userUpdateInfoService.UpdateUserAsync(user, userUpdateInfoDto, Response);
return StatusCode((int)result.StatusCode, result.Data);
}

[HttpPut("{id:int}")]
public async Task<IActionResult> UpdatePassword(int id, UserPasswordInfoDto passwordInfoDto)
{
var user = await userReceiver.ReceiveUserAsync(id);
var result = await passwordService.UpdatePasswordAsync(user, passwordInfoDto);
return StatusCode((int)result.StatusCode, result.Data);
}

[HttpDelete("{id:int}")]
public async Task<IActionResult> 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<IActionResult> CreateUser(CreateUserDto createUserDto)
{
var result = await userCreateService.CreateUser(createUserDto);
return StatusCode((int)result.StatusCode, result.Data);
}

[HttpPut("{id:int}")]
public async Task<IActionResult> UpdateRoles(int id, List<string> newRoles)
{
Expand Down
7 changes: 7 additions & 0 deletions RelationshipAnalysis/Dto/GetAllUsersDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RelationshipAnalysis.Dto;

public class GetAllUsersDto
{
public List<UserOutputInfoDto> Users { get; init; }
public int AllUserCount { get; init; }
}
6 changes: 6 additions & 0 deletions RelationshipAnalysis/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RelationshipAnalysis/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@
<data name="SuccessfulLoginMessage" xml:space="preserve">
<value>Login was successful!</value>
</data>
<data name="DeleteAccountAccessErrorMessage" xml:space="preserve">
<value>You cant delete your account!</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace RelationshipAnalysis.Services.AdminPanelServices.Abstraction;

public interface IAllUserService
{
ActionResponse<List<UserOutputInfoDto>> GetAllUser(List<User> users);
ActionResponse<GetAllUsersDto> GetAllUser(List<User> users);
int ReceiveAllUserCount();
}
36 changes: 26 additions & 10 deletions RelationshipAnalysis/Services/AdminPanelServices/AllUserService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
using AutoMapper;
using Microsoft.IdentityModel.Tokens;
using RelationshipAnalysis.Context;
using RelationshipAnalysis.Dto;
using RelationshipAnalysis.Enums;
using RelationshipAnalysis.Models.Auth;
using RelationshipAnalysis.Services.AdminPanelServices.Abstraction;

namespace RelationshipAnalysis.Services.AdminPanelServices;

public class AllUserService(IMapper mapper, IRoleReceiver rolesReceiver) : IAllUserService
public class AllUserService(ApplicationDbContext context, IMapper mapper, IRoleReceiver rolesReceiver) : IAllUserService
{
public ActionResponse<List<UserOutputInfoDto>> GetAllUser(List<User> users)
public int ReceiveAllUserCount()
{
var users = context.Users.ToList();
return users.Count;
}
public ActionResponse<GetAllUsersDto> GetAllUser(List<User> 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<UserOutputInfoDto> usersList)
{
return new GetAllUsersDto()
{
Users = usersList,
AllUserCount = ReceiveAllUserCount()
};
}

private List<UserOutputInfoDto> GetAllUserOutputs(List<User> users)
private List<UserOutputInfoDto> GetAllUsersList(List<User> users)
{
var userOutputs = new List<UserOutputInfoDto>();
foreach (var user in users)
Expand All @@ -36,18 +52,18 @@ private List<UserOutputInfoDto> GetAllUserOutputs(List<User> users)
return userOutputs;
}

private ActionResponse<List<UserOutputInfoDto>> SuccessResult(List<UserOutputInfoDto> users)
private ActionResponse<GetAllUsersDto> SuccessResult(GetAllUsersDto outPut)
{
return new ActionResponse<List<UserOutputInfoDto>>()
return new ActionResponse<GetAllUsersDto>()
{
Data = users,
Data = outPut,
StatusCode = StatusCodeType.Success
};
}

private ActionResponse<List<UserOutputInfoDto>> NotFoundResult()
private ActionResponse<GetAllUsersDto> NotFoundResult()
{
return new ActionResponse<List<UserOutputInfoDto>>()
return new ActionResponse<GetAllUsersDto>()
{
StatusCode = StatusCodeType.NotFound
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IUserReceiver
Task<User> ReceiveUserAsync(ClaimsPrincipal userClaims);
Task<User> ReceiveUserAsync(int id);
List<User> ReceiveAllUser(int page, int size);
int ReceiveAllUserCount();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@ public List<User> 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;
}
}

0 comments on commit a1b5f6c

Please sign in to comment.