private IQueryable <Citation> ApplyCitationFilter(AdminCitationListViewModel model) { var citations = _accountCtx.Citations.ForAccount(model.AccountId.Value) .Include(x => x.Violation).ThenInclude(m => m.Category).ThenInclude(m => m.Type) .Include(x => x.Payments) .OrderByDescending(x => x.CreateUtc) .AsQueryable(); if (model.CreatedFrom.HasValue) { citations = citations.Where(s => s.CreateUtc >= model.CreatedFrom.Value.Floor()); } if (model.CreatedTo.HasValue) { citations = citations.Where(s => s.CreateUtc <= model.CreatedTo.Value.Ceiling()); } if (model.StatusId.HasValue) { citations = citations.Where(s => s.Status == model.StatusId); } if (model.AssignedToId.HasValue) { citations = citations.Where(s => s.AssignedToId == model.AssignedToId); } if (!string.IsNullOrWhiteSpace(model.LicensePlate)) { citations = citations.Where(s => s.LicensePlate.Contains(model.LicensePlate)); } if (model.ViolationTypeId.HasValue) { citations = citations.Where(s => s.Violation.Category.TypeId == model.ViolationTypeId); } if (model.ViolationId.HasValue) { citations = citations.Where(s => s.ViolationId == model.ViolationId); } if (!string.IsNullOrEmpty(model.PostalCode)) { citations = citations.Where(s => s.PostalCode.Contains(model.PostalCode)); } if (!string.IsNullOrEmpty(model.Street)) { citations = citations.Where(s => s.Street.Contains(model.Street)); } if (model.HasBalance.HasValue && model.HasBalance == true) { citations = citations.Where(s => s.FineAmount.HasValue) .Where(m => m.FineAmount.Value > 0) .Where(m => m.Balance.HasValue) .Where(m => m.Balance.Value == 0); } if (model.HasBalance.HasValue && model.HasBalance == false) { citations = citations.Where(s => !s.FineAmount.HasValue); } return(citations); }
/// <summary> /// getting all citation list /// </summary> /// <param name="CreatedFrom"></param> /// <param name="Createdto"></param> /// <param name="StatusId"></param> /// <param name="AssignedToId"></param> /// <param name="model"></param> /// <returns></returns> public async Task <IActionResult> Index(AdminCitationListViewModel model) { var currentPageNum = model.Page; var offset = (model.PageSize * currentPageNum) - model.PageSize; if (model.AccountId.HasValue) { var accountDetail = await CommonContext.CommonAccounts.Include(m => m.Partition).SingleOrDefaultAsync(account => account.Id == model.AccountId); _accountCtx = ContextsUtility.CreateAccountContext(Cryptography.Decrypt(accountDetail.Partition.ConnectionString)); var citations = ApplyCitationFilter(model); #region sort switch (model.SortOrder) { case "Status": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.Status); } else { citations = citations.OrderBy(s => s.Status); } break; case "Violation": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.Violation.Name); } else { citations = citations.OrderBy(s => s.Violation.Name); } break; case "AssignedTo": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.AssignedTo); } else { citations = citations.OrderBy(s => s.AssignedTo); } break; case "Created": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.CreateUtc); } else { citations = citations.OrderBy(s => s.CreateUtc); } break; case "License": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.LicensePlate); } else { citations = citations.OrderBy(s => s.LicensePlate); } break; case "Number": if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.CitationNumber); } else { citations = citations.OrderBy(s => s.CitationNumber); } break; default: if (model.SortDirection == "DESC") { citations = citations.OrderByDescending(s => s.CreateUtc); } else { citations = citations.OrderBy(s => s.CreateUtc); } break; } #endregion var totalQueryCount = await citations.CountAsync(); model.CitationsListItem = Mapper.Map <List <CitationsListItem> >(await citations.Skip(offset).Take(model.PageSize).ToListAsync()); foreach (var m in model.CitationsListItem) { m.CreatedHumanizerDate = m.Created.Humanize(); } model.Paging.CurrentPage = currentPageNum; model.Paging.ItemsPerPage = model.PageSize; model.Paging.TotalItems = totalQueryCount; } #region dropdown lists //Accounts List //TODO: this should be cached and retrieved from a service var accounts = await CommonContext.CommonAccounts.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString(), }) .ToListAsync(); model.Accounts = accounts; #endregion return(View(model)); }