improved GetAllSummariesAsync()

ref #330
This commit is contained in:
Collin Barrett 2018-08-11 16:02:21 -05:00
parent 233170685f
commit e3f310c500
2 changed files with 15 additions and 36 deletions

View file

@ -18,39 +18,8 @@ public FilterListService(FilterListsDbContext dbContext, IMapper mapper) : base(
{
}
public async Task<IEnumerable<ListSummaryDto>> GetAllSummariesAsync()
{
var summaries = await GetSummaryDtos();
var latestSnapshots = await GetLatestSnapshots();
return summaries.GroupJoin(latestSnapshots, summary => summary.Id, snap => snap.FilterListId,
(summary, snap) =>
{
snap = snap as Data.Entities.Snapshot[] ?? snap.ToArray();
var updatedDate = snap.Any() ? snap.Single().CreatedDateUtc : (DateTime?) null;
return new ListSummaryDto
{
Id = summary.Id,
AddedDate = summary.AddedDate,
Languages = summary.Languages,
Name = summary.Name,
UpdatedDate = updatedDate,
ViewUrl = summary.ViewUrl
};
});
}
private async Task<List<ListSummaryDto>> GetSummaryDtos() =>
await DbContext.FilterLists.OrderBy(l => l.Name)
.ProjectTo<ListSummaryDto>(Mapper.ConfigurationProvider)
.ToListAsync();
private async Task<List<Data.Entities.Snapshot>> GetLatestSnapshots() =>
await DbContext.Snapshots.AsNoTracking()
.Where(s => s.IsCompleted && s.HttpStatusCode == "200" &&
(s.AddedSnapshotRules.Count > 0 || s.RemovedSnapshotRules.Count > 0))
.GroupBy(s => s.FilterListId,
(key, x) => x.OrderByDescending(y => y.CreatedDateUtc).First())
.ToListAsync();
public async Task<IEnumerable<ListSummaryDto>> GetAllSummariesAsync() =>
await DbContext.FilterLists.ProjectTo<ListSummaryDto>(Mapper.ConfigurationProvider).ToListAsync();
public async Task<ListDetailsDto> GetDetailsAsync(uint id)
{
@ -79,7 +48,7 @@ private async Task<int> GetActiveRuleCount(ListDetailsDto details)
s.RemovedSnapshotRules.Count > 0))
.Select(s => s.CreatedDateUtc)
.OrderByDescending(s => s.Date);
return snapshotDates.Any() ? (DateTime?) await snapshotDates.FirstAsync() : null;
return snapshotDates.Any() ? (DateTime?)await snapshotDates.FirstAsync() : null;
}
}
}

View file

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using AutoMapper;
using FilterLists.Data.Entities;
using FilterLists.Services.FilterList.Models;
@ -21,7 +22,16 @@ private void CreateListSummaryDtoMap() =>
CreateMap<Data.Entities.FilterList, ListSummaryDto>()
.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.AddedDate, conf => conf.MapFrom(list => list.CreatedDateUtc))
.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>()