diff --git a/PyroFetes/Endpoints/Suppliers/CreateSupplierEndpoint.cs b/PyroFetes/Endpoints/Suppliers/CreateSupplierEndpoint.cs index 8ec116f..19f674c 100644 --- a/PyroFetes/Endpoints/Suppliers/CreateSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Suppliers/CreateSupplierEndpoint.cs @@ -2,10 +2,13 @@ using PyroFetes.DTO.Supplier.Request; using PyroFetes.DTO.Supplier.Response; using PyroFetes.Models; +using PyroFetes.Repositories; namespace PyroFetes.Endpoints.Suppliers; -public class CreateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +public class CreateSupplierEndpoint( + SuppliersRepository suppliersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -26,20 +29,8 @@ public class CreateSupplierEndpoint(PyroFetesDbContext database) : Endpoint(supplier), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Suppliers/DeleteSupplierEndpoint.cs b/PyroFetes/Endpoints/Suppliers/DeleteSupplierEndpoint.cs index 2f3dd5a..4aaef71 100644 --- a/PyroFetes/Endpoints/Suppliers/DeleteSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Suppliers/DeleteSupplierEndpoint.cs @@ -1,6 +1,8 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Suppliers; namespace PyroFetes.Endpoints.Suppliers; @@ -9,7 +11,7 @@ public class DeleteSupplierRequest public int Id { get; set; } } -public class DeleteSupplierEndpoint(PyroFetesDbContext database) : Endpoint +public class DeleteSupplierEndpoint(SuppliersRepository suppliersRepository) : Endpoint { public override void Configure() { @@ -19,7 +21,7 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + Supplier? supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.Id), ct); if (supplier == null) { @@ -27,8 +29,7 @@ public class DeleteSupplierEndpoint(PyroFetesDbContext database) : Endpoint> +public class GetAllSuppliersEndpoint(SuppliersRepository suppliersRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -14,19 +15,6 @@ public class GetAllSuppliersEndpoint(PyroFetesDbContext database) : EndpointWith public override async Task HandleAsync(CancellationToken ct) { - List supplier = await database.Suppliers - .Select(supplier => new GetSupplierDto() - { - Id = supplier.Id, - Name = supplier.Name, - Email = supplier.Email, - Phone = supplier.Phone, - Address = supplier.Address, - City = supplier.City, - ZipCode = supplier.ZipCode, - DeliveryDelay = supplier.DeliveryDelay - }).ToListAsync(ct); - - await Send.OkAsync(supplier, ct); + await Send.OkAsync(await suppliersRepository.ProjectToListAsync(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Suppliers/GetSupplierEndpoint.cs b/PyroFetes/Endpoints/Suppliers/GetSupplierEndpoint.cs index 628969d..7c6377a 100644 --- a/PyroFetes/Endpoints/Suppliers/GetSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Suppliers/GetSupplierEndpoint.cs @@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Supplier.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Suppliers; namespace PyroFetes.Endpoints.Suppliers; @@ -10,7 +12,9 @@ public class GetSupplierRequest public int Id { get; set; } } -public class GetSupplierEndpoint(PyroFetesDbContext database) : Endpoint +public class GetSupplierEndpoint( + SuppliersRepository suppliersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -20,8 +24,7 @@ public class GetSupplierEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + Supplier? supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.Id), ct); if (supplier == null) { @@ -29,17 +32,6 @@ public class GetSupplierEndpoint(PyroFetesDbContext database) : Endpoint(supplier), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Suppliers/PatchSupplierDeleveryDelayEndpoint.cs b/PyroFetes/Endpoints/Suppliers/PatchSupplierDeleveryDelayEndpoint.cs index 85d4040..f4ece04 100644 --- a/PyroFetes/Endpoints/Suppliers/PatchSupplierDeleveryDelayEndpoint.cs +++ b/PyroFetes/Endpoints/Suppliers/PatchSupplierDeleveryDelayEndpoint.cs @@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Supplier.Request; using PyroFetes.DTO.Supplier.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Suppliers; namespace PyroFetes.Endpoints.Suppliers; -public class PatchSupplierDeleveryDelayEndpoint(PyroFetesDbContext database) : Endpoint +public class PatchSupplierDeleveryDelayEndpoint( + SuppliersRepository suppliersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,7 @@ public class PatchSupplierDeleveryDelayEndpoint(PyroFetesDbContext database) : E public override async Task HandleAsync(PatchSupplierDeliveryDelayDto req, CancellationToken ct) { - Supplier? supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + Supplier? supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.Id), ct); if (supplier == null) { @@ -25,14 +29,8 @@ public class PatchSupplierDeleveryDelayEndpoint(PyroFetesDbContext database) : E } supplier.DeliveryDelay = req.DeliveryDelay; - await database.SaveChangesAsync(ct); - - GetSupplierDto responseDto = new() - { - Id = supplier.Id, - DeliveryDelay = supplier.DeliveryDelay, - }; + await suppliersRepository.UpdateAsync(supplier, ct); - await Send.OkAsync(responseDto, ct); + await Send.OkAsync(mapper.Map(supplier), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Suppliers/UpdateSupplierEndpoint.cs b/PyroFetes/Endpoints/Suppliers/UpdateSupplierEndpoint.cs index e9c4ef4..0ef1516 100644 --- a/PyroFetes/Endpoints/Suppliers/UpdateSupplierEndpoint.cs +++ b/PyroFetes/Endpoints/Suppliers/UpdateSupplierEndpoint.cs @@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Supplier.Request; using PyroFetes.DTO.Supplier.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Suppliers; namespace PyroFetes.Endpoints.Suppliers; -public class UpdateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +public class UpdateSupplierEndpoint( + SuppliersRepository suppliersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,7 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + Supplier? supplier = await suppliersRepository.FirstOrDefaultAsync(new GetSupplierByIdSpec(req.Id), ct); if (supplier == null) { @@ -31,20 +35,9 @@ public class UpdateSupplierEndpoint(PyroFetesDbContext database) : Endpoint(supplier), ct); } } \ No newline at end of file