public async Task <IActionResult> GetHistoryItems([FromQuery] HistoryItemParams historyItemParams) { var historyItems = await _historyRepository.GetHistoryItems(historyItemParams); Response.AddPagination(historyItems.CurrentPage, historyItems.PageSize, historyItems.TotalCount, historyItems.TotalPages); return(Ok(historyItems)); }
public async Task <PagedList <PriceHistoryItem> > GetHistoryItems(HistoryItemParams historyItemParams) { var historyItems = _context.PriceHistory.Include(p => p.Source).AsQueryable(); if (historyItemParams.SourceId != 0) { historyItems = historyItems.Where(u => u.SourceId == historyItemParams.SourceId); } DateTime dateFrom; DateTime dateTo; if (DateTime.TryParse(historyItemParams.DateFrom, out dateFrom)) { historyItems = historyItems.Where(u => u.Timestamp >= dateFrom); } if (DateTime.TryParse(historyItemParams.DateTo, out dateTo)) { historyItems = historyItems.Where(u => u.Timestamp <= dateTo); } if (!string.IsNullOrEmpty(historyItemParams.OrderBy)) { switch (historyItemParams.OrderBy) { case "price": historyItems = historyItems.OrderByDescending(u => u.Bid); break; default: historyItems = historyItems.OrderByDescending(u => u.Timestamp); break; } } return(await PagedList <PriceHistoryItem> .CreateAsync(historyItems, historyItemParams.PageNumber, historyItemParams.PageSize)); }