From 9684dbcbc747f9b0b4eda1dc53dd05d251d9f2d1 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 13 Nov 2025 14:44:28 +0100 Subject: [PATCH] Merged AutoMapper branch --- .../Deliverers/CreateDelivererEndpoint.cs | 14 +- .../Deliverers/GetAllDelivererEndpoint.cs | 3 +- .../Deliverers/GetDelivererEndpoint.cs | 10 +- .../Deliverers/UpdateDelivererEndpoint.cs | 10 +- .../MappingProfiles/DtoToEntityMappings.cs | 68 ++++ .../MappingProfiles/EntityToDtoMappings.cs | 46 +++ PyroFetes/Program.cs | 16 + PyroFetes/PyroFetes.csproj | 5 +- PyroFetes/Repositories/PyrofetesRepository.cs | 328 ++++++++++++++++++ 9 files changed, 477 insertions(+), 23 deletions(-) create mode 100644 PyroFetes/MappingProfiles/DtoToEntityMappings.cs create mode 100644 PyroFetes/MappingProfiles/EntityToDtoMappings.cs create mode 100644 PyroFetes/Repositories/PyrofetesRepository.cs diff --git a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs index 6bbe746..c8e0e58 100644 --- a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs @@ -5,7 +5,9 @@ using PyroFetes.Models; namespace PyroFetes.Endpoints.Deliverers; -public class CreateDelivererEndpoint(PyroFetesDbContext database) : Endpoint +public class CreateDelivererEndpoint( + PyroFetesDbContext database, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -20,15 +22,11 @@ public class CreateDelivererEndpoint(PyroFetesDbContext database) : Endpoint(newDeliverer), ct); } } diff --git a/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs index 47cafc6..a0ce359 100644 --- a/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs @@ -1,8 +1,7 @@ +using AutoMapper.QueryableExtensions; using FastEndpoints; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Scaffolding.Metadata; using PyroFetes.DTO.Deliverer.Response; -using PyroFetes.Models; namespace PyroFetes.Endpoints.Deliverers; diff --git a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs index 35a4b69..b5cfe90 100644 --- a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs @@ -10,7 +10,9 @@ public class GetDelivererRequest public int DelivererId { get; set; } } -public class GetDelivererEndpoint(PyroFetesDbContext database) : Endpoint +public class GetDelivererEndpoint( + PyroFetesDbContext database, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -29,11 +31,7 @@ public class GetDelivererEndpoint(PyroFetesDbContext database) : Endpoint(deliverer), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs index b8c7a5e..f962ef0 100644 --- a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs @@ -6,7 +6,9 @@ using PyroFetes.Models; namespace PyroFetes.Endpoints.Deliverers; -public class UpdateDelivererEndpoint(PyroFetesDbContext database) : Endpoint +public class UpdateDelivererEndpoint( + PyroFetesDbContext database, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -29,11 +31,7 @@ public class UpdateDelivererEndpoint(PyroFetesDbContext database) : Endpoint(deliverer), ct); } } \ No newline at end of file diff --git a/PyroFetes/MappingProfiles/DtoToEntityMappings.cs b/PyroFetes/MappingProfiles/DtoToEntityMappings.cs new file mode 100644 index 0000000..d1a1db8 --- /dev/null +++ b/PyroFetes/MappingProfiles/DtoToEntityMappings.cs @@ -0,0 +1,68 @@ +using AutoMapper; +using PyroFetes.DTO.Deliverer.Request; +using PyroFetes.DTO.DeliveryNote.Request; +using PyroFetes.DTO.Price.Request; +using PyroFetes.DTO.Product.Request; +using PyroFetes.DTO.ProductDelivery.Request; +using PyroFetes.DTO.PurchaseOrder.Request; +using PyroFetes.DTO.PurchaseProduct.Request; +using PyroFetes.DTO.Quotation.Request; +using PyroFetes.DTO.QuotationProduct.Request; +using PyroFetes.DTO.SettingDTO.Request; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.User.Request; +using PyroFetes.DTO.WareHouseProduct.Request; +using PyroFetes.Models; + +namespace PyroFetes.MappingProfiles; + +public class DtoToEntityMappings : Profile +{ + public DtoToEntityMappings() + { + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + } +} \ No newline at end of file diff --git a/PyroFetes/MappingProfiles/EntityToDtoMappings.cs b/PyroFetes/MappingProfiles/EntityToDtoMappings.cs new file mode 100644 index 0000000..0fbbffe --- /dev/null +++ b/PyroFetes/MappingProfiles/EntityToDtoMappings.cs @@ -0,0 +1,46 @@ +using AutoMapper; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.DTO.DeliveryNote.Response; +using PyroFetes.DTO.Price.Response; +using PyroFetes.DTO.Product.Response; +using PyroFetes.DTO.ProductDelivery.Response; +using PyroFetes.DTO.PurchaseOrder.Response; +using PyroFetes.DTO.PurchaseProduct.Response; +using PyroFetes.DTO.Quotation.Response; +using PyroFetes.DTO.QuotationProduct.Response; +using PyroFetes.DTO.SettingDTO.Response; +using PyroFetes.DTO.User.Response; +using PyroFetes.DTO.WareHouseProduct.Response; +using PyroFetes.Models; + +namespace PyroFetes.MappingProfiles; + +public class EntityToDtoMappings : Profile +{ + public EntityToDtoMappings() + { + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + } +} \ No newline at end of file diff --git a/PyroFetes/Program.cs b/PyroFetes/Program.cs index e92c405..2b82940 100644 --- a/PyroFetes/Program.cs +++ b/PyroFetes/Program.cs @@ -1,7 +1,11 @@ +using AutoMapper; +using AutoMapper.EquivalencyExpression; using PyroFetes; using FastEndpoints; using FastEndpoints.Swagger; using FastEndpoints.Security; +using PyroFetes.MappingProfiles; +using IMapper = FastEndpoints.IMapper; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); @@ -15,6 +19,18 @@ builder.Services // On ajoute ici la configuration de la base de données builder.Services.AddDbContext(); + +MapperConfiguration mappingConfig = new(mc => +{ + mc.AddCollectionMappers(); + mc.AddProfile(new DtoToEntityMappings()); + mc.AddProfile(new EntityToDtoMappings()); +}, new LoggerFactory()); + + +AutoMapper.IMapper mapper = mappingConfig.CreateMapper(); +builder.Services.AddSingleton(mapper); + // On construit l'application en lui donnant vie WebApplication app = builder.Build(); app.UseAuthentication() diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index 10361d5..cd211f5 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,7 +7,9 @@ - + + + @@ -20,6 +22,7 @@ + diff --git a/PyroFetes/Repositories/PyrofetesRepository.cs b/PyroFetes/Repositories/PyrofetesRepository.cs new file mode 100644 index 0000000..0f09bd3 --- /dev/null +++ b/PyroFetes/Repositories/PyrofetesRepository.cs @@ -0,0 +1,328 @@ +using System.Linq.Expressions; +using Ardalis.Specification; +using Ardalis.Specification.EntityFrameworkCore; +using AutoMapper.QueryableExtensions; +using Microsoft.EntityFrameworkCore; +using Plainquire.Page; + +namespace PyroFetes.Repositories; + +public class PyrofetesRepository(DbContext databaseContext, AutoMapper.IMapper mapper) : RepositoryBase(databaseContext) where T : class +{ + private readonly DbContext _databaseContext = databaseContext; + + /// + /// + /// + /// + /// + public async Task FirstOrDefaultAsync(CancellationToken cancellationToken = default) + { + return await _databaseContext.Set().AsQueryable().FirstOrDefaultAsync(cancellationToken); + } + + /// + /// + /// + /// + /// + public async Task FirstAsync(CancellationToken cancellationToken = default) + { + return await _databaseContext.Set().AsQueryable().FirstAsync(cancellationToken); + } + + /// + /// + /// + /// + /// + /// + public async Task SingleAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification).SingleAsync(cancellationToken); + } + + /// + /// + /// + /// + /// + public async Task SingleAsync(CancellationToken cancellationToken = default) + { + return await _databaseContext.Set().AsQueryable().SingleAsync(cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + public async Task SumAsync( + ISpecification specification, + Expression> selector, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification).SumAsync(selector, cancellationToken); + } + + // /// + // /// + // /// + // /// + // /// + // /// + // /// + // public async Task SumAsync( + // ISpecification specification, + // Expression> selector, + // CancellationToken cancellationToken = default) + // { + // return await ApplySpecification(specification).SumAsync(selector, cancellationToken); + // } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task ProjectToSingleOrDefaultAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .SingleOrDefaultAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task ProjectToSingleOrDefaultAsync( + ISpecification specification, + Action postProcessor, + CancellationToken cancellationToken = default) + { + TResult? result = await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .SingleOrDefaultAsync(cancellationToken: cancellationToken); + postProcessor(result); + return result; + } + + // /// + // /// + // /// + // /// + // /// + // /// + // public async Task ProjectToSingleOrDefaultAsync( + // CancellationToken cancellationToken = default) + // { + // return await _databaseContext.Set().AsQueryable().ProjectTo(mapper.ConfigurationProvider).SingleOrDefaultAsync(cancellationToken: cancellationToken); + // } + + /// + /// + /// + /// + /// + /// + /// + public async Task ProjectToSingleAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .SingleAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task ProjectToSingleAsync( + ISpecification specification, + Action postProcessor, + CancellationToken cancellationToken = default) + { + TResult result = await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .SingleAsync(cancellationToken: cancellationToken); + postProcessor(result); + return result; + } + + /// + /// + /// + /// + /// + /// + public async Task ProjectToSingleAsync( + CancellationToken cancellationToken = default) + { + return await _databaseContext.Set() + .AsQueryable() + .ProjectTo(mapper.ConfigurationProvider) + .SingleAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + CancellationToken cancellationToken = default) + { + return await _databaseContext.Set() + .AsQueryable() + .ProjectTo(mapper.ConfigurationProvider) + .ToListAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + Action> postProcessor, + CancellationToken cancellationToken = default) + { + List results = await _databaseContext.Set() + .AsQueryable() + .ProjectTo(mapper.ConfigurationProvider) + .ToListAsync(cancellationToken: cancellationToken); + postProcessor(results); + return results; + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + ISpecification specification, + Action> postProcessor, + CancellationToken cancellationToken = default) + { + List results = await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .ToListAsync(cancellationToken: cancellationToken); + postProcessor(results); + return results; + } + + /// + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .ToListAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + ISpecification specification, + EntityPage page, + Action> postProcessor, + CancellationToken cancellationToken = default) + { + List results = await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .Page(page) + .ToListAsync(cancellationToken: cancellationToken); + postProcessor(results); + return results; + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> ProjectToListAsync( + ISpecification specification, + EntityPage page, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification) + .ProjectTo(mapper.ConfigurationProvider) + .Page(page) + .ToListAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> SelectManyAsync( + ISpecification specification, + Expression>> selector, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification).SelectMany(selector).ToListAsync(cancellationToken: cancellationToken); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> SelectAsync( + ISpecification specification, + Expression> selector, + CancellationToken cancellationToken = default) + { + return await ApplySpecification(specification).Select(selector).ToListAsync(cancellationToken: cancellationToken); + } +} \ No newline at end of file