move GetDetailsAsync() query logic to AutoMapper

ref #331
This commit is contained in:
Collin Barrett 2018-08-11 17:09:24 -05:00
parent e3f310c500
commit d9fac1f03c
2 changed files with 24 additions and 34 deletions

View file

@ -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<IEnumerable<ListSummaryDto>> GetAllSummariesAsync() =>
await DbContext.FilterLists.ProjectTo<ListSummaryDto>(Mapper.ConfigurationProvider).ToListAsync();
public async Task<ListDetailsDto> GetDetailsAsync(uint id)
{
var details = await DbContext.FilterLists.ProjectTo<ListDetailsDto>(Mapper.ConfigurationProvider)
.FirstAsync(x => x.Id == id)
.FilterParentListFromMaintainerAdditionalLists();
details.RuleCount = await GetActiveRuleCount(details);
details.UpdatedDate = await GetUpdatedDate(details);
return details;
}
private async Task<int> 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<DateTime?> 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<ListDetailsDto> GetDetailsAsync(uint id) =>
await DbContext.FilterLists.ProjectTo<ListDetailsDto>(Mapper.ConfigurationProvider)
.FirstOrDefaultAsync(x => x.Id == id)
.FilterParentListFromMaintainerAdditionalLists();
}
}

View file

@ -20,9 +20,9 @@ public MappingProfiles()
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.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<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.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<DateTime?>()
.FirstOrDefault()));
private void CreateListMaintainerDtoMap() =>
CreateMap<Maintainer, ListMaintainerDto>()