public async Task <PaginatedResponse <InventoryApiModel> > Handle(GetAllInventoriesQuery request, CancellationToken cancellationToken)
        // public async Task<IEnumerable<InventoryApiModel>> Handle(GetAllInventoriesQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var inventories = await _repo.GetAllInventories(request, cancellationToken)
                                  .ConfigureAwait(false);

                var prev = _uriGen.AddQueryStringParams("pageNumber", (request.PageNumber - 1).ToString() !);
                prev.AddQueryStringParams("pageSize", request.PageSize.ToString() !);
                var nextL = _uriGen.AddQueryStringParams("pageNumber", (request.PageNumber + 1).ToString() !);
                nextL.AddQueryStringParams("pageSize", request.PageSize.ToString() !);
                var pagedList = PagedList <InventoryApiModel> .CreatePagedList(inventories.AsQueryable(),
                                                                               request.PageNumber, request.PageSize);

                var prevLink = pagedList.HasPrevious ? prev.GenerateUri() : null;
                var nextLink = pagedList.HasNext ? nextL.GenerateUri() : null;
                var result   = new PaginatedResponse <InventoryApiModel>(pagedList, pagedList.TotalCount,
                                                                         pagedList.PageSize, pagedList.CurrentPage, prevLink?.AbsoluteUri, nextLink?.AbsoluteUri, ResponseStatus.Success);
                return(result);
            }
            catch (Exception e)
            {
                return(new PaginatedResponse <InventoryApiModel>(new PagedList <InventoryApiModel>(), 0, 10, 1, "", "", ResponseStatus.Error,
                                                                 new { ErrorMessage = e.Message }));
                // return new List<InventoryApiModel>();
            }
        }
示例#2
0
        public async Task <PaginatedResponse <SaleApiModel> > Handle(GetAllSalesQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var sales = await _repo.GetAllSales(request, cancellationToken).ConfigureAwait(false);

                var prev = _uriGen.AddQueryStringParams("pageNumber", (request.PageNumber - 1).ToString() !);
                prev.AddQueryStringParams("pageSize", request.PageSize.ToString() !);
                var nextL = _uriGen.AddQueryStringParams("pageNumber", (request.PageNumber + 1).ToString() !);
                nextL.AddQueryStringParams("pageSize", request.PageSize.ToString() !);

                var prevLink = sales.HasPrevious
                    ? prev.GenerateUri() : null;
                var nextLink = sales.HasNext
                    ? nextL.GenerateUri() : null;

                var result = new PaginatedResponse <SaleApiModel>(sales,
                                                                  sales.TotalCount, sales.PageSize, sales.CurrentPage,
                                                                  prevLink?.PathAndQuery, nextLink?.PathAndQuery, ResponseStatus.Success);

                return(result);
            }
            catch (Exception e)
            {
                return(new PaginatedResponse <SaleApiModel>(new PagedList <SaleApiModel>(), 0, 10, 1, "", "", ResponseStatus.Error, new { ErrorMessage = e.Message }));
            }
        }