using Distribution.Models; using Distribution.Infra.Authorization; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Distribution.Features.LoadingEvent; [ApiController] [Route("api/loading-sheets")] [Authorize] public class LoadingSheetController(LoadingSheetService service, ICurrentUserService currentUser) : ControllerBase { [HttpGet] [Authorize(Policy = "loading.view")] public async Task GetAll( [FromQuery] Guid? rideId, [FromQuery] LoadingSheetStatus? status, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { var result = await service.GetAllAsync(rideId, status, page, pageSize); return Ok(result); } [HttpGet("{id:guid}")] [Authorize(Policy = "loading.view")] public async Task GetById(Guid id) { var result = await service.GetByIdAsync(id); return result is null ? NotFound() : Ok(result); } [HttpPost] [Authorize(Policy = "loading.dispatch")] public async Task Create(CreateLoadingSheetRequest request, CancellationToken ct) { try { var result = await service.CreateAsync(request, ct); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPut("{id:guid}/items")] [Authorize(Policy = "loading.dispatch")] public async Task SetItems(Guid id, SetItemsRequest request, CancellationToken ct) { try { var result = await service.SetItemsAsync(id, request, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/confirm-dispatch")] [Authorize(Policy = "loading.dispatch")] public async Task ConfirmDispatch(Guid id, CancellationToken ct) { var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); try { var result = await service.ConfirmDispatchAsync(id, userId, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPut("{id:guid}/receipt-items")] [Authorize(Policy = "loading.receive")] public async Task SetReceiptItems(Guid id, SetReceiptItemsRequest request, CancellationToken ct) { try { var result = await service.SetReceiptItemsAsync(id, request, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/confirm-receipt")] [Authorize(Policy = "loading.receive")] public async Task ConfirmReceipt(Guid id, ConfirmReceiptRequest request, CancellationToken ct) { var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); try { var result = await service.ConfirmReceiptAsync(id, request, userId, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/resolve")] [Authorize(Policy = "loading.resolve")] public async Task Resolve(Guid id, ResolveDiscrepancyRequest request, CancellationToken ct) { var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); try { var result = await service.ResolveAsync(id, request, userId, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } }