using AutoMapper; using PowderCoating.Core.Entities; using PowderCoating.Application.DTOs.Company; namespace PowderCoating.Application.Mappings; public class FormulaLibraryProfile : Profile { public FormulaLibraryProfile() { CreateMap() .ForMember(dest => dest.InspiredByName, opt => opt.MapFrom(src => src.InspiredBy != null ? src.InspiredBy.Name : null)) .ForMember(dest => dest.InspiredByCompanyName, opt => opt.MapFrom(src => src.InspiredBy != null ? src.InspiredBy.SourceCompanyName : null)) .ForMember(dest => dest.AlreadyImported, opt => opt.Ignore()); // set by service CreateMap() .IncludeBase() .ForMember(dest => dest.FieldCount, opt => opt.MapFrom(src => CountFields(src.FieldsJson))); } private static int CountFields(string fieldsJson) { try { var doc = System.Text.Json.JsonDocument.Parse(fieldsJson); return doc.RootElement.ValueKind == System.Text.Json.JsonValueKind.Array ? doc.RootElement.GetArrayLength() : 0; } catch { return 0; } } }