56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Order;
|
|
|
|
[ApiController]
|
|
[Route("api/orders")]
|
|
[Authorize]
|
|
public class OrderController(OrderService service) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize(Policy = "orders.view")]
|
|
public async Task<IActionResult> GetAll() =>
|
|
Ok(await service.GetAllAsync());
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize(Policy = "orders.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("store/{storeId:guid}")]
|
|
[Authorize(Policy = "orders.view")]
|
|
public async Task<IActionResult> GetByStore(Guid storeId)
|
|
{
|
|
var result = await service.GetByStoreIdAsync(storeId);
|
|
return result is null ? Forbid() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize(Policy = "orders.create")]
|
|
public async Task<IActionResult> Create(CreateOrderRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return result is null ? Forbid() : CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize(Policy = "orders.update")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateOrderRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize(Policy = "orders.delete")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
}
|