231 lines
8.3 KiB
C#
231 lines
8.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using UserManagement.Application.Dtos.Incomming;
|
|
using UserManagement.Application.Interfaces;
|
|
|
|
namespace UserManagement.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize(Roles = "Admin")]
|
|
public class UserController : Controller
|
|
{
|
|
// CTOR
|
|
private readonly IUserService _userService;
|
|
private readonly ILogger<UserController> _logger;
|
|
public UserController(IUserService userService, ILogger<UserController> logger)
|
|
{
|
|
_userService = userService;
|
|
_logger = logger;
|
|
}
|
|
|
|
// CREATE
|
|
[HttpPost]
|
|
[SwaggerOperation(Summary = "Create User")]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateUser([FromBody] CreatingUserDto creatingUserDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var createdUser = await _userService.AddUserAsync(creatingUserDto);
|
|
|
|
if (createdUser is not null)
|
|
{
|
|
return CreatedAtAction(nameof(GetUserById), new { id = createdUser.Id }, createdUser);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Erstellung des Benutzers fehlgeschlagen!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// READ ALL
|
|
[HttpGet]
|
|
[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.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("userId/{id}", Name = "GetUserById")]
|
|
[SwaggerOperation(Summary = "Get User by Id")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetUserById([FromRoute] int id, [FromQuery] bool includeRoles = true)
|
|
{
|
|
try
|
|
{
|
|
var user = await _userService.GetUserByIdAsync(id, includeRoles);
|
|
return Ok(user);
|
|
}
|
|
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 USERNAME
|
|
[HttpGet("username/{username}", Name = "GetUserByUsername")]
|
|
[SwaggerOperation(Summary = "Get User by Username")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetUserByUsername([FromRoute] string username, [FromQuery] bool includeRoles = true)
|
|
{
|
|
try
|
|
{
|
|
var user = await _userService.GetUserByUsernameAsync(username, includeRoles);
|
|
return Ok(user);
|
|
}
|
|
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 ROLE
|
|
[HttpGet("role/{role}", Name = "GetUsersByRole")]
|
|
[SwaggerOperation(Summary = "Get Users by Role")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetUsersByRole([FromRoute] string role)
|
|
{
|
|
try
|
|
{
|
|
var users = await _userService.GetUsersByRoleAsync(role);
|
|
return Ok(users);
|
|
}
|
|
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("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([FromRoute] int id, [FromBody] UpdatingUserDto updatingUserDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var updated = await _userService.UpdateUserAsync(updatingUserDto);
|
|
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);
|
|
}
|
|
}
|
|
|
|
// DELETE
|
|
[HttpDelete("userId/{id}", Name = "DeleteUser")]
|
|
[SwaggerOperation(Summary = "Delete User")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> DeleteUser([FromRoute] int id)
|
|
{
|
|
try
|
|
{
|
|
var deleted = await _userService.DeleteUserAsync(id);
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|