public async Task <GasStationsDashboard> GetGasStations(GetGasStationsDashboard query) { var now = DateTime.UtcNow; var gasStations = await dbContext.GasStations .Include(x => x.SubmitedPrices) .ThenInclude(x => x.Ratings) .Include(x => x.Franchise) .ThenInclude(x => x.WholesalePrices) .PickPageAsync(query.PageNumber, query.PageSize); return(new GasStationsDashboard( paging: gasStations.Paging, results: gasStations.Results .Select(x => new GasStationsDashboardItem( gasStation: x.ToContract(), franchiseName: x.Franchise?.Name, wholesalePrices: x.Franchise.WholesalePrices.Select(ToDashboard).ToList(), minimalSubmittedPrices: GetAggregatedPricesInWindowForFuelType(x.SubmitedPrices, x => x.Min()), maximalSubmittedPrices: GetAggregatedPricesInWindowForFuelType(x.SubmitedPrices, x => x.Max()) )) .ToList())); IReadOnlyCollection <FuelPrice> GetAggregatedPricesInWindowForFuelType( IEnumerable <PriceSubmission> priceSubmissions, Func <IEnumerable <decimal>, decimal> aggregation) { return(priceSubmissions .Where(x => x.TotalScore >= 0) .GroupBy(x => x.FuelType) .Select(fuelPrices => { var prices = fuelPrices.Where(x => now - x.SubmissionDate <= query.PastSubmittedPricesAggregationWindow).ToList(); var amount = prices.Any() ? aggregation(prices.Select(x => x.Amount)) : priceSubmissions.OrderByDescending(x => x.SubmissionDate).First().Amount; return new FuelPrice(fuelPrices.Key, amount); }) .ToList()); } }
public Task <GasStationsDashboard> GetGasStationsDashboard([FromQuery] GetGasStationsDashboard query) { return(service.GetGasStations(query)); }