public async Task <IActionResult> GetInventoryForUser(GetInventoryParameters inventoryParameters) { ViewData["MaximumQuantityFilter"] = inventoryParameters.MaximumQuantity; ViewData["SkuFilter"] = inventoryParameters.Sku; ViewData["ProductIdFilter"] = inventoryParameters.ProductId; var user = await _userManager.GetUserAsync(HttpContext.User); var inventories = await _cachingInventoryStore.GetInventoryForUserAsync(user.Id, inventoryParameters); return(View(inventories)); }
public Task <PagedList <Inventory> > GetInventoryForUserAsync(int userId, GetInventoryParameters getInventoryParameters) { var collectionBeforePaging = _context.Inventory .Include(x => x.Product) .Where(i => i.Product.Admin.Id == userId) .OrderBy(a => a.Product.Id) .AsNoTracking() .AsQueryable(); if (!string.IsNullOrEmpty(getInventoryParameters.Sku)) { // trim & ignore casing var skuForWhereClause = getInventoryParameters.Sku .Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(a => a.Sku.ToLowerInvariant() == skuForWhereClause); } if (getInventoryParameters.ProductId.HasValue) { // trim & ignore casing var productIdFilterForWhereClause = getInventoryParameters.ProductId.Value; collectionBeforePaging = collectionBeforePaging .Where(a => a.Product.Id == productIdFilterForWhereClause); } if (getInventoryParameters.MaximumQuantity.HasValue) { // trim & ignore casing var maximumQuantityQueryForWhereClause = getInventoryParameters.MaximumQuantity.Value; collectionBeforePaging = collectionBeforePaging .Where(a => a.Quantity <= maximumQuantityQueryForWhereClause); } return(PagedList <Inventory> .CreateAsync(collectionBeforePaging, getInventoryParameters.PageNumber, getInventoryParameters.PageSize)); }
public async Task <PagedList <InventoryDto> > GetInventoryForUserAsync(int userId, GetInventoryParameters getInventoryParameters) { PagedList <InventoryDto> result; string key = "user:"******"|inventoryList" + "|pageNumber" + getInventoryParameters.PageNumber + "|pageSize:" + getInventoryParameters.PageSize + (!string.IsNullOrEmpty(getInventoryParameters.Sku) ? "|sku:" + getInventoryParameters.Sku : "") + (getInventoryParameters.MaximumQuantity.HasValue ? "|maximumQuantity:" + getInventoryParameters.MaximumQuantity.Value:"") + (getInventoryParameters.ProductId.HasValue ? "|productId:" + getInventoryParameters.ProductId.Value : ""); var cachedResults = await _cacheStorage.GetObjectAsync <PagedList <InventoryDto> >(key); if (cachedResults != null) { result = cachedResults; } else { var productsForUserFromRepo = await _inventoryRepository.GetInventoryForUserAsync(userId, getInventoryParameters); result = Mapper.Map <PagedList <InventoryDto> >(productsForUserFromRepo); await _cacheStorage.SetStringAsync(key, JsonConvert.SerializeObject(result)); } return(result); }