Esempio n. 1
0
        public async Task <IActionResult> GetInventory([FromQuery] InventoryFilterModel filterModel,
                                                       int count = 10, int page = 1)
        {
            var result = await _inventoryService.GetInventory(page, count, filterModel);

            return(ApiResponder.RespondSuccess(result, null, result.GetPaginationData()));
        }
        public async Task <CustomList <InventoryReturnModel> > GetInventory(int page, int count, InventoryFilterModel filterModel)
        {
            if (page <= 0 || count <= 0)
            {
                throw new InvalidPaginationDataException();
            }

            var startAt = (page - 1) * count;

            var items = await _context.ProductInventories.Skip(startAt).Take(count)
                        .OrderByDescending(i => i.ImportedDate)
                        .Where(i => filterModel.ProductId <= 0 ? i.ProductId > filterModel.ProductId :
                               i.ProductId == filterModel.ProductId &&
                               filterModel.SupplierId <= 0 ? i.SupplierId > filterModel.SupplierId :
                               i.SupplierId == filterModel.SupplierId &&
                               i.Product.Name.Contains(filterModel.ProductName, StringComparison.InvariantCultureIgnoreCase) &&
                               i.Supplier.Name.Contains(filterModel.SupplierName, StringComparison.InvariantCultureIgnoreCase))
                        .Include(i => i.Customer)
                        .Include(i => i.Supplier)
                        .Include(i => i.Product)
                        .ToListAsync();

            var totalCount = await _context.ProductInventories
                             .OrderByDescending(i => i.ImportedDate)
                             .Where(i => filterModel.ProductId <= 0 ? i.ProductId > filterModel.ProductId :
                                    i.ProductId == filterModel.ProductId &&
                                    filterModel.SupplierId <= 0 ? i.SupplierId > filterModel.SupplierId :
                                    i.SupplierId == filterModel.SupplierId &&
                                    i.Product.Name.Contains(filterModel.ProductName, StringComparison.InvariantCultureIgnoreCase) &&
                                    i.Supplier.Name.Contains(filterModel.SupplierName, StringComparison.InvariantCultureIgnoreCase))
                             .Include(i => i.Customer)
                             .Include(i => i.Supplier)
                             .Include(i => i.Product).CountAsync();

            var totalPages = totalCount / count + (totalCount % count > 0 ? 1 : 0);

            var itemReturnModels = _mapper.Map <List <ProductInventory>, CustomList <InventoryReturnModel> >(items);

            foreach (var itemReturnModel in itemReturnModels)
            {
                if (itemReturnModel.ProductDetails == null)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(itemReturnModel.ProductDetails.Image) ||
                    string.IsNullOrWhiteSpace(itemReturnModel.ProductDetails.Image))
                {
                    itemReturnModel.ProductDetails.Image = DefaultImageUrl;
                }
            }
            itemReturnModels.CurrentPage   = page;
            itemReturnModels.TotalItems    = totalCount;
            itemReturnModels.IsListPartial = true;
            itemReturnModels.TotalPages    = totalPages;

            return(itemReturnModels);
        }