FlexibleQueries+GetUsersByRole
This commit is contained in:
parent
556571ae78
commit
dc3fb100b1
@ -29,7 +29,7 @@ namespace UserManagement.API.Controllers
|
||||
public async Task<IActionResult> Login([FromBody] LoginDto login)
|
||||
{
|
||||
// Validate user
|
||||
var user = await _userService.GetByUsernameAsync(login.Username);
|
||||
var user = await _userService.GetByUsernameAsync(login.Username, includeRoles: true);
|
||||
if (user is null)
|
||||
{
|
||||
return Unauthorized();
|
||||
|
||||
@ -25,7 +25,7 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
//[Authorize(Roles = "Admin")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<IActionResult> CreateUser([FromBody] CreatingUserDto creatingUserDto)
|
||||
{
|
||||
// Validate incomming model
|
||||
@ -63,9 +63,9 @@ namespace UserManagement.API.Controllers
|
||||
[HttpGet]
|
||||
[SwaggerOperation(Summary = "Get all Users")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAllUsers()
|
||||
public async Task<IActionResult> GetAllUsers([FromQuery] bool includeRoles = true)
|
||||
{
|
||||
var users = await _userService.GetUsersAsync();
|
||||
var users = await _userService.GetUsersAsync(includeRoles);
|
||||
return Ok(users);
|
||||
}
|
||||
|
||||
@ -75,13 +75,13 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserById(int id)
|
||||
public async Task<IActionResult> GetUserById(int id, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
var user = await _userService.GetByIdAsync(id);
|
||||
var user = await _userService.GetByIdAsync(id, includeRoles);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
@ -95,13 +95,13 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserByUsername(string username)
|
||||
public async Task<IActionResult> GetUserByUsername(string username, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
return BadRequest("Username connot be empty");
|
||||
}
|
||||
var user = await _userService.GetByUsernameAsync(username);
|
||||
var user = await _userService.GetByUsernameAsync(username, includeRoles);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
@ -109,6 +109,22 @@ namespace UserManagement.API.Controllers
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
[HttpGet("role/{role}", Name = "GetUsersByRole")]
|
||||
[SwaggerOperation(Summary = "Get Users by Role")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUsersByRole(string role)
|
||||
{
|
||||
if (string.IsNullOrEmpty(role))
|
||||
{
|
||||
return BadRequest("Role cannot be empty");
|
||||
}
|
||||
var users = await _userService.GetByRoleAsync(role);
|
||||
return Ok(users);
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
[HttpPut("id/{id}", Name = "UpdateUser")]
|
||||
[SwaggerOperation(Summary = "Update User")]
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
public class ReadingUserRolesDto
|
||||
{
|
||||
public int RoleId { get; set; }
|
||||
|
||||
public string RoleName { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,20 +10,20 @@ namespace UserManagement.Application.Interfaces
|
||||
Task<User?> AddUserAsync(CreatingUserDto creatingUserDto);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersAsync();
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true);
|
||||
|
||||
// READ BY ID
|
||||
Task<ReadingUserDto> GetByIdAsync(int id);
|
||||
Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true);
|
||||
|
||||
// READ BY USERNAME
|
||||
Task<ReadingUserDto> GetByUsernameAsync(string username);
|
||||
Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true);
|
||||
|
||||
// READ BY ROLE
|
||||
Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto);
|
||||
|
||||
//// UPDATE USER ROLE
|
||||
//Task UpdateUserRoleAsync(int userId, int roleId);
|
||||
|
||||
// DELETE
|
||||
Task<bool> DeleteUserAsync(int id);
|
||||
}
|
||||
|
||||
@ -33,9 +33,9 @@ namespace UserManagement.Application.MappingProfiles
|
||||
|
||||
CreateMap<User, ReadingUserDto>()
|
||||
.ForMember(dest => dest.UserRoles, opt => opt.MapFrom(src =>
|
||||
src.UserRoles.Select(ur => new ReadingRoleDto
|
||||
src.UserRoles!.Select(ur => new ReadingRoleDto
|
||||
{
|
||||
Id = ur.Role.Id,
|
||||
Id = ur.Role!.Id,
|
||||
Name = ur.Role.Name
|
||||
}).ToList()));
|
||||
|
||||
|
||||
@ -15,9 +15,9 @@ namespace UserManagement.Application.Services
|
||||
// AUTHENTICATE
|
||||
public async Task<bool> ValidateAsync(string username, string password)
|
||||
{
|
||||
var user = await _userRepository.GetByUsernameAsync(username);
|
||||
var user = await _userRepository.GetByUsernameAsync(username, includeRoles: true);
|
||||
|
||||
return BCrypt.Net.BCrypt.Verify(password, user.PasswordHash);
|
||||
return BCrypt.Net.BCrypt.Verify(password, user!.PasswordHash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,29 +50,37 @@ namespace UserManagement.Application.Services
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync()
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true)
|
||||
{
|
||||
var users = await _userRepository.GetAllAsync();
|
||||
var users = await _userRepository.GetAllAsync(includeRoles);
|
||||
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<ReadingUserDto> GetByIdAsync(int id)
|
||||
public async Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true)
|
||||
{
|
||||
var user = await _userRepository.GetByIdAsync(id);
|
||||
var user = await _userRepository.GetByIdAsync(id, includeRoles);
|
||||
var readDto = _mapper.Map<ReadingUserDto>(user);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
public async Task<ReadingUserDto> GetByUsernameAsync(string username)
|
||||
public async Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true)
|
||||
{
|
||||
var user = await _userRepository.GetByUsernameAsync(username);
|
||||
var user = await _userRepository.GetByUsernameAsync(username, includeRoles);
|
||||
var readDto = _mapper.Map<ReadingUserDto>(user);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role)
|
||||
{
|
||||
var users = await _userRepository.GetByRoleAsync(role);
|
||||
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
public async Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto)
|
||||
{
|
||||
@ -90,7 +98,7 @@ namespace UserManagement.Application.Services
|
||||
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(updatingUserDto.Password);
|
||||
}
|
||||
|
||||
user.UserRoles.Clear();
|
||||
user.UserRoles!.Clear();
|
||||
|
||||
foreach(var roleId in updatingUserDto.RoleIds)
|
||||
{
|
||||
@ -109,25 +117,6 @@ namespace UserManagement.Application.Services
|
||||
return isUpdated;
|
||||
}
|
||||
|
||||
//// UPDATE USER ROLE -- die Rolle eines Users aktualisieren
|
||||
//public async Task UpdateUserRoleAsync(int userId, int roleId)
|
||||
//{
|
||||
// var user = await _userRepository.GetByIdAsync(userId);
|
||||
// if (user == null)
|
||||
// {
|
||||
// throw new ArgumentException("User not found");
|
||||
// }
|
||||
|
||||
// var role = await _roleRepository.GetByIdAsync(roleId);
|
||||
// if (role == null)
|
||||
// {
|
||||
// throw new ArgumentException("Role not found");
|
||||
// }
|
||||
|
||||
// user.UserRoles = roleId;
|
||||
// await _userRepository.SaveAsync();
|
||||
//}
|
||||
|
||||
// DELETE
|
||||
public async Task<bool> DeleteUserAsync(int id)
|
||||
{
|
||||
|
||||
@ -16,7 +16,7 @@ namespace UserManagement.Domain.Entities
|
||||
public required string Name { get; set; }
|
||||
|
||||
[Column("USERS")]
|
||||
public ICollection<UserRole>? UserRoles { get; init; } = new Collection<UserRole>();
|
||||
public ICollection<UserRole>? UserRoles { get; set; } = new Collection<UserRole>();
|
||||
|
||||
[Required]
|
||||
[Column("CREATION_DATE", TypeName = "datetime")]
|
||||
|
||||
@ -23,16 +23,10 @@ namespace UserManagement.Domain.Entities
|
||||
[Column("LAST_NAME")]
|
||||
public string LastName { get; set; }
|
||||
|
||||
//[Obsolete("Use password hash")]
|
||||
//[Required]
|
||||
//[Column("PASSWORD")]
|
||||
//public required string Password { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column("PASSWORD_HASH")]
|
||||
public required string PasswordHash { get; set; }
|
||||
|
||||
[Column("ROLES")]
|
||||
public ICollection<UserRole>? UserRoles { get; set; } = new Collection<UserRole>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,21 +8,21 @@ namespace UserManagement.Infrastructure.Interfaces
|
||||
Task<User?> AddAsync(User user);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<User>> GetAllAsync();
|
||||
Task<IEnumerable<User>> GetAllAsync(bool includeRoles = true);
|
||||
|
||||
// READ BY ID
|
||||
Task<User?> GetByIdAsync(int id);
|
||||
Task<User?> GetByIdAsync(int id, bool includeRoles = true);
|
||||
|
||||
// READ BY USERNAME
|
||||
Task<User?> GetByUsernameAsync(string username);
|
||||
Task<User?> GetByUsernameAsync(string username, bool includeRoles = true);
|
||||
|
||||
// READ BY ROLE
|
||||
Task<IEnumerable<User>> GetByRoleAsync(string role);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateAsync(User user);
|
||||
|
||||
// DELETE
|
||||
Task<bool> DeleteAsync(User user);
|
||||
|
||||
// SAVE
|
||||
Task<bool> SaveAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,30 +22,52 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<User>> GetAllAsync()
|
||||
public async Task<IEnumerable<User>> GetAllAsync(bool includeRoles = true)
|
||||
{
|
||||
return await _context.Users
|
||||
.Include(u => u.UserRoles)
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.ToListAsync();
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<User?> GetByIdAsync(int id)
|
||||
public async Task<User?> GetByIdAsync(int id, bool includeRoles = true)
|
||||
{
|
||||
return await _context.Users.Where(user => user.Id == id)
|
||||
.Include(user => user.UserRoles)
|
||||
.ThenInclude(ur =>ur.Role)
|
||||
.FirstAsync();
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (id > 0)
|
||||
query = query.Where(u => u.Id == id);
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
public async Task<User?> GetByUsernameAsync(string username)
|
||||
public async Task<User?> GetByUsernameAsync(string username, bool includeRoles = true)
|
||||
{
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (!string.IsNullOrEmpty(username))
|
||||
query = query.Where(u => u.UserName == username);
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
public async Task<IEnumerable<User>> GetByRoleAsync(string role)
|
||||
{
|
||||
return await _context.Users
|
||||
.Include(user => user.UserRoles)
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.FirstOrDefaultAsync(u => u.UserName == username);
|
||||
.Include(u => u.UserRoles)!
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.Where(ur => ur.UserRoles!.Any(r => r.Role!.Name == role))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
@ -63,12 +85,5 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
var result = await _context.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
// SAVE
|
||||
public async Task<bool> SaveAsync()
|
||||
{
|
||||
var saved = await _context.SaveChangesAsync();
|
||||
return saved > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user