refactor mapping profiles

This commit is contained in:
Collin M. Barrett 2018-08-11 20:17:50 -05:00
parent 7d2b586ffb
commit 4eb9b1273e
5 changed files with 99 additions and 73 deletions

View file

@ -1,73 +0,0 @@
using System;
using System.Linq;
using AutoMapper;
using FilterLists.Data.Entities;
using FilterLists.Services.FilterList.Models;
using JetBrains.Annotations;
namespace FilterLists.Services.FilterList
{
[UsedImplicitly]
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateListSummaryDtoMap();
CreateListDetailsDtoMap();
CreateListMaintainerDtoMap();
CreateListSyntaxDtoMap();
}
private void CreateListSummaryDtoMap() =>
CreateMap<Data.Entities.FilterList, ListSummaryDto>()
.ForMember(dto => dto.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc))
.ForMember(dto => dto.Languages,
conf => conf.MapFrom(list => list.FilterListLanguages.Select(listLangs => listLangs.Language)))
.ForMember(dto => dto.UpdatedDate,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s =>
s.IsCompleted && s.HttpStatusCode == "200" &&
(s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0))
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
private void CreateListDetailsDtoMap() =>
CreateMap<Data.Entities.FilterList, ListDetailsDto>()
.ForMember(dto => dto.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc))
.ForMember(dto => dto.Languages,
conf => conf.MapFrom(list => list.FilterListLanguages.Select(listLangs => listLangs.Language.Name)))
.ForMember(dto => dto.Maintainers,
conf => conf.MapFrom(list =>
list.FilterListMaintainers.Select(listMaints => listMaints.Maintainer)))
.ForMember(dto => dto.RuleCount,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s => s.IsCompleted && s.HttpStatusCode == "200")
.SelectMany(sr => sr.AddedSnapshotRules)
.Count() -
list.Snapshots.Where(s => s.IsCompleted && s.HttpStatusCode == "200")
.SelectMany(sr => sr.RemovedSnapshotRules)
.Count()))
.ForMember(dto => dto.UpdatedDate,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s =>
s.IsCompleted && s.HttpStatusCode == "200" &&
(s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0))
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
private void CreateListMaintainerDtoMap() =>
CreateMap<Maintainer, ListMaintainerDto>()
.ForMember(dto => dto.AdditionalLists,
conf => conf.MapFrom(maint =>
maint.FilterListMaintainers.Select(listMaints => listMaints.FilterList)));
private void CreateListSyntaxDtoMap() =>
CreateMap<Syntax, ListSyntaxDto>()
.ForMember(dto => dto.SupportedSoftware,
conf => conf.MapFrom(syntax => syntax.SoftwareSyntaxes.Select(softSyn => softSyn.Software)));
}
}

View file

@ -0,0 +1,37 @@
using System;
using System.Linq;
using AutoMapper;
using FilterLists.Services.FilterList.Models;
using JetBrains.Annotations;
namespace FilterLists.Services.FilterList.MappingProfiles
{
[UsedImplicitly]
public class ListDetailsDtoMappingProfile : Profile
{
public ListDetailsDtoMappingProfile() =>
CreateMap<Data.Entities.FilterList, ListDetailsDto>()
.ForMember(dto => dto.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc))
.ForMember(dto => dto.Languages,
conf => conf.MapFrom(list => list.FilterListLanguages.Select(listLangs => listLangs.Language.Name)))
.ForMember(dto => dto.Maintainers,
conf => conf.MapFrom(list =>
list.FilterListMaintainers.Select(listMaints => listMaints.Maintainer)))
.ForMember(dto => dto.RuleCount,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s => s.IsCompleted && s.HttpStatusCode == "200")
.SelectMany(sr => sr.AddedSnapshotRules)
.Count() - list.Snapshots.Where(s => s.IsCompleted && s.HttpStatusCode == "200")
.SelectMany(sr => sr.RemovedSnapshotRules)
.Count()))
.ForMember(dto => dto.UpdatedDate,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s =>
s.IsCompleted && s.HttpStatusCode == "200" &&
(s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0))
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
}
}

View file

@ -0,0 +1,18 @@
using System.Linq;
using AutoMapper;
using FilterLists.Data.Entities;
using FilterLists.Services.FilterList.Models;
using JetBrains.Annotations;
namespace FilterLists.Services.FilterList.MappingProfiles
{
[UsedImplicitly]
public class ListMaintainerDtoMappingProfile : Profile
{
public ListMaintainerDtoMappingProfile() =>
CreateMap<Maintainer, ListMaintainerDto>()
.ForMember(dto => dto.AdditionalLists,
conf => conf.MapFrom(maint =>
maint.FilterListMaintainers.Select(listMaints => listMaints.FilterList)));
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Linq;
using AutoMapper;
using FilterLists.Services.FilterList.Models;
using JetBrains.Annotations;
namespace FilterLists.Services.FilterList.MappingProfiles
{
[UsedImplicitly]
public class ListSummaryDtoMappingProfile : Profile
{
public ListSummaryDtoMappingProfile() =>
CreateMap<Data.Entities.FilterList, ListSummaryDto>()
.ForMember(dto => dto.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc))
.ForMember(dto => dto.Languages,
conf => conf.MapFrom(list => list.FilterListLanguages.Select(listLangs => listLangs.Language)))
.ForMember(dto => dto.UpdatedDate,
conf => conf.MapFrom(list =>
list.Snapshots.Where(s =>
s.IsCompleted && s.HttpStatusCode == "200" &&
(s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0))
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
}
}

View file

@ -0,0 +1,17 @@
using System.Linq;
using AutoMapper;
using FilterLists.Data.Entities;
using FilterLists.Services.FilterList.Models;
using JetBrains.Annotations;
namespace FilterLists.Services.FilterList.MappingProfiles
{
[UsedImplicitly]
public class ListSyntaxDtoMappingProfile : Profile
{
public ListSyntaxDtoMappingProfile() =>
CreateMap<Syntax, ListSyntaxDto>()
.ForMember(dto => dto.SupportedSoftware,
conf => conf.MapFrom(syntax => syntax.SoftwareSyntaxes.Select(softSyn => softSyn.Software)));
}
}