Files
mws.backend.dotnet/mws.api/Controllers/RolesController.cs
T

50 lines
1.8 KiB
C#
Raw Normal View History

using MediatR;
2026-08-13 23:10:22 +07:00
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(ISender sender, IPermissionService permissions) : ControllerBase
2026-08-13 23:10:22 +07:00
{
[HttpGet]
public async Task<ActionResult<List<RoleDto>>> GetAll(CancellationToken ct)
{
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.View, ct);
return Ok(await sender.Send(new GetRolesQuery(), ct));
2026-08-13 23:10:22 +07:00
}
[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 sender.Send(new GetRoleQuery(id), ct));
2026-08-13 23:10:22 +07:00
}
[HttpPost]
public async Task<ActionResult<RoleDto>> Create([FromBody] SaveRoleRequest request, CancellationToken ct)
{
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Create, ct);
return Ok(await sender.Send(new CreateRoleCommand(request), ct));
2026-08-13 23:10:22 +07:00
}
[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 sender.Send(new UpdateRoleCommand(id, request), ct));
2026-08-13 23:10:22 +07:00
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
{
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Delete, ct);
await sender.Send(new DeleteRoleCommand(id), ct);
2026-08-13 23:10:22 +07:00
return NoContent();
}
}