From 3616b42ef3a6be8d0a43127986146432432cfe55 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sun, 12 Aug 2018 08:01:28 -0500 Subject: [PATCH] refactor some mapping profiles --- .../ListDetailsDtoMappingProfile.cs | 43 +++++++++---------- .../ListMaintainerDtoMappingProfile.cs | 5 +-- .../ListSummaryDtoMappingProfile.cs | 28 ++++++------ .../ListSyntaxDtoMappingProfile.cs | 3 +- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/FilterLists.Services/FilterList/MappingProfiles/ListDetailsDtoMappingProfile.cs b/src/FilterLists.Services/FilterList/MappingProfiles/ListDetailsDtoMappingProfile.cs index b6ad13e77..6c8ca4271 100644 --- a/src/FilterLists.Services/FilterList/MappingProfiles/ListDetailsDtoMappingProfile.cs +++ b/src/FilterLists.Services/FilterList/MappingProfiles/ListDetailsDtoMappingProfile.cs @@ -9,29 +9,28 @@ namespace FilterLists.Services.FilterList.MappingProfiles [UsedImplicitly] public class ListDetailsDtoMappingProfile : Profile { + private readonly Func isDiffSnapshot = + s => s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0; + + private readonly Func isSuccessfulSnapshot = + s => s.IsCompleted && s.HttpStatusCode == "200"; + public ListDetailsDtoMappingProfile() => CreateMap() - .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() - .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() + .FirstOrDefault())); } } \ No newline at end of file diff --git a/src/FilterLists.Services/FilterList/MappingProfiles/ListMaintainerDtoMappingProfile.cs b/src/FilterLists.Services/FilterList/MappingProfiles/ListMaintainerDtoMappingProfile.cs index 612cfb3d3..a54f3eec3 100644 --- a/src/FilterLists.Services/FilterList/MappingProfiles/ListMaintainerDtoMappingProfile.cs +++ b/src/FilterLists.Services/FilterList/MappingProfiles/ListMaintainerDtoMappingProfile.cs @@ -11,8 +11,7 @@ public class ListMaintainerDtoMappingProfile : Profile { public ListMaintainerDtoMappingProfile() => CreateMap() - .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))); } } \ No newline at end of file diff --git a/src/FilterLists.Services/FilterList/MappingProfiles/ListSummaryDtoMappingProfile.cs b/src/FilterLists.Services/FilterList/MappingProfiles/ListSummaryDtoMappingProfile.cs index 53867243c..4dbad8f46 100644 --- a/src/FilterLists.Services/FilterList/MappingProfiles/ListSummaryDtoMappingProfile.cs +++ b/src/FilterLists.Services/FilterList/MappingProfiles/ListSummaryDtoMappingProfile.cs @@ -9,19 +9,23 @@ namespace FilterLists.Services.FilterList.MappingProfiles [UsedImplicitly] public class ListSummaryDtoMappingProfile : Profile { + private readonly Func isDiffSnapshot = + s => s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0; + + private readonly Func isSuccessfulSnapshot = + s => s.IsCompleted && s.HttpStatusCode == "200"; + public ListSummaryDtoMappingProfile() => CreateMap() - .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() - .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() + .FirstOrDefault())); } } \ No newline at end of file diff --git a/src/FilterLists.Services/FilterList/MappingProfiles/ListSyntaxDtoMappingProfile.cs b/src/FilterLists.Services/FilterList/MappingProfiles/ListSyntaxDtoMappingProfile.cs index a2a7f9fd2..084058287 100644 --- a/src/FilterLists.Services/FilterList/MappingProfiles/ListSyntaxDtoMappingProfile.cs +++ b/src/FilterLists.Services/FilterList/MappingProfiles/ListSyntaxDtoMappingProfile.cs @@ -11,7 +11,6 @@ public class ListSyntaxDtoMappingProfile : Profile { public ListSyntaxDtoMappingProfile() => CreateMap() - .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))); } } \ No newline at end of file