From d9fac1f03c64a87fa2f78faec7ac2d4e8953fe9a Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Sat, 11 Aug 2018 17:09:24 -0500 Subject: [PATCH] move GetDetailsAsync() query logic to AutoMapper ref #331 --- .../FilterList/FilterListService.cs | 37 +++---------------- .../FilterList/MappingProfiles.cs | 21 ++++++++++- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/FilterLists.Services/FilterList/FilterListService.cs b/src/FilterLists.Services/FilterList/FilterListService.cs index b6025d9ad..e511be0a1 100644 --- a/src/FilterLists.Services/FilterList/FilterListService.cs +++ b/src/FilterLists.Services/FilterList/FilterListService.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using AutoMapper.QueryableExtensions; @@ -21,34 +19,9 @@ public FilterListService(FilterListsDbContext dbContext, IMapper mapper) : base( public async Task> GetAllSummariesAsync() => await DbContext.FilterLists.ProjectTo(Mapper.ConfigurationProvider).ToListAsync(); - public async Task GetDetailsAsync(uint id) - { - var details = await DbContext.FilterLists.ProjectTo(Mapper.ConfigurationProvider) - .FirstAsync(x => x.Id == id) - .FilterParentListFromMaintainerAdditionalLists(); - details.RuleCount = await GetActiveRuleCount(details); - details.UpdatedDate = await GetUpdatedDate(details); - return details; - } - - private async Task GetActiveRuleCount(ListDetailsDto details) - { - var listSnapshots = DbContext.Snapshots.AsNoTracking() - .Where(s => s.FilterListId == details.Id && s.IsCompleted); - return await listSnapshots.SelectMany(sr => sr.AddedSnapshotRules).CountAsync() - - await listSnapshots.SelectMany(sr => sr.RemovedSnapshotRules).CountAsync(); - } - - private async Task GetUpdatedDate(ListDetailsDto details) - { - var snapshotDates = DbContext.Snapshots.AsNoTracking() - .Where(s => s.FilterListId == details.Id && s.IsCompleted && - s.HttpStatusCode == "200" && - (s.AddedSnapshotRules.Count > 0 || - s.RemovedSnapshotRules.Count > 0)) - .Select(s => s.CreatedDateUtc) - .OrderByDescending(s => s.Date); - return snapshotDates.Any() ? (DateTime?)await snapshotDates.FirstAsync() : null; - } + public async Task GetDetailsAsync(uint id) => + await DbContext.FilterLists.ProjectTo(Mapper.ConfigurationProvider) + .FirstOrDefaultAsync(x => x.Id == id) + .FilterParentListFromMaintainerAdditionalLists(); } } \ No newline at end of file diff --git a/src/FilterLists.Services/FilterList/MappingProfiles.cs b/src/FilterLists.Services/FilterList/MappingProfiles.cs index 65822a577..3561a29f7 100644 --- a/src/FilterLists.Services/FilterList/MappingProfiles.cs +++ b/src/FilterLists.Services/FilterList/MappingProfiles.cs @@ -20,9 +20,9 @@ public MappingProfiles() private void CreateListSummaryDtoMap() => 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.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc)) .ForMember(dto => dto.UpdatedDate, conf => conf.MapFrom(list => list.Snapshots.Where(s => @@ -35,12 +35,29 @@ private void CreateListSummaryDtoMap() => private void CreateListDetailsDtoMap() => 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.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc)); + .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())); private void CreateListMaintainerDtoMap() => CreateMap()