49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Mws.Application.Permissions;
|
||
|
|
using Mws.Application.Roles;
|
||
|
|
|
||
|
|
namespace Mws.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/roles")]
|
||
|
|
[Authorize]
|
||
|
|
public class RolesController(IRoleService roleService, IPermissionService permissions) : ControllerBase
|
||
|
|
{
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<ActionResult<List<RoleDto>>> GetAll(CancellationToken ct)
|
||
|
|
{
|
||
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.View, ct);
|
||
|
|
return Ok(await roleService.GetRolesAsync(ct));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("{id:guid}")]
|
||
|
|
public async Task<ActionResult<RoleDto>> Get(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.View, ct);
|
||
|
|
return Ok(await roleService.GetRoleAsync(id, ct));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<ActionResult<RoleDto>> Create([FromBody] SaveRoleRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Create, ct);
|
||
|
|
return Ok(await roleService.CreateRoleAsync(request, ct));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPut("{id:guid}")]
|
||
|
|
public async Task<ActionResult<RoleDto>> Update(Guid id, [FromBody] SaveRoleRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Edit, ct);
|
||
|
|
return Ok(await roleService.UpdateRoleAsync(id, request, ct));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Delete, ct);
|
||
|
|
await roleService.DeleteRoleAsync(id, ct);
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
}
|