diff --git a/PyroFetes/Endpoints/DeliveryNotes/UpdateDeliveryNoteEndpoint.cs b/PyroFetes/Endpoints/DeliveryNotes/UpdateDeliveryNoteEndpoint.cs new file mode 100644 index 00000000..5d347cb8 --- /dev/null +++ b/PyroFetes/Endpoints/DeliveryNotes/UpdateDeliveryNoteEndpoint.cs @@ -0,0 +1,40 @@ +using FastEndpoints; +using PyroFetes.DTO.DeliveryNote.Request; +using PyroFetes.DTO.DeliveryNote.Response; +using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.DeliveryNotes; + +namespace PyroFetes.Endpoints.DeliveryNotes; + +public class UpdateDeliveryNoteEndpoint( + DeliveryNotesRepository deliveryNotesRepository, + AutoMapper.IMapper mapper) : Endpoint +{ + public override void Configure() + { + Put("/deliveryNotes/{@Id}", x => new {x.Id}); + AllowAnonymous(); + } + + public override async Task HandleAsync(UpdateDeliveryNoteDto req, CancellationToken ct) + { + DeliveryNote? deliveryNote = await deliveryNotesRepository.FirstOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct); + + if (deliveryNote == null) + { + await Send.NotFoundAsync(ct); + return; + } + + deliveryNote.TrackingNumber = req.TrackingNumber; + deliveryNote.EstimateDeliveryDate = req.EstimateDeliveryDate; + deliveryNote.ExpeditionDate = req.ExpeditionDate; + deliveryNote.RealDeliveryDate = req.RealDeliveryDate; + deliveryNote.DelivererId = req.DelivererId; + + await deliveryNotesRepository.UpdateAsync(deliveryNote, ct); + + await Send.OkAsync(mapper.Map(deliveryNote), ct); + } +} \ No newline at end of file