using CarCareTracker.External.Interfaces; using CarCareTracker.Helper; using CarCareTracker.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace CarCareTracker.Controllers { [Authorize] public class APIController : Controller { private readonly IVehicleDataAccess _dataAccess; private readonly INoteDataAccess _noteDataAccess; private readonly IServiceRecordDataAccess _serviceRecordDataAccess; private readonly IGasRecordDataAccess _gasRecordDataAccess; private readonly ICollisionRecordDataAccess _collisionRecordDataAccess; private readonly ITaxRecordDataAccess _taxRecordDataAccess; private readonly IReminderRecordDataAccess _reminderRecordDataAccess; private readonly IUpgradeRecordDataAccess _upgradeRecordDataAccess; private readonly IReminderHelper _reminderHelper; private readonly IGasHelper _gasHelper; public APIController(IVehicleDataAccess dataAccess, IGasHelper gasHelper, IReminderHelper reminderHelper, INoteDataAccess noteDataAccess, IServiceRecordDataAccess serviceRecordDataAccess, IGasRecordDataAccess gasRecordDataAccess, ICollisionRecordDataAccess collisionRecordDataAccess, ITaxRecordDataAccess taxRecordDataAccess, IReminderRecordDataAccess reminderRecordDataAccess, IUpgradeRecordDataAccess upgradeRecordDataAccess) { _dataAccess = dataAccess; _noteDataAccess = noteDataAccess; _serviceRecordDataAccess = serviceRecordDataAccess; _gasRecordDataAccess = gasRecordDataAccess; _collisionRecordDataAccess = collisionRecordDataAccess; _taxRecordDataAccess = taxRecordDataAccess; _reminderRecordDataAccess = reminderRecordDataAccess; _upgradeRecordDataAccess = upgradeRecordDataAccess; _gasHelper = gasHelper; _reminderHelper = reminderHelper; } public IActionResult Index() { return View(); } [HttpGet] [Route("/api/vehicles")] public IActionResult Vehicles() { var result = _dataAccess.GetVehicles(); return Json(result); } [HttpGet] [Route("/api/vehicle/servicerecords")] public IActionResult ServiceRecords(int vehicleId) { var vehicleRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId); var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } [HttpGet] [Route("/api/vehicle/repairrecords")] public IActionResult RepairRecords(int vehicleId) { var vehicleRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId); var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } [HttpGet] [Route("/api/vehicle/upgraderecords")] public IActionResult UpgradeRecords(int vehicleId) { var vehicleRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId); var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } [HttpGet] [Route("/api/vehicle/taxrecords")] public IActionResult TaxRecords(int vehicleId) { var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId); return Json(result); } [HttpGet] [Route("/api/vehicle/gasrecords")] public IActionResult GasRecords(int vehicleId, bool useMPG, bool useUKMPG) { var vehicleRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId); var result = _gasHelper.GetGasRecordViewModels(vehicleRecords, useMPG, useUKMPG).Select(x => new GasRecordExportModel { Date = x.Date, Odometer = x.Mileage.ToString(), Cost = x.Cost.ToString(), FuelConsumed = x.Gallons.ToString(), FuelEconomy = x.MilesPerGallon.ToString()}); return Json(result); } [HttpGet] [Route("/api/vehicle/reminders")] public IActionResult Reminders(int vehicleId) { var currentMileage = GetMaxMileage(vehicleId); var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId); var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes}); return Json(results); } private int GetMaxMileage(int vehicleId) { var numbersArray = new List(); var serviceRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId); if (serviceRecords.Any()) { numbersArray.Add(serviceRecords.Max(x => x.Mileage)); } var repairRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId); if (repairRecords.Any()) { numbersArray.Add(repairRecords.Max(x => x.Mileage)); } var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId); if (gasRecords.Any()) { numbersArray.Add(gasRecords.Max(x => x.Mileage)); } var upgradeRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId); if (upgradeRecords.Any()) { numbersArray.Add(upgradeRecords.Max(x => x.Mileage)); } return numbersArray.Any() ? numbersArray.Max() : 0; } } }