83 lines
2.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using UserManagement.Application.Dtos.Incomming;
using UserManagement.Application.Dtos.Outgoing;
using UserManagement.Application.Interfaces;
namespace UserManagement.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserRolesController : Controller
{
// CTOR
private readonly IUserRolesService _userRolesService;
public UserRolesController(IUserRolesService userRolesService)
{
_userRolesService = userRolesService;
}
// CREATE ASSIGNMENT
[HttpPost("AssignRoleToUser")]
[SwaggerOperation(Summary = "Assign Role to User the better way")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> CreateAssignmentAsync(CreatingUserRolesDto creatingUserRolesDto)
{
try
{
var result = await _userRolesService.CreateAssignmentAsync(creatingUserRolesDto);
if (result)
{
return Ok("Rolle erfolgreich zugewiesen!");
}
else
{
return BadRequest("Zuweisen der Rolle fehlgeschlagen!");
}
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
}
// REMOVE ROLE FROM USER
[HttpDelete("RemoveRoleFromUser")]
[SwaggerOperation(Summary = "Remove Role from User")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> RemoveRoleFromUser(CreatingUserRolesDto creatingUserRolesDto)
{
try
{
var result = await _userRolesService.RemoveRoleFromUserAsync(creatingUserRolesDto);
if (result)
{
return Ok("Rolle erfolgreich entfernt!");
}
else
{
return BadRequest("Entfernung der Rolle fehlgeschlagen!");
}
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
}
}
}