From 943dee8d743e700cf259b5630fe1c63d81adc7d8 Mon Sep 17 00:00:00 2001 From: ivancheahhh Date: Tue, 2 Jan 2024 16:55:12 -0700 Subject: [PATCH] added backend code for gas records. --- Controllers/VehicleController.cs | 58 +++++++++++++- .../Implementations/GasRecordDataAccess.cs | 47 +++++++++++ External/Interfaces/IGasRecordDataAccess.cs | 12 +++ Models/GasRecord.cs | 19 +++++ Models/GasRecordInput.cs | 20 +++++ Models/GasRecordViewModel.cs | 21 +++++ Program.cs | 1 + Views/Vehicle/_Gas.cshtml | 54 +++++++++++++ Views/Vehicle/_GasModal.cshtml | 77 +++++++++++++++++++ Views/Vehicle/_ServiceRecordModal.cshtml | 10 +-- wwwroot/js/servicerecord.js | 4 +- 11 files changed, 315 insertions(+), 8 deletions(-) create mode 100644 External/Implementations/GasRecordDataAccess.cs create mode 100644 External/Interfaces/IGasRecordDataAccess.cs create mode 100644 Models/GasRecord.cs create mode 100644 Models/GasRecordInput.cs create mode 100644 Models/GasRecordViewModel.cs create mode 100644 Views/Vehicle/_Gas.cshtml create mode 100644 Views/Vehicle/_GasModal.cshtml diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index b875a98..b1b1089 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -18,16 +18,24 @@ namespace CarCareTracker.Controllers private readonly IVehicleDataAccess _dataAccess; private readonly INoteDataAccess _noteDataAccess; private readonly IServiceRecordDataAccess _serviceRecordDataAccess; + private readonly IGasRecordDataAccess _gasRecordDataAccess; private readonly IWebHostEnvironment _webEnv; private readonly IFileHelper _fileHelper; - public VehicleController(ILogger logger, IFileHelper fileHelper, IVehicleDataAccess dataAccess, INoteDataAccess noteDataAccess, IServiceRecordDataAccess serviceRecordDataAccess, IWebHostEnvironment webEnv) + public VehicleController(ILogger logger, + IFileHelper fileHelper, + IVehicleDataAccess dataAccess, + INoteDataAccess noteDataAccess, + IServiceRecordDataAccess serviceRecordDataAccess, + IGasRecordDataAccess gasRecordDataAccess, + IWebHostEnvironment webEnv) { _logger = logger; _dataAccess = dataAccess; _noteDataAccess = noteDataAccess; _fileHelper = fileHelper; _serviceRecordDataAccess = serviceRecordDataAccess; + _gasRecordDataAccess = gasRecordDataAccess; _webEnv = webEnv; } [HttpGet] @@ -64,6 +72,52 @@ namespace CarCareTracker.Controllers } return Json(""); } + #region "Gas Records" + [HttpGet] + public IActionResult GetGasRecordsByVehicleId(int vehicleId) + { + var result = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId); + var computedResults = new List(); + //perform computation. + for(int i = 0; i < result.Count; i++) + { + if (i > 0) + { + var currentObject = result[i]; + var previousObject = result[i - 1]; + var deltaMileage = currentObject.Mileage - previousObject.Mileage; + computedResults.Add(new GasRecordViewModel() + { + Id = currentObject.Id, + VehicleId = currentObject.VehicleId, + Date = currentObject.Date.ToShortDateString(), + Mileage = currentObject.Mileage, + Gallons = currentObject.Gallons, + Cost = currentObject.Cost, + DeltaMileage = deltaMileage, + MilesPerGallon = deltaMileage / currentObject.Gallons, + CostPerGallon = (currentObject.Cost / currentObject.Gallons) + }); + } else + { + computedResults.Add(new GasRecordViewModel() + { + Id = result[i].Id, + VehicleId = result[i].VehicleId, + Date = result[i].Date.ToShortDateString(), + Mileage = result[i].Mileage, + Gallons = result[i].Gallons, + Cost = result[i].Cost, + DeltaMileage = 0, + MilesPerGallon = 0, + CostPerGallon = (result[i].Cost / result[i].Gallons) + }); + } + } + return PartialView("_GasRecords", result); + } + #endregion + #region "Service Records" [HttpGet] public IActionResult GetServiceRecordsByVehicleId(int vehicleId) { @@ -105,5 +159,7 @@ namespace CarCareTracker.Controllers var result = _serviceRecordDataAccess.DeleteServiceRecordById(serviceRecordId); return Json(result); } + #endregion + } } diff --git a/External/Implementations/GasRecordDataAccess.cs b/External/Implementations/GasRecordDataAccess.cs new file mode 100644 index 0000000..87b23a6 --- /dev/null +++ b/External/Implementations/GasRecordDataAccess.cs @@ -0,0 +1,47 @@ +using CarCareTracker.External.Interfaces; +using CarCareTracker.Models; +using LiteDB; + +namespace CarCareTracker.External.Implementations +{ + public class GasRecordDataAccess: IGasRecordDataAccess + { + private static string dbName = "cartracker.db"; + private static string tableName = "gasrecords"; + public List GetGasRecordsByVehicleId(int vehicleId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + var gasRecords = table.Find(Query.EQ(nameof(GasRecord.VehicleId), vehicleId)).OrderBy(x => x.Date); + return gasRecords.ToList() ?? new List(); + }; + } + public GasRecord GetGasRecordById(int gasRecordId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + return table.FindById(gasRecordId); + }; + } + public bool DeleteGasRecordById(int gasRecordId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + table.Delete(gasRecordId); + return true; + }; + } + public bool SaveGasRecordToVehicle(GasRecord gasRecord) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + table.Upsert(gasRecord); + return true; + }; + } + } +} diff --git a/External/Interfaces/IGasRecordDataAccess.cs b/External/Interfaces/IGasRecordDataAccess.cs new file mode 100644 index 0000000..1a219f7 --- /dev/null +++ b/External/Interfaces/IGasRecordDataAccess.cs @@ -0,0 +1,12 @@ +using CarCareTracker.Models; + +namespace CarCareTracker.External.Interfaces +{ + public interface IGasRecordDataAccess + { + public List GetGasRecordsByVehicleId(int vehicleId); + public GasRecord GetGasRecordById(int gasRecordId); + public bool DeleteGasRecordById(int gasRecordId); + public bool SaveGasRecordToVehicle(GasRecord gasRecord); + } +} diff --git a/Models/GasRecord.cs b/Models/GasRecord.cs new file mode 100644 index 0000000..77b5424 --- /dev/null +++ b/Models/GasRecord.cs @@ -0,0 +1,19 @@ +namespace CarCareTracker.Models +{ + public class GasRecord + { + public int Id { get; set; } + public int VehicleId { get; set; } + public DateTime Date { get; set; } + /// + /// American moment + /// + public int Mileage { get; set; } + /// + /// Wtf is a kilometer? + /// + public decimal Gallons { get; set; } + public decimal Cost { get; set; } + public List Files { get; set; } = new List(); + } +} diff --git a/Models/GasRecordInput.cs b/Models/GasRecordInput.cs new file mode 100644 index 0000000..51d23ff --- /dev/null +++ b/Models/GasRecordInput.cs @@ -0,0 +1,20 @@ +namespace CarCareTracker.Models +{ + public class GasRecordInput + { + public int Id { get; set; } + public int VehicleId { get; set; } + public string Date { get; set; } + /// + /// American moment + /// + public int Mileage { get; set; } + /// + /// Wtf is a kilometer? + /// + public decimal Gallons { get; set; } + public decimal Cost { get; set; } + public List Files { get; set; } = new List(); + public GasRecord ToGasRecord() { return new GasRecord { Id = Id, Cost = Cost, Date = DateTime.Parse(Date), Gallons = Gallons, Mileage = Mileage, VehicleId = VehicleId, Files = Files }; } + } +} diff --git a/Models/GasRecordViewModel.cs b/Models/GasRecordViewModel.cs new file mode 100644 index 0000000..0131d7d --- /dev/null +++ b/Models/GasRecordViewModel.cs @@ -0,0 +1,21 @@ +namespace CarCareTracker.Models +{ + public class GasRecordViewModel + { + public int Id { get; set; } + public int VehicleId { get; set; } + public string Date { get; set; } + /// + /// American moment + /// + public int Mileage { get; set; } + /// + /// Wtf is a kilometer? + /// + public decimal Gallons { get; set; } + public decimal Cost { get; set; } + public int DeltaMileage { get; set; } + public decimal MilesPerGallon { get; set; } + public decimal CostPerGallon { get; set; } + } +} diff --git a/Program.cs b/Program.cs index 4baec00..7fb0e27 100644 --- a/Program.cs +++ b/Program.cs @@ -9,6 +9,7 @@ builder.Services.AddControllersWithViews(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml new file mode 100644 index 0000000..9b6e32d --- /dev/null +++ b/Views/Vehicle/_Gas.cshtml @@ -0,0 +1,54 @@ +@model List +
+
+
+ @($"# of Gas Records: {Model.Count()}") + @($"Average MPG: {Model.Where(y=>y.MilesPerGallon > 0).Average(x => x.MilesPerGallon)}") + @($"Min MPG: {Model.Min(x => x.MilesPerGallon)}") + @($"Max MPG: {Model.Max(x => x.MilesPerGallon)}") + @($"Total Gallons: {Model.Sum(x=>x.Gallons)}") + @($"Total Cost: {Model.Sum(x => x.Cost).ToString("C")}") +
+
+ +
+
+
+
+
+ + + + + + + + + + + + + @foreach (GasRecordViewModel gasRecord in Model) + { + + + + + + + + + } + +
Date RefueledMileageGallonsMPGCostUnit Cost
@gasRecord.Date@gasRecord.Mileage@gasRecord.Gallons@gasRecord.MilesPerGallon@gasRecord.Cost.ToString("C")@gasRecord.CostPerGallon
+
+
+ + + \ No newline at end of file diff --git a/Views/Vehicle/_GasModal.cshtml b/Views/Vehicle/_GasModal.cshtml new file mode 100644 index 0000000..50f05ab --- /dev/null +++ b/Views/Vehicle/_GasModal.cshtml @@ -0,0 +1,77 @@ +@model GasRecordInput + + + + \ No newline at end of file diff --git a/Views/Vehicle/_ServiceRecordModal.cshtml b/Views/Vehicle/_ServiceRecordModal.cshtml index 9860f8d..7e37a24 100644 --- a/Views/Vehicle/_ServiceRecordModal.cshtml +++ b/Views/Vehicle/_ServiceRecordModal.cshtml @@ -1,6 +1,6 @@ @model ServiceRecordInput } - + } else { - + } @@ -57,11 +57,11 @@ @if (Model.Id == 0) { - + } else if (Model.Id > 0) { - + }