public async Task <ResultsAndTotalCount <SupportedBankDTO> > GetResultAndTotalCountSupportedBanksAsync( string propertyValueContains, OrderBySettings <SupportedBank> orderBySettings = null, int skip = 0, int?take = null) { Expression <Func <SupportedBank, bool> > filterExpression; if (string.IsNullOrEmpty(propertyValueContains)) { filterExpression = null; } else { string uppercasePropertyValueContains = propertyValueContains.ToUpper(); filterExpression = SupportedBank => SupportedBank.Name.ToUpper().Contains(uppercasePropertyValueContains); } ResultsAndTotalCount <SupportedBank> resultsAndTotalCount = await _unitOfWork .SupportedBankRepository .GetResultsAndTotalCountAsync( filterExpression, orderBySettings, skip, take ); List <SupportedBankDTO> supportedBanks = resultsAndTotalCount.Results.ToSupportedBankDTOs().ToList(); return(new ResultsAndTotalCount <SupportedBankDTO>(supportedBanks, resultsAndTotalCount.TotalCount)); }
public async Task <IActionResult> Index(string password, string filter = null, string columnName = null, bool isDescending = false, int pageNumber = 1) { if (pageNumber < 1) { throw new ArgumentOutOfRangeException(nameof(pageNumber), "Must be a positive integer."); } if (password != _adminPassword) { ViewData["AdminPasswordError"] = string.IsNullOrEmpty(password) ? "" : "Wrong admin password"; ViewBag.Filter = filter; ViewBag.ColumnName = columnName; ViewBag.PageNumber = pageNumber; ViewBag.IsDescendingString = isDescending.ToString().ToLower(); ViewBag.PageCount = 0; ViewBag.HasAdminPassword = false; return(View(new List <SupportedBankVM>())); } OrderBySettings <SupportedBank> orderBySettings; if (string.IsNullOrEmpty(columnName)) { orderBySettings = null; } else { Expression <Func <SupportedBank, object> > orderByExpression = columnName switch { "Name" => _bank => _bank.Name, _ => throw new NotImplementedException($"{nameof(columnName)} = {columnName}"), }; orderBySettings = new OrderBySettings <SupportedBank>(orderByExpression, !isDescending); } int skip = (pageNumber - 1) * PageSize; var resultsAndTotalCountSupportedBanks = await _supportedBankService.GetResultAndTotalCountSupportedBanksAsync(filter, orderBySettings, skip, PageSize); int pageCount = Utils.CalculatePageCount(resultsAndTotalCountSupportedBanks.TotalCount, PageSize); ViewBag.Filter = filter; ViewBag.ColumnName = columnName; ViewBag.PageNumber = pageNumber; ViewBag.IsDescendingString = isDescending.ToString().ToLower(); ViewBag.PageCount = pageCount; ViewBag.HasAdminPassword = true; ViewBag.AdminPassword = password; IEnumerable <SupportedBankVM> supportedBankVMs = resultsAndTotalCountSupportedBanks.Results.ToSupportedBankVMs(); return(View(supportedBankVMs)); }
public async Task <ResultsAndTotalCount <TEntity> > GetResultsAndTotalCountAsync( Expression <Func <TEntity, bool> > filterExpression = null, OrderBySettings <TEntity> orderBySettings = null, int skip = 0, int?take = null, params Expression <Func <TEntity, object> >[] includes ) { IQueryable <TEntity> query = DbSet.AsQueryable(); if (filterExpression != null) { query = query.Where(filterExpression); } int?totalCount = (skip > 0 || take.HasValue) ? (int?)await query.CountAsync() : (int?)null; if (orderBySettings != null) { query = orderBySettings.IsAscending ? query.OrderBy(orderBySettings.PropertySelector) : query.OrderByDescending(orderBySettings.PropertySelector); } if (skip > 0) { query = query.Skip(skip); } if (take.HasValue) { query = query.Take(take.Value); } foreach (Expression <Func <TEntity, object> > includeExpression in includes) { query = query.Include(includeExpression); } ICollection <TEntity> entities = await query.ToListAsync(); if (!totalCount.HasValue) { totalCount = entities.Count; } ResultsAndTotalCount <TEntity> resultsAndTotalCountDTO = new ResultsAndTotalCount <TEntity>(entities, totalCount.Value); return(resultsAndTotalCountDTO); }
public async Task <IActionResult> Index(string password, string filter = null, string columnName = null, bool isDescending = false, int pageNumber = 1) { if (pageNumber < 1) { throw new ArgumentOutOfRangeException(nameof(pageNumber), "Must be a positive integer."); } if (password != _adminPassword) { ViewData["AdminPasswordError"] = string.IsNullOrEmpty(password) ? "" : "Wrong admin password"; ViewBag.Filter = filter; ViewBag.ColumnName = columnName; ViewBag.PageNumber = pageNumber; ViewBag.IsDescendingString = isDescending.ToString().ToLower(); ViewBag.PageCount = 0; ViewBag.HasAdminPassword = false; return(View(new List <WalletVM>())); } OrderBySettings <Wallet> orderBySettings; if (string.IsNullOrEmpty(columnName)) { orderBySettings = null; } else { Expression <Func <Wallet, object> > orderByExpression = columnName switch { "First name" => _wallet => _wallet.PersonalData.FirstName, "Last name" => _wallet => _wallet.PersonalData.LastName, "Unique Master Citizen Number" => _wallet => _wallet.UniqueMasterCitizenNumber.Value, "Supported bank" => _wallet => _wallet.SupportedBank.Name, "Current amount" => _wallet => _wallet.CurrentAmount, "Status" => _wallet => _wallet.Status, "Created at" => _wallet => _wallet.CreatedAt, _ => throw new NotImplementedException($"{nameof(columnName)} = {columnName}"), }; orderBySettings = new OrderBySettings <Wallet>(orderByExpression, !isDescending); } int skip = (pageNumber - 1) * PageSize; var resultsAndTotalCountSupportedBanks = await _walletService .GetResultAndTotalCountWalletsAsync(filter, orderBySettings, skip, PageSize ); int pageCount = Utils.CalculatePageCount(resultsAndTotalCountSupportedBanks.TotalCount, PageSize); ViewBag.Filter = filter; ViewBag.ColumnName = columnName; ViewBag.PageNumber = pageNumber; ViewBag.IsDescendingString = isDescending.ToString().ToLower(); ViewBag.PageCount = pageCount; ViewBag.AdminPassword = password; ViewBag.HasAdminPassword = true; IEnumerable <WalletVM> walletVMs = resultsAndTotalCountSupportedBanks.Results.ToWalletVMs(); return(View(walletVMs)); }
public async Task <ResultsAndTotalCount <WalletDTO> > GetResultAndTotalCountWalletsAsync(string propertyValueContains, OrderBySettings <Wallet> orderBySettings, int skip, int take) { Expression <Func <Wallet, bool> > filterExpression; if (string.IsNullOrEmpty(propertyValueContains)) { filterExpression = null; } else { string uppercasePropertyValueContains = propertyValueContains.ToUpper(); filterExpression = Wallet => Wallet.PersonalData.FirstName.ToUpper().Contains(uppercasePropertyValueContains) || Wallet.PersonalData.LastName.ToUpper().Contains(uppercasePropertyValueContains) || Wallet.UniqueMasterCitizenNumber.Value.ToUpper().Contains(uppercasePropertyValueContains) || Wallet.SupportedBank.Name.ToUpper().Contains(uppercasePropertyValueContains); } ResultsAndTotalCount <Wallet> resultsAndTotalCount = await _unitOfWork .WalletRepository .GetResultsAndTotalCountAsync( filterExpression, orderBySettings, skip, take, Wallet => Wallet.SupportedBank ); List <WalletDTO> wallets = resultsAndTotalCount.Results.ToWalletDTOs().ToList(); return(new ResultsAndTotalCount <WalletDTO>(wallets, resultsAndTotalCount.TotalCount)); }