public async Task <PageResponse <OrderDetails> > GetCompletedOrdersAsync(PageRequest pageRequest) { var filterQuery = _dbContext.OrderDetails .Where(x => x.OrderStatus == OrderStatus.Delivered); #region Obtain Nodes var dataQuery = filterQuery; if (pageRequest.First.HasValue) { if (!string.IsNullOrEmpty(pageRequest.After)) { int lastId = CursorHelper.FromCursor(pageRequest.After); dataQuery = dataQuery.Where(x => x.Id > lastId); } dataQuery = dataQuery.Take(pageRequest.First.Value); } if (pageRequest.OrderBy?.Field == Enums.CompletedOrdersSortingFields.Address) { dataQuery = (pageRequest.OrderBy.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.AddressLine1) : dataQuery.OrderBy(x => x.AddressLine1); } else if (pageRequest.OrderBy?.Field == Enums.CompletedOrdersSortingFields.Amount) { dataQuery = (pageRequest.OrderBy.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Amount) : dataQuery.OrderBy(x => x.Amount); } else { dataQuery = (pageRequest.OrderBy.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Id) : dataQuery.OrderBy(x => x.Id); } List <OrderDetails> nodes = await dataQuery.ToListAsync(); #endregion #region Obtain Flags int maxId = nodes.Max(x => x.Id); int minId = nodes.Min(x => x.Id); bool hasNextPage = await filterQuery.AnyAsync(x => x.Id > maxId); bool hasPrevPage = await filterQuery.AnyAsync(x => x.Id < minId); int totalCount = await filterQuery.CountAsync(); #endregion return(new PageResponse <OrderDetails> { Nodes = nodes, HasNextPage = hasNextPage, HasPreviousPage = hasPrevPage, TotalCount = totalCount }); }
public async Task <PageResponse <OrderDetail> > GetCompletedOrdersAsync(PageRequest pageRequest) { var deliveredOrders = this.context.OrderDetails .Where(x => x.OrderStatus == OrderStatus.Delivered); #region Obtain Nodes var dataQuery = deliveredOrders; if (pageRequest.First.HasValue) { // if after is specified // we get the last id, and only get orders = OrderStatus.Delivered is greater than the last id if (!string.IsNullOrEmpty(pageRequest.After)) { int lastId = CursorHelper.FromCursor(pageRequest.After); dataQuery = dataQuery.Where(x => x.Id > lastId); } dataQuery = dataQuery.Take(pageRequest.First.Value); } // LOGIC FOR BEFORE AND LAST NOT YET VERIFIED if (pageRequest.Last.HasValue) { if (!string.IsNullOrEmpty(pageRequest.Before)) { int beforeId = CursorHelper.FromCursor(pageRequest.Before); dataQuery = dataQuery.Where(x => x.Id > beforeId); } // dataQuery = dataQuery.TakeLast(pageRequest.Last.Value); // TakeLast might not be translated by EF to SQL(not yet tested) // we reverse the order so that we start on the end dataQuery = dataQuery.OrderByDescending(x => x.Id).Take(pageRequest.Last.Value); } // We only sort if we have specified OrderBy if (pageRequest.OrderBy?.Field == Enums.CompletedOrdersSortingFields.Address) { dataQuery = (pageRequest.OrderBy.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.AddressLine1) : dataQuery.OrderBy(x => x.AddressLine1); } else if (pageRequest.OrderBy?.Field == Enums.CompletedOrdersSortingFields.Amount) { dataQuery = (pageRequest.OrderBy.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Amount) : dataQuery.OrderBy(x => x.Amount); } else { dataQuery = (pageRequest.OrderBy?.Direction == Enums.SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Id) : dataQuery.OrderBy(x => x.Id); } List <OrderDetail> nodes = await dataQuery?.ToListAsync(); #endregion #region Obtain Flags //WHY DO WE NEED node.max id? and min int maxId = nodes.Count > 0 ? nodes.Max(x => x.Id) : 0; int minId = nodes.Count > 0 ? nodes.Min(x => x.Id) : 0; bool hasNextPage = await deliveredOrders.AnyAsync(x => x.Id > maxId); bool hasPrevPage = await deliveredOrders.AnyAsync(x => x.Id < minId); int totalCount = await deliveredOrders.CountAsync(); #endregion return(new PageResponse <OrderDetail> { Nodes = nodes, HasNextPage = hasNextPage, HasPreviousPage = hasPrevPage, TotalCount = totalCount }); }
public async Task <PagedResponse <OrderDetail> > GetCompletedOrdersAsync(PagedRequest request) { var filterQuery = _context.OrderDetails.Where(x => x.OrderStatus == OrderStatus.Delivered); #region Obtain Nodes var dataQuery = filterQuery; if (request.First.HasValue) { if (!string.IsNullOrEmpty(request.After)) { var lastId = CursorHelper.FromCursor(request.After); dataQuery = dataQuery.Where(x => x.Id > lastId); } dataQuery = dataQuery.Take(request.First.Value); } switch (request.OrderBy?.Field) { case CompletedOrderSortingFields.Address: dataQuery = (request.OrderBy.SortingDirection == SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.AddressLine1) : dataQuery.OrderBy(x => x.AddressLine1); break; case CompletedOrderSortingFields.Amount: dataQuery = (request.OrderBy.SortingDirection == SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Amount) : dataQuery.OrderBy(x => x.Amount); break; case CompletedOrderSortingFields.Id: break; case null: break; default: dataQuery = (request?.OrderBy?.SortingDirection == SortingDirection.DESC) ? dataQuery.OrderByDescending(x => x.Id) : dataQuery.OrderBy(x => x.Id); break; } var nodes = await dataQuery.ToListAsync(); #endregion #region Obtain Flags var maxId = nodes.Max(x => x.Id); var minId = nodes.Min(x => x.Id); var hasNextPage = await filterQuery.AnyAsync(x => x.Id > maxId); var hasPreviousPage = await filterQuery.AnyAsync(x => x.Id < minId); var totalCount = await filterQuery.CountAsync(); #endregion return(new PagedResponse <OrderDetail> { HasNextPage = hasNextPage, HasPreviousPage = hasPreviousPage, Nodes = nodes, TotalCount = totalCount }); }