using System.Collections.Generic; using System.Linq; using AutoMapper; using LeafWeb.Core.Entities; using LeafWeb.Core.Utility; using LeafWeb.WebCms.Models; namespace LeafWeb.WebCms.App_Start { public static class AutoMapperConfig { public static void RegisterMappings() { Mapper.CreateMap() .ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore()); Mapper.CreateMap() .ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore()); // LeafInputDetails Mapper.CreateMap().ConvertUsing(file => file?.Contents.GetString()); Mapper.CreateMap().ConvertUsing(file => file?.FileContents.Contents.GetString()); Mapper.CreateMap().ConvertUsing(st => st.ToString()); Mapper.CreateMap().ConvertUsing(st => new LeafInputStatus()); Mapper.CreateMap() .Include() .ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id)) .ForMember(dest => dest.HasLeafChart, opt => opt.ResolveUsing(src => src.OutputFiles.Any(o => o.IsLeafChartFile))); Mapper.CreateMap(); // LeafInputData Mapper.CreateMap() .ForMember(dest => dest.Filename, opt => opt.MapFrom(src => src.LeafOutputFile.Filename)); //.ForMember(dest => dest.Filename, opt => opt.ResolveUsing(src => src.LeafOutputFile.Filename)); // LeafInputDataCurveViewModel Mapper.CreateMap(); // LeafInputDataSite Mapper.CreateMap(); // LeafInputStatus Mapper.CreateMap() .ForMember(dest => dest.Status, opt => opt.MapFrom(li => li.Status.ToString())); // VERY WEIRD. This next part "fixes" some map configuration issue with LeafInputDataCurve //AutoMapper.AutoMapperMappingException {"Missing type map configuration or unsupported mapping. // //Mapping types: //HashSet`1 -> LeafInputData //System.Collections.Generic.HashSet`1[[LeafWeb.Core.Entities.LeafInputDataCurve, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> LeafWeb.Core.Entities.LeafInputData // //Destination path: //LeafInputDetails.LeafInputData.LeafInputData.LeafInputData0[0] // //Source value: //System.Collections.Generic.HashSet`1[LeafWeb.Core.Entities.LeafInputDataCurve]"} Mapper.CreateMap, LeafInputData>() .ForAllMembers(opt => opt.Ignore()); Mapper.CreateMap() .ForMember(dest => dest.SiteId, opt => opt.MapFrom(lid => lid.Site.SiteId)) .ForMember(dest => dest.SpeciesSampled, opt => opt.MapFrom(lid => lid.Site.SpeciesSampled)) .ForMember(dest => dest.Latitude, opt => opt.MapFrom(lid => lid.Site.Latitude)) .ForMember(dest => dest.Longitude, opt => opt.MapFrom(lid => lid.Site.Longitude)) // TODO: Change to range? .ForMember(dest => dest.CO2S, opt => opt.MapFrom(lid => lid.Data.Max(d => d.CO2S) - lid.Data.Min(d => d.CO2S))) .ForMember(dest => dest.PARi, opt => opt.MapFrom(lid => lid.Data.Max(d => d.PARi) - lid.Data.Min(d => d.PARi))) .ForMember(dest => dest.Tleaf, opt => opt.MapFrom(lid => lid.Data.Max(d => d.Tleaf) - lid.Data.Min(d => d.Tleaf))) .ForMember(dest => dest.PhiPS2, opt => opt.MapFrom(lid => lid.Data.Max(d => d.PhiPS2) - lid.Data.Min(d => d.PhiPS2))) ; } } }