From 91b31150734ad9b44c9d39f9230c50dff8c55cec Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Sun, 4 May 2025 00:55:28 +0300 Subject: [PATCH] Add product AutoMapper profile --- .../Mapping/ProductMappingProfile.cs | 74 +++++++++++++++++++ .../Printbase.Infrastructure.csproj | 8 ++ 2 files changed, 82 insertions(+) create mode 100644 src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs diff --git a/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs b/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs new file mode 100644 index 0000000..3b521af --- /dev/null +++ b/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs @@ -0,0 +1,74 @@ +using AutoMapper; +using Printbase.Domain.Entities.Products; +using Printbase.Infrastructure.DbEntities.Products; + +namespace Printbase.Infrastructure.Mapping; + +public class ProductMappingProfile : Profile +{ + public ProductMappingProfile() + { + CreateMap() + .ForMember(dest => dest.Variants, opt => opt.MapFrom(src => src.Variants)) + .ForMember(dest => dest.Type, opt => opt.Ignore()); + + CreateMap() + .ConstructUsing((src, _) => new Product( + src.Id, + src.Name, + src.TypeId, + src.Description, + src.Discount)) + .ForMember(dest => dest.Variants, opt => opt.Ignore()) + .AfterMap((src, dest, ctx) => + { + foreach (var dbVariant in src.Variants) + { + var variant = ctx.Mapper.Map(dbVariant); + dest.AddVariant(variant); + } + }); + + CreateMap() + .ForMember(dest => dest.Product, opt => opt.Ignore()); + + CreateMap() + .ConstructUsing((src, _) => new ProductVariant( + src.Id, + src.ProductId, + src.Price, + src.Color, + src.Size, + src.Discount, + src.Stock)); + + CreateMap() + .ForMember(dest => dest.Group, opt => opt.Ignore()) + .ForMember(dest => dest.Products, opt => opt.Ignore()); + + CreateMap() + .ConstructUsing((src, _) => new ProductType( + src.Id, + src.Name, + src.GroupId, + src.Description)); + + CreateMap() + .ForMember(dest => dest.Types, opt => opt.MapFrom(src => src.Types)); + + CreateMap() + .ConstructUsing((src, _) => new ProductGroup( + src.Id, + src.Name, + src.Description)) + .ForMember(dest => dest.Types, opt => opt.Ignore()) + .AfterMap((src, dest, ctx) => + { + foreach (var dbType in src.Types) + { + var type = ctx.Mapper.Map(dbType); + dest.AddType(type); + } + }); + } +} \ No newline at end of file diff --git a/src/Printbase.Infrastructure/Printbase.Infrastructure.csproj b/src/Printbase.Infrastructure/Printbase.Infrastructure.csproj index 17b910f..8baa33a 100644 --- a/src/Printbase.Infrastructure/Printbase.Infrastructure.csproj +++ b/src/Printbase.Infrastructure/Printbase.Infrastructure.csproj @@ -6,4 +6,12 @@ enable + + + + + + + +