202 lines
7.1 KiB
C#
202 lines
7.1 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 RoleController : ControllerBase
|
|
{
|
|
// CTOR
|
|
private readonly IRoleService _roleService;
|
|
private readonly ILogger<UserController> _logger;
|
|
public RoleController(IRoleService roleService, ILogger<UserController> logger)
|
|
{
|
|
_roleService = roleService;
|
|
_logger = logger;
|
|
}
|
|
|
|
// CREATE
|
|
[HttpPost]
|
|
[SwaggerOperation(Summary = "Create Role")]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateRole([FromBody] CreatingRoleDto creatingRoleDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var createdRole = await _roleService.AddRoleAsync(creatingRoleDto);
|
|
|
|
if (createdRole is not null)
|
|
{
|
|
return CreatedAtAction(nameof(GetRoleById), new { id = createdRole.Id }, createdRole);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Erstellung der Rolle fehlgeschlagen!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
// READ ALL
|
|
[HttpGet]
|
|
[SwaggerOperation(Summary = "Get all Roles")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetAllRoles()
|
|
{
|
|
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("roleId/{id}", Name = "GetRoleById")]
|
|
[SwaggerOperation(Summary = "Get Role by Id")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetRoleById([FromRoute] int id)
|
|
{
|
|
try
|
|
{
|
|
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("rolename/{name}", Name = "GetRoleByName")]
|
|
[SwaggerOperation(Summary = "Get Role by Name")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> GetRoleByName([FromRoute] string name)
|
|
{
|
|
try
|
|
{
|
|
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("roleId/{id}", Name = "UpdateRole")]
|
|
[SwaggerOperation(Summary = "Update Role")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> UpdateRole([FromRoute] int id,[FromBody] UpdatingRoleDto updatingRoleDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
var updated = await _roleService.UpdateRoleAsync(updatingRoleDto);
|
|
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("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)
|
|
{
|
|
try
|
|
{
|
|
var deleted = await _roleService.DeleteRoleAsync(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);
|
|
}
|
|
}
|
|
}
|
|
}
|