From c6d4ef2c580e70fcd734f57f7be816b0bfa28489 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 13 Nov 2025 15:26:22 +0100 Subject: [PATCH] Adapted Deliverer endpoints with repository spec and automapper --- .../Endpoints/Deliverers/CreateDelivererEndpoint.cs | 7 +++---- .../Endpoints/Deliverers/DeleteDelivererEndpoint.cs | 11 +++++------ .../Endpoints/Deliverers/GetAllDelivererEndpoint.cs | 11 +++-------- .../Endpoints/Deliverers/GetDelivererEndpoint.cs | 6 ++++-- .../Endpoints/Deliverers/UpdateDelivererEndpoint.cs | 8 +++++--- PyroFetes/Repositories/DeliverersRepository.cs | 5 +++++ .../Deliverers/GetDelivererByIdSpec.cs | 13 +++++++++++++ 7 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 PyroFetes/Repositories/DeliverersRepository.cs create mode 100644 PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs diff --git a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs index c8e0e58..d7f67ff 100644 --- a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs @@ -2,11 +2,12 @@ using FastEndpoints; using PyroFetes.DTO.Deliverer.Request; using PyroFetes.DTO.Deliverer.Response; using PyroFetes.Models; +using PyroFetes.Repositories; namespace PyroFetes.Endpoints.Deliverers; public class CreateDelivererEndpoint( - PyroFetesDbContext database, + DeliverersRepository deliverersRepository, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() @@ -23,9 +24,7 @@ public class CreateDelivererEndpoint( Transporter = req.Transporter, }; - database.Deliverers.Add(newDeliverer); - - await database.SaveChangesAsync(ct); + await deliverersRepository.AddAsync(newDeliverer, ct); await Send.OkAsync(mapper.Map(newDeliverer), ct); } diff --git a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs index 2c8e4a8..363e41f 100644 --- a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -1,6 +1,7 @@ using FastEndpoints; -using Microsoft.EntityFrameworkCore; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Deliverers; namespace PyroFetes.Endpoints.Deliverers; @@ -8,7 +9,7 @@ public class DeleteDelivererRequest { public int DelivererId { get; set; } } -public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint +public class DeleteDelivererEndpoint(DeliverersRepository deliverersRepository) : Endpoint { public override void Configure() { @@ -19,7 +20,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpointx.Id == req.DelivererId, ct); + Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct); if (deliverer == null) { @@ -27,9 +28,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint> +public class GetAllDelivererEndpoint(DeliverersRepository deliverersRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -16,13 +17,7 @@ public class GetAllDelivererEndpoint(PyroFetesDbContext database) : EndpointWith public override async Task HandleAsync(CancellationToken ct) { - List deliverers = await database.Deliverers.Select(x => new GetDelivererDto() - { - Id = x.Id, - Transporter = x.Transporter, - }).ToListAsync(ct); - - await Send.OkAsync(deliverers, ct); + await Send.OkAsync(await deliverersRepository.ProjectToListAsync(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs index b5cfe90..2cbd731 100644 --- a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs @@ -2,6 +2,8 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Deliverer.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Deliverers; namespace PyroFetes.Endpoints.Deliverers; @@ -11,7 +13,7 @@ public class GetDelivererRequest } public class GetDelivererEndpoint( - PyroFetesDbContext database, + DeliverersRepository deliverersRepository, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() @@ -23,7 +25,7 @@ public class GetDelivererEndpoint( public override async Task HandleAsync(GetDelivererRequest req, CancellationToken ct) { - Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.DelivererId, ct); + Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct); if (deliverer == null) { diff --git a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs index f962ef0..33df25b 100644 --- a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs @@ -3,11 +3,13 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Deliverer.Request; using PyroFetes.DTO.Deliverer.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Deliverers; namespace PyroFetes.Endpoints.Deliverers; public class UpdateDelivererEndpoint( - PyroFetesDbContext database, + DeliverersRepository deliverersRepository, AutoMapper.IMapper mapper) : Endpoint { public override void Configure() @@ -19,7 +21,7 @@ public class UpdateDelivererEndpoint( public override async Task HandleAsync(UpdateDelivererDto req, CancellationToken ct) { - Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.Id, ct); + Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.Id), ct); if (deliverer == null) { @@ -29,7 +31,7 @@ public class UpdateDelivererEndpoint( deliverer.Transporter = req.Transporter; - await database.SaveChangesAsync(ct); + await deliverersRepository.UpdateAsync(deliverer,ct); await Send.OkAsync(mapper.Map(deliverer), ct); } diff --git a/PyroFetes/Repositories/DeliverersRepository.cs b/PyroFetes/Repositories/DeliverersRepository.cs new file mode 100644 index 0000000..5f2af84 --- /dev/null +++ b/PyroFetes/Repositories/DeliverersRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class DeliverersRepository(PyroFetesDbContext lmdContext, AutoMapper.IMapper mapper) : PyrofetesRepository(lmdContext, mapper); diff --git a/PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs b/PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs new file mode 100644 index 0000000..6376381 --- /dev/null +++ b/PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Deliverers; + +public sealed class GetDelivererByIdSpec : Specification +{ + public GetDelivererByIdSpec(int delivererId) + { + Query + .Where(x => x.Id == delivererId); + } +} \ No newline at end of file