Files
LeafWeb/WebCms/App_Start/AutoMapperConfig.cs

76 lines
4.1 KiB
C#

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<LeafInputCreate, LeafInput>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
Mapper.CreateMap<LeafInput, LeafInputCreate>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
// LeafInputDetails
Mapper.CreateMap<LeafInputFile, string>().ConvertUsing(file => file?.Contents.GetString());
Mapper.CreateMap<LeafOutputFile, string>().ConvertUsing(file => file?.FileContents.Contents.GetString());
Mapper.CreateMap<LeafInputStatusType, string>().ConvertUsing(st => st.ToString());
Mapper.CreateMap<LeafInputStatusType, LeafInputStatus>().ConvertUsing(st => new LeafInputStatus());
Mapper.CreateMap<LeafInput, LeafInputDetails>()
.Include<LeafInput, LeafInputDetails_Admin>()
.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<LeafInput, LeafInputDetails_Admin>();
// LeafInputData
Mapper.CreateMap<LeafInputData, LeafInputDataViewModel>()
.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<LeafInputDataCurve, LeafInputDataCurveViewModel>();
// LeafInputDataSite
Mapper.CreateMap<LeafInputDataSite, LeafInputDataSiteViewModel>();
// LeafInputStatus
Mapper.CreateMap<LeafInputStatus, LeafInputStatusViewModel>()
.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<ICollection<LeafInputDataCurve>, LeafInputData>()
.ForAllMembers(opt => opt.Ignore());
Mapper.CreateMap<LeafInputData, LeafDataResultViewModel>()
.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)))
;
}
}
}