using Distribution.Infra.Authorization; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Distribution.Features.Grn; [ApiController] [Authorize] public class GrnController(GrnService service, ICurrentUserService currentUser) : ControllerBase { [HttpGet("api/goods-receipts/{id:guid}")] public async Task GetById(Guid id) { var result = await service.GetByIdAsync(id); return result is null ? NotFound() : Ok(result); } [HttpGet("api/purchase-orders/{id:guid}/goods-receipts")] public async Task GetByPurchaseOrder(Guid id) => Ok(await service.GetByPurchaseOrderIdAsync(id)); [HttpPost("api/purchase-orders/{id:guid}/goods-receipts")] public async Task Create(Guid id, CreateGrnRequest 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 }); } } }