Added Logging + some little changes in repositories
This commit is contained in:
parent
ebd07204b3
commit
3df98fb399
@ -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, includeRoles: true);
|
||||
var user = await _userService.GetUserByUsernameAsync(login.Username, includeRoles: true);
|
||||
if (user is null)
|
||||
{
|
||||
return Unauthorized("Benutzername und Passwort stimmen nicht überein!");
|
||||
|
||||
@ -13,9 +13,11 @@ namespace UserManagement.API.Controllers
|
||||
{
|
||||
// CTOR
|
||||
private readonly IRoleService _roleService;
|
||||
public RoleController(IRoleService roleService)
|
||||
private readonly ILogger<UserController> _logger;
|
||||
public RoleController(IRoleService roleService, ILogger<UserController> logger)
|
||||
{
|
||||
_roleService = roleService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// CREATE
|
||||
@ -26,7 +28,6 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> CreateRole([FromBody] CreatingRoleDto creatingRoleDto)
|
||||
{
|
||||
// Validate incomming model
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
@ -34,25 +35,20 @@ namespace UserManagement.API.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
// Try to add role asynchronously
|
||||
var result = await _roleService.AddRoleAsync(creatingRoleDto);
|
||||
var createdRole = await _roleService.AddRoleAsync(creatingRoleDto);
|
||||
|
||||
// If role is successfully created, return a CreatedAtAction response with the created resource
|
||||
if (result is not null)
|
||||
if (createdRole is not null)
|
||||
{
|
||||
var id = result.Id;
|
||||
var createdResource = new { Id = id };
|
||||
var actionName = nameof(GetRoleById);
|
||||
var routeValue = new { id = createdResource.Id };
|
||||
return CreatedAtAction(actionName, routeValue, createdResource);
|
||||
return CreatedAtAction(nameof(GetRoleById), new { id = createdRole.Id }, createdRole);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("geht nix");
|
||||
return BadRequest("Erstellung der Rolle fehlgeschlagen!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
||||
}
|
||||
}
|
||||
@ -61,95 +57,145 @@ namespace UserManagement.API.Controllers
|
||||
[HttpGet]
|
||||
[SwaggerOperation(Summary = "Get all Roles")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetRoles()
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetAllRoles()
|
||||
{
|
||||
var roles = await _roleService.GetAllAsync();
|
||||
try
|
||||
{
|
||||
var roles = await _roleService.GetAllRolesAsync();
|
||||
return Ok(roles);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
[HttpGet("id/{id}", Name = "GetRoleById")]
|
||||
[HttpGet("roleId/{id}", Name = "GetRoleById")]
|
||||
[SwaggerOperation(Summary = "Get Role by Id")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetRoleById(int id)
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetRoleById([FromRoute] int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
try
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
var role = await _roleService.GetByIdAsync(id);
|
||||
if (role == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
var role = await _roleService.GetRoleByIdAsync(id);
|
||||
return Ok(role);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// READ BY NAME
|
||||
[HttpGet("name/{name}", Name = "GetRoleByName")]
|
||||
[HttpGet("rolename/{name}", Name = "GetRoleByName")]
|
||||
[SwaggerOperation(Summary = "Get Role by Name")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetRoleByName(string name)
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetRoleByName([FromRoute] string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
try
|
||||
{
|
||||
return BadRequest("Name cannot be empty");
|
||||
}
|
||||
var role = await _roleService.GetByNameAsync(name);
|
||||
if (role == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
var role = await _roleService.GetRoleByNameAsync(name);
|
||||
return Ok(role);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
[HttpPut("id/{id}", Name = "UpdateRole")]
|
||||
[HttpPut("roleId/{id}", Name = "UpdateRole")]
|
||||
[SwaggerOperation(Summary = "Update Role")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> UpdateRole(int id, UpdatingRoleDto updatingRoleDto)
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateRole([FromRoute] int id,[FromBody] UpdatingRoleDto updatingRoleDto)
|
||||
{
|
||||
if (id <= 0)
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var updated = await _roleService.UpdateRoleAsync(updatingRoleDto);
|
||||
|
||||
if (!updated)
|
||||
{
|
||||
return BadRequest("Update failed");
|
||||
return NoContent();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
// DELETE
|
||||
[HttpDelete("id/{id}", Name = "DeleteRole")]
|
||||
[HttpDelete("roleId/{id}", Name = "DeleteRole")]
|
||||
[SwaggerOperation(Summary = "Delete Role")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> DeleteRole([FromRoute] int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
try
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
|
||||
var deleted = await _roleService.DeleteRoleAsync(id);
|
||||
|
||||
if (!deleted)
|
||||
{
|
||||
return BadRequest("Deletion failed");
|
||||
return NoContent();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,7 +26,6 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<IActionResult> CreateUser([FromBody] CreatingUserDto creatingUserDto)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
@ -49,7 +48,8 @@ namespace UserManagement.API.Controllers
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,30 +58,37 @@ namespace UserManagement.API.Controllers
|
||||
[SwaggerOperation(Summary = "Get all Users")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetAllUsers([FromQuery] bool includeRoles = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var users = await _userService.GetUsersAsync(includeRoles);
|
||||
var users = await _userService.GetAllUsersAsync(includeRoles);
|
||||
return Ok(users);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
[HttpGet("id/{id}", Name = "GetUserById")]
|
||||
[HttpGet("userId/{id}", Name = "GetUserById")]
|
||||
[SwaggerOperation(Summary = "Get User by Id")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserById(int id, [FromQuery] bool includeRoles = true)
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetUserById([FromRoute] int id, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _userService.GetByIdAsync(id, includeRoles);
|
||||
var user = await _userService.GetUserByIdAsync(id, includeRoles);
|
||||
return Ok(user);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
@ -92,6 +99,11 @@ namespace UserManagement.API.Controllers
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
@ -100,11 +112,12 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserByUsername(string username, [FromQuery] bool includeRoles = true)
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetUserByUsername([FromRoute] string username, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _userService.GetByUsernameAsync(username, includeRoles);
|
||||
var user = await _userService.GetUserByUsernameAsync(username, includeRoles);
|
||||
return Ok(user);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
@ -115,6 +128,11 @@ namespace UserManagement.API.Controllers
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
@ -123,11 +141,12 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUsersByRole(string role)
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetUsersByRole([FromRoute] string role)
|
||||
{
|
||||
try
|
||||
{
|
||||
var users = await _userService.GetByRoleAsync(role);
|
||||
var users = await _userService.GetUsersByRoleAsync(role);
|
||||
return Ok(users);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
@ -138,20 +157,31 @@ namespace UserManagement.API.Controllers
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
[HttpPut("id/{id}", Name = "UpdateUser")]
|
||||
[HttpPut("userId/{id}", Name = "UpdateUser")]
|
||||
[SwaggerOperation(Summary = "Update User")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateUser(int id, UpdatingUserDto updatingUserDto)
|
||||
public async Task<IActionResult> UpdateUser([FromRoute] int id, [FromBody] UpdatingUserDto updatingUserDto)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var updated = await _userService.UpdateUserAsync(updatingUserDto);
|
||||
return Ok(updated);
|
||||
return NoContent();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
@ -169,17 +199,18 @@ namespace UserManagement.API.Controllers
|
||||
}
|
||||
|
||||
// DELETE
|
||||
[HttpDelete("id/{id}", Name = "DeleteUser")]
|
||||
[HttpDelete("userId/{id}", Name = "DeleteUser")]
|
||||
[SwaggerOperation(Summary = "Delete User")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> DeleteUser([FromBody] int id)
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> DeleteUser([FromRoute] int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var deleted = await _userService.DeleteUserAsync(id);
|
||||
return Ok(deleted);
|
||||
return NoContent();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
@ -189,6 +220,11 @@ namespace UserManagement.API.Controllers
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,13 +10,13 @@ namespace UserManagement.Application.Interfaces
|
||||
Task<Role?> AddRoleAsync(CreatingRoleDto creatingRoleDto);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<ReadingRoleDto>> GetAllAsync();
|
||||
Task<IEnumerable<ReadingRoleDto>> GetAllRolesAsync();
|
||||
|
||||
// READ BY ID
|
||||
Task<ReadingRoleDto> GetByIdAsync(int id);
|
||||
Task<ReadingRoleDto> GetRoleByIdAsync(int id);
|
||||
|
||||
// READ BY NAME
|
||||
Task<ReadingRoleDto> GetByNameAsync(string name);
|
||||
Task<ReadingRoleDto> GetRoleByNameAsync(string name);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateRoleAsync(UpdatingRoleDto updatingRoleDto);
|
||||
|
||||
@ -10,16 +10,16 @@ namespace UserManagement.Application.Interfaces
|
||||
Task<User?> AddUserAsync(CreatingUserDto creatingUserDto);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true);
|
||||
Task<IEnumerable<ReadingUserDto>> GetAllUsersAsync(bool includeRoles = true);
|
||||
|
||||
// READ BY ID
|
||||
Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true);
|
||||
Task<ReadingUserDto> GetUserByIdAsync(int id, bool includeRoles = true);
|
||||
|
||||
// READ BY USERNAME
|
||||
Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true);
|
||||
Task<ReadingUserDto> GetUserByUsernameAsync(string username, bool includeRoles = true);
|
||||
|
||||
// READ BY ROLE
|
||||
Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role);
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersByRoleAsync(string role);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto);
|
||||
|
||||
@ -22,51 +22,103 @@ namespace UserManagement.Application.Services
|
||||
public async Task<Role?> AddRoleAsync(CreatingRoleDto creatingRoleDto)
|
||||
{
|
||||
var role = _mapper.Map<Role>(creatingRoleDto);
|
||||
|
||||
var created = await _roleRepository.AddAsync(role);
|
||||
|
||||
return created;
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<ReadingRoleDto>> GetAllAsync()
|
||||
public async Task<IEnumerable<ReadingRoleDto>> GetAllRolesAsync()
|
||||
{
|
||||
var roles = await _roleRepository.GetAllAsync();
|
||||
|
||||
if (roles == null)
|
||||
{
|
||||
throw new KeyNotFoundException("Keine Rollen gefunden!");
|
||||
}
|
||||
|
||||
var readDto = _mapper.Map<IEnumerable<ReadingRoleDto>>(roles);
|
||||
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<ReadingRoleDto> GetByIdAsync(int id)
|
||||
public async Task<ReadingRoleDto> GetRoleByIdAsync(int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
throw new ArgumentException("Ungültige Id!");
|
||||
}
|
||||
|
||||
var role = await _roleRepository.GetByIdAsync(id);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
||||
}
|
||||
|
||||
var readDto = _mapper.Map<ReadingRoleDto>(role);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY NAME
|
||||
public async Task<ReadingRoleDto> GetByNameAsync(string name)
|
||||
public async Task<ReadingRoleDto> GetRoleByNameAsync(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
throw new ArgumentException("Ungültiger Rollenname!");
|
||||
}
|
||||
|
||||
var role = await _roleRepository.GetByNameAsync(name);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
||||
}
|
||||
|
||||
var readDto = _mapper.Map<ReadingRoleDto>(role);
|
||||
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
public async Task<bool> UpdateRoleAsync(UpdatingRoleDto updatingRoleDto)
|
||||
{
|
||||
if (updatingRoleDto.Id <= 0)
|
||||
{
|
||||
throw new ArgumentException("Ungültige Id!");
|
||||
}
|
||||
|
||||
var role = _mapper.Map<Role>(updatingRoleDto);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
||||
}
|
||||
|
||||
bool isUpdated = await _roleRepository.UpdateAsync(role);
|
||||
|
||||
return isUpdated;
|
||||
}
|
||||
|
||||
// DELETE
|
||||
public async Task<bool> DeleteRoleAsync(int id)
|
||||
{
|
||||
Role? role = await _roleRepository.GetByIdAsync(id);
|
||||
if (id <= 0)
|
||||
{
|
||||
throw new ArgumentException("Ungültige Id!");
|
||||
}
|
||||
|
||||
if (role is null)
|
||||
return false;
|
||||
var role = await _roleRepository.GetByIdAsync(id);
|
||||
|
||||
if (role == null)
|
||||
{
|
||||
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
||||
}
|
||||
|
||||
bool isDeleted = await _roleRepository.DeleteAsync(role);
|
||||
|
||||
return isDeleted;
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ namespace UserManagement.Application.Services
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true)
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetAllUsersAsync(bool includeRoles = true)
|
||||
{
|
||||
var users = await _userRepository.GetAllAsync(includeRoles);
|
||||
|
||||
@ -51,7 +51,7 @@ namespace UserManagement.Application.Services
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true)
|
||||
public async Task<ReadingUserDto> GetUserByIdAsync(int id, bool includeRoles = true)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
@ -71,7 +71,7 @@ namespace UserManagement.Application.Services
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
public async Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true)
|
||||
public async Task<ReadingUserDto> GetUserByUsernameAsync(string username, bool includeRoles = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
@ -91,7 +91,7 @@ namespace UserManagement.Application.Services
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role)
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersByRoleAsync(string role)
|
||||
{
|
||||
if (string.IsNullOrEmpty(role))
|
||||
{
|
||||
|
||||
@ -24,19 +24,25 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<Role>> GetAllAsync()
|
||||
{
|
||||
return await _context.Roles.ToListAsync();
|
||||
return await _context.Roles
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<Role?> GetByIdAsync(int id)
|
||||
{
|
||||
return await _context.Roles.FindAsync(id);
|
||||
return await _context.Roles
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.Id == id);
|
||||
}
|
||||
|
||||
// READ BY NAME
|
||||
public async Task<Role?> GetByNameAsync(string name)
|
||||
{
|
||||
return await _context.Roles.FirstOrDefaultAsync(n => n.Name == name);
|
||||
return await _context.Roles
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(n => n.Name == name);
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
|
||||
@ -37,13 +37,10 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
{
|
||||
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();
|
||||
return await query.FirstOrDefaultAsync(u => u.Id == id);
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
@ -51,13 +48,10 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
{
|
||||
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();
|
||||
return await query.FirstOrDefaultAsync(u => u.UserName == username);
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
@ -68,8 +62,6 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.Where(ur => ur.UserRoles!.Any(r => r.Role!.Name == role));
|
||||
|
||||
var sql = query.ToQueryString();
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user