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 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 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 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 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 UpdateRole(int id, UpdatingRoleDto updatingRoleDto) { var updated = await _roleService.UpdateRoleAsync(updatingRoleDto); 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 DeleteRole([FromRoute] int id) { await _roleService.DeleteRoleAsync(id); return Ok(); } } }