58 lines
2.3 KiB
C#
58 lines
2.3 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.SupplierClaims;
|
|
|
|
[ApiController]
|
|
public class SupplierClaimController(SupplierClaimService service, ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet("api/supplier-claims/{id:guid}")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("api/purchase-orders/{id:guid}/claims")]
|
|
public async Task<IActionResult> GetByPurchaseOrder(Guid id) =>
|
|
Ok(await service.GetByPurchaseOrderIdAsync(id));
|
|
|
|
[HttpPost("api/purchase-orders/{id:guid}/claims")]
|
|
public async Task<IActionResult> Create(Guid id, CreateSupplierClaimRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.CreateAsync(id, request, userId, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("api/supplier-claims/{id:guid}/approve")]
|
|
public async Task<IActionResult> Approve(Guid id, ApproveSupplierClaimRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.ApproveAsync(id, request, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("api/supplier-claims/{id:guid}/reject")]
|
|
public async Task<IActionResult> Reject(Guid id, RejectSupplierClaimRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.RejectAsync(id, request, userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
|
|
}
|
|
}
|