48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Route;
|
|
|
|
[ApiController]
|
|
[Route("api/routes")]
|
|
[Authorize]
|
|
public class RouteController(RouteService service) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize(Policy = "routes.view")]
|
|
public async Task<IActionResult> GetAll() =>
|
|
Ok(await service.GetAllAsync());
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize(Policy = "routes.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize(Policy = "routes.create")]
|
|
public async Task<IActionResult> Create(CreateRouteRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize(Policy = "routes.update")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateRouteRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize(Policy = "routes.delete")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
}
|