using CarCareTracker.Models; using System.Globalization; namespace CarCareTracker.Helper { public interface IReportHelper { IEnumerable GetOdometerRecordSum(List odometerRecords, int year = 0); IEnumerable GetServiceRecordSum(List serviceRecords, int year = 0); IEnumerable GetRepairRecordSum(List repairRecords, int year = 0); IEnumerable GetUpgradeRecordSum(List upgradeRecords, int year = 0); IEnumerable GetGasRecordSum(List gasRecords, int year = 0); IEnumerable GetTaxRecordSum(List taxRecords, int year = 0); } public class ReportHelper: IReportHelper { public IEnumerable GetOdometerRecordSum(List odometerRecords, int year = 0) { if (year != default) { odometerRecords.RemoveAll(x => x.Date.Year != year); } return odometerRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = 0, MaxMileage = x.Max(y => y.Mileage), MinMileage = x.Min(y => y.Mileage) }); } public IEnumerable GetServiceRecordSum(List serviceRecords, int year = 0) { if (year != default) { serviceRecords.RemoveAll(x => x.Date.Year != year); } return serviceRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = x.Sum(y => y.Cost), MaxMileage = x.Max(y=>y.Mileage), MinMileage = x.Min(y=>y.Mileage) }); } public IEnumerable GetRepairRecordSum(List repairRecords, int year = 0) { if (year != default) { repairRecords.RemoveAll(x => x.Date.Year != year); } return repairRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = x.Sum(y => y.Cost), MaxMileage = x.Max(y => y.Mileage), MinMileage = x.Min(y => y.Mileage) }); } public IEnumerable GetUpgradeRecordSum(List upgradeRecords, int year = 0) { if (year != default) { upgradeRecords.RemoveAll(x => x.Date.Year != year); } return upgradeRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = x.Sum(y => y.Cost), MaxMileage = x.Max(y => y.Mileage), MinMileage = x.Min(y => y.Mileage) }); } public IEnumerable GetGasRecordSum(List gasRecords, int year = 0) { if (year != default) { gasRecords.RemoveAll(x => x.Date.Year != year); } return gasRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = x.Sum(y => y.Cost), MaxMileage = x.Max(y => y.Mileage), MinMileage = x.Min(y => y.Mileage) }); } public IEnumerable GetTaxRecordSum(List taxRecords, int year = 0) { if (year != default) { taxRecords.RemoveAll(x => x.Date.Year != year); } return taxRecords.GroupBy(x => x.Date.Month).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), Cost = x.Sum(y => y.Cost) }); } } }