public IEnumerable <YearPriceModel> GetPricesByManufactureDate(PricesChartFilterOptions filters) { var filteredRecords = _dbContext.Records .AsNoTracking() .Where(r => r.ManufactureDate.Year >= filters.YearRange.Start && r.ManufactureDate.Year <= filters.YearRange.End); if (filters.ModelId != null) { filteredRecords = filteredRecords.Where(r => r.ModelId == filters.ModelId); } else if (filters.BrandId != null) { filteredRecords = filteredRecords.Include(r => r.Model) .Where(r => r.Model.BrandId == filters.BrandId); } if (filters.FuelType != null) { filteredRecords = filteredRecords.Where(r => r.Fuel == filters.FuelType); } var groupedRecords = filteredRecords.GroupBy(r => r.ManufactureDate.Year) .Select(g => new YearPriceModel { Year = g.Key, Price = Convert.ToInt32(g.Average(r => r.Price)) }); var result = FillGapsWithNull(groupedRecords.ToList(), filters.YearRange); return(result); }
public ActionResult <IList <YearPriceModel> > ByManufactureDate([FromBody] PricesChartFilterOptions filters) { return(Ok(_pricesChartService.GetPricesByManufactureDate(filters))); }