Skip to content

Commit

Permalink
feat: add get-users-api
Browse files Browse the repository at this point in the history
  • Loading branch information
amiralirahimii committed Aug 20, 2024
1 parent 6787d32 commit 68cdd41
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Application/DTOs/Identity/GetUser/GetUserResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Application.DTOs.Identity.GetUser;

public class GetUserResponse
{
public string FirstName { get; set; } = String.Empty;
public string LastName { get; set; } = String.Empty;
public string Email { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string Role { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public interface IUserManagerRepository
Task<IdentityResult> ChangePasswordAsync(AppUser user, string currentPassword, string newPassword);
Task<string> GetRoleAsync(AppUser user);
Task<bool> CheckPasswordAsync(AppUser user, string password);
Task<List<AppUser>> GetUsersAsync();
}
3 changes: 3 additions & 0 deletions src/Application/Interfaces/Services/IIdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Application.DTOs;
using Application.DTOs.Identity;
using Application.DTOs.Identity.ChangeRole;
using Application.DTOs.Identity.GetUser;
using Domain.Entities;

namespace Application.Interfaces.Services;

Expand All @@ -9,4 +11,5 @@ public interface IIdentityService
Task<Result<CreateUserResponse>> SignUpUser(CreateUserRequest createIdentityDto);
Task<Result<LoginUserResponse>> Login(LoginUserRequest loginDto);
Task<Result> ChangeRole(ChangeRoleRequest changeRoleRequest);
Task<List<GetUserResponse>> GetUsersAsync();
}
13 changes: 13 additions & 0 deletions src/Application/Mappers/IdentityMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Application.DTOs.Identity;
using Application.DTOs.Identity.GetUser;
using Domain.Entities;

namespace Application.Mappers;
Expand Down Expand Up @@ -40,4 +41,16 @@ public static LoginUserResponse ToLoginUserResponse(this AppUser appUser, string
Token = token
};
}

public static GetUserResponse ToGetUserResponse(this AppUser appUser, string role)
{
return new GetUserResponse
{
FirstName = appUser.FirstName,
LastName = appUser.LastName,
Email = appUser.Email,
UserName = appUser.UserName,
Role = role
};
}
}
16 changes: 16 additions & 0 deletions src/Application/Services/IdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Application.DTOs;
using Application.DTOs.Identity;
using Application.DTOs.Identity.ChangeRole;
using Application.DTOs.Identity.GetUser;
using Application.ExtensionMethods;
using Application.Interfaces;
using Application.Interfaces.Repositories;
Expand Down Expand Up @@ -91,4 +92,19 @@ public async Task<Result> ChangeRole(ChangeRoleRequest request)

return result.Succeeded ? Result.Ok() : Result.Fail(result.Errors.FirstMessage());
}

public async Task<List<GetUserResponse>> GetUsersAsync()
{
var users = await _userManagerRepository.GetUsersAsync();
var userWithRoles = new List<GetUserResponse>();

foreach (var user in users)
{
var role = await _userManagerRepository.GetRoleAsync(user);
var userResponse = user.ToGetUserResponse(role);
userWithRoles.Add(userResponse);
}

return userWithRoles;
}
}
6 changes: 6 additions & 0 deletions src/Infrastructure/Repositories/UserManagerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Application.Interfaces.Repositories;
using Domain.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Repositories;

Expand Down Expand Up @@ -79,4 +80,9 @@ public async Task<bool> CheckPasswordAsync(AppUser user, string password)
// TODO there should be a sign in manager service
return await _userManager.CheckPasswordAsync(user, password);
}

public async Task<List<AppUser>> GetUsersAsync()
{
return await _userManager.Users.ToListAsync();
}
}
10 changes: 10 additions & 0 deletions src/Web/Controllers/IdentityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ public async Task<IActionResult> ChangeRole([FromBody] ChangeRoleDto changeRoleD

return Ok("Role changed successfully!");
}

[HttpGet]
[Authorize]
[RequiresClaim(Claims.Role, AppRoles.Admin)]
public async Task<IActionResult> GetUsersAsync()
{
var appUsersWithRoles = await _identityService.GetUsersAsync();

return Ok(appUsersWithRoles);
}
}
1 change: 1 addition & 0 deletions src/Web/Mappers/IdentityMapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Application.DTOs.Identity;
using Application.DTOs.Identity.ChangeRole;
using Domain.Entities;
using Web.DTOs.Identity;

namespace Web.Mappers;
Expand Down

0 comments on commit 68cdd41

Please sign in to comment.