refactor some mapping profiles

This commit is contained in:
Collin M. Barrett 2018-08-12 08:01:28 -05:00
parent 2a2e99a237
commit 3616b42ef3
4 changed files with 40 additions and 39 deletions

View file

@ -9,29 +9,28 @@ namespace FilterLists.Services.FilterList.MappingProfiles
[UsedImplicitly]
public class ListDetailsDtoMappingProfile : Profile
{
private readonly Func<Data.Entities.Snapshot, bool> isDiffSnapshot =
s => s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0;
private readonly Func<Data.Entities.Snapshot, bool> isSuccessfulSnapshot =
s => s.IsCompleted && s.HttpStatusCode == "200";
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()));
.ForMember(d => d.AddedDate, c => c.MapFrom(l => l.CreatedDateUtc))
.ForMember(d => d.Languages, c => c.MapFrom(l => l.FilterListLanguages.Select(la => la.Language.Name)))
.ForMember(d => d.Maintainers, c => c.MapFrom(l => l.FilterListMaintainers.Select(m => m.Maintainer)))
.ForMember(d => d.RuleCount,
c => c.MapFrom(l =>
l.Snapshots.Where(isSuccessfulSnapshot).SelectMany(sr => sr.AddedSnapshotRules).Count() -
l.Snapshots.Where(isSuccessfulSnapshot).SelectMany(sr => sr.RemovedSnapshotRules).Count()))
.ForMember(d => d.UpdatedDate,
c => c.MapFrom(l =>
l.Snapshots.Where(isSuccessfulSnapshot)
.Where(isDiffSnapshot)
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
}
}

View file

@ -11,8 +11,7 @@ public class ListMaintainerDtoMappingProfile : Profile
{
public ListMaintainerDtoMappingProfile() =>
CreateMap<Maintainer, ListMaintainerDto>()
.ForMember(dto => dto.AdditionalLists,
conf => conf.MapFrom(maint =>
maint.FilterListMaintainers.Select(listMaints => listMaints.FilterList)));
.ForMember(d => d.AdditionalLists,
c => c.MapFrom(m => m.FilterListMaintainers.Select(lm => lm.FilterList)));
}
}

View file

@ -9,19 +9,23 @@ namespace FilterLists.Services.FilterList.MappingProfiles
[UsedImplicitly]
public class ListSummaryDtoMappingProfile : Profile
{
private readonly Func<Data.Entities.Snapshot, bool> isDiffSnapshot =
s => s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0;
private readonly Func<Data.Entities.Snapshot, bool> isSuccessfulSnapshot =
s => s.IsCompleted && s.HttpStatusCode == "200";
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()));
.ForMember(d => d.AddedDate, c => c.MapFrom(l => l.CreatedDateUtc))
.ForMember(d => d.Languages, c => c.MapFrom(l => l.FilterListLanguages.Select(la => la.Language)))
.ForMember(d => d.UpdatedDate,
c => c.MapFrom(l =>
l.Snapshots.Where(isSuccessfulSnapshot)
.Where(isDiffSnapshot)
.OrderByDescending(s => s.CreatedDateUtc)
.Select(s => s.CreatedDateUtc)
.Cast<DateTime?>()
.FirstOrDefault()));
}
}

View file

@ -11,7 +11,6 @@ public class ListSyntaxDtoMappingProfile : Profile
{
public ListSyntaxDtoMappingProfile() =>
CreateMap<Syntax, ListSyntaxDto>()
.ForMember(dto => dto.SupportedSoftware,
conf => conf.MapFrom(syntax => syntax.SoftwareSyntaxes.Select(softSyn => softSyn.Software)));
.ForMember(d => d.SupportedSoftware, c => c.MapFrom(s => s.SoftwareSyntaxes.Select(ss => ss.Software)));
}
}