156 lines
5.2 KiB
C#
156 lines
5.2 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;
|
|
public RoleController(IRoleService roleService)
|
|
{
|
|
_roleService = roleService;
|
|
}
|
|
|
|
// CREATE
|
|
[HttpPost]
|
|
[SwaggerOperation(Summary = "Create Role")]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateRole([FromBody] CreatingRoleDto creatingRoleDto)
|
|
{
|
|
// Validate incomming model
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
// Try to add role asynchronously
|
|
var result = await _roleService.AddRoleAsync(creatingRoleDto);
|
|
|
|
// If role is successfully created, return a CreatedAtAction response with the created resource
|
|
if (result 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);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("geht nix");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
// READ ALL
|
|
[HttpGet]
|
|
[SwaggerOperation(Summary = "Get all Roles")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetRoles()
|
|
{
|
|
var roles = await _roleService.GetAllAsync();
|
|
return Ok(roles);
|
|
}
|
|
|
|
// READ BY ID
|
|
[HttpGet("id/{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)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid Id");
|
|
}
|
|
var role = await _roleService.GetByIdAsync(id);
|
|
if (role == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(role);
|
|
}
|
|
|
|
// READ BY NAME
|
|
[HttpGet("name/{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)
|
|
{
|
|
if (string.IsNullOrEmpty(name))
|
|
{
|
|
return BadRequest("Name cannot be empty");
|
|
}
|
|
var role = await _roleService.GetByNameAsync(name);
|
|
if (role == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(role);
|
|
}
|
|
|
|
// UPDATE
|
|
[HttpPut("id/{id}", Name = "UpdateRole")]
|
|
[SwaggerOperation(Summary = "Update Role")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> UpdateRole(int id, UpdatingRoleDto updatingRoleDto)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid Id");
|
|
}
|
|
|
|
var updated = await _roleService.UpdateRoleAsync(updatingRoleDto);
|
|
|
|
if (!updated)
|
|
{
|
|
return BadRequest("Update failed");
|
|
}
|
|
|
|
return Ok(updated);
|
|
}
|
|
|
|
// DELETE
|
|
[HttpDelete("id/{id}", Name = "DeleteRole")]
|
|
[SwaggerOperation(Summary = "Delete Role")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> DeleteRole([FromRoute] int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid Id");
|
|
}
|
|
|
|
var deleted = await _roleService.DeleteRoleAsync(id);
|
|
|
|
if (!deleted)
|
|
{
|
|
return BadRequest("Deletion failed");
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|