public async Task <ActionResult <PaginatedItemsDto <StockItem> > > GetPaginated( [FromHeader] string authToken, [FromQuery] int pageIndex = 0, [FromQuery] int pageSize = 1, [FromQuery] string orderBy = "StockId", [FromQuery] bool isAscending = true, [FromQuery] StockItemSearchParameters searchParameters = null ) { if (pageIndex < 0) { return(BadRequest("Page index must be greater than or equal to 0.")); } if (pageSize < 1) { return(BadRequest("Page size must be greater than or equal to 1.")); } if (!await _authenticationService.CheckAccess(authToken, "equipmentView")) { return(Unauthorized()); } var stock = await _stockItemService.GetEquipmentStock(searchParameters.EquipmentId); var queryable = stock.AsQueryable(); var filtered = GetFiltered(queryable, searchParameters); var ordered = isAscending ? filtered.OrderByMember(orderBy) : filtered.OrderByMemberDescending(orderBy); var paginated = ordered.Skip(pageIndex * pageSize).Take(pageSize); long totalCount = ordered.LongCount(); return(Ok(new PaginatedItemsDto <StockItem>(pageIndex, pageSize, totalCount, paginated))); }
private IQueryable <StockItem> GetFiltered(IQueryable <StockItem> query, StockItemSearchParameters search) { var result = query; if (search == null) { return(result); } if (!string.IsNullOrEmpty(search.StockId)) { result = result.Where(e => !string.IsNullOrEmpty(e.StockId) && e.StockId.ToLower().Contains(search.StockId.ToLower())); } if (!string.IsNullOrEmpty(search.SerialNumber)) { result = result.Where(e => !string.IsNullOrEmpty(e.SerialNumber) && e.SerialNumber.ToLower().Contains(search.SerialNumber.ToLower())); } if (search.InStockFlag) { result = result.Where(e => e.InStockFlag == search.InStockFlag); } return(result); }