Skip to content

Commit

Permalink
Merge pull request #45 from IliyanIlievPH/44
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
starkmsu authored May 28, 2020
2 parents 2e38cb2 + 8308691 commit bfe2679
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ public async Task<CampaignsPage> GetCampaignsAsync(CampaignListRequest request)
{
using (var context = _contextFactory.CreateDataContext())
{
var hasPartnersFilter = request.PartnerIds != null && request.PartnerIds.Any();
var query = context.VoucherCampaigns
.Where(c => c.State != CampaignState.Deleted);

if (request.PartnerIds != null && request.PartnerIds.Any())
if (hasPartnersFilter)
{
query = query.Where(c => request.PartnerIds.Contains(c.PartnerId));
}
Expand Down Expand Up @@ -131,14 +132,35 @@ public async Task<CampaignsPage> GetCampaignsAsync(CampaignListRequest request)
query = query.Where(p => p.CreatedBy == request.CreatedBy.ToString());
}

var result = await query
query = query
.Include(c => c.LocalizedContents)
.AsNoTracking()
.OrderByDescending(i => i.CreationDate)
.Skip(request.Skip)
.Take(request.Take)
.ToListAsync();
.AsNoTracking();

List<VoucherCampaignEntity> result;

if (hasPartnersFilter && request.PartnerIds.Length > 1)
{
var partnersOrders = new Dictionary<Guid, int>();
for (var i = 0; i < request.PartnerIds.Length; i++)
{
partnersOrders.Add(request.PartnerIds[i], i);
}
//This is done in memory because we cannot apply this ordering and paging on db level (can't be translated)
result = await query.ToListAsync();
result = result
.OrderBy(p => partnersOrders[p.PartnerId])
.Skip(request.Skip)
.Take(request.Take)
.ToList();
}
else
{
result = await query
.OrderByDescending(i => i.CreationDate)
.Skip(request.Skip)
.Take(request.Take)
.ToListAsync();
}
var totalCount = await query.CountAsync();

return new CampaignsPage
Expand Down

0 comments on commit bfe2679

Please sign in to comment.