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 + + + + + + + +