From ecd2b83cf0087bf37f556636034a441fa17a1941 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sun, 7 Jan 2024 14:31:04 -0700 Subject: [PATCH] moved electric vehicle flag to vehicle level. --- Controllers/HomeController.cs | 3 +-- Controllers/VehicleController.cs | 16 ++++++++++-- Models/GasRecord/GasRecordInputContainer.cs | 8 ++++++ .../GasRecord/GasRecordViewModelContainer.cs | 8 ++++++ Models/UserConfig.cs | 1 - Models/Vehicle.cs | 1 + Views/Home/_Settings.cshtml | 7 +---- Views/Vehicle/_Gas.cshtml | 20 +++++++------- Views/Vehicle/_GasModal.cshtml | 26 +++++++++---------- Views/Vehicle/_VehicleModal.cshtml | 4 +++ appsettings.json | 3 +-- wwwroot/js/shared.js | 4 ++- 12 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 Models/GasRecord/GasRecordInputContainer.cs create mode 100644 Models/GasRecord/GasRecordViewModelContainer.cs diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 8b30827..dad44ec 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -45,8 +45,7 @@ namespace CarCareTracker.Controllers UseDarkMode = bool.Parse(_config[nameof(UserConfig.UseDarkMode)]), UseMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]), UseDescending = bool.Parse(_config[nameof(UserConfig.UseDescending)]), - EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]), - UsekWh = bool.Parse(_config[nameof(UserConfig.UsekWh)]) + EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]) }; return PartialView("_Settings", userConfig); } diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index d89efc2..138f346 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -303,7 +303,13 @@ namespace CarCareTracker.Controllers { computedResults = computedResults.OrderByDescending(x => DateTime.Parse(x.Date)).ThenByDescending(x => x.Mileage).ToList(); } - return PartialView("_Gas", computedResults); + var vehicleIsElectric = _dataAccess.GetVehicleById(vehicleId).IsElectric; + var viewModel = new GasRecordViewModelContainer() + { + UseKwh = vehicleIsElectric, + GasRecords = computedResults + }; + return PartialView("_Gas", viewModel); } [HttpPost] public IActionResult SaveGasRecordToVehicleId(GasRecordInput gasRecord) @@ -332,7 +338,13 @@ namespace CarCareTracker.Controllers Gallons = result.Gallons, IsFillToFull = result.IsFillToFull }; - return PartialView("_GasModal", convertedResult); + var vehicleIsElectric = _dataAccess.GetVehicleById(convertedResult.VehicleId).IsElectric; + var viewModel = new GasRecordInputContainer() + { + UseKwh = vehicleIsElectric, + GasRecord = convertedResult + }; + return PartialView("_GasModal", viewModel); } [HttpPost] public IActionResult DeleteGasRecordById(int gasRecordId) diff --git a/Models/GasRecord/GasRecordInputContainer.cs b/Models/GasRecord/GasRecordInputContainer.cs new file mode 100644 index 0000000..5d9c88d --- /dev/null +++ b/Models/GasRecord/GasRecordInputContainer.cs @@ -0,0 +1,8 @@ +namespace CarCareTracker.Models +{ + public class GasRecordInputContainer + { + public bool UseKwh { get; set; } + public GasRecordInput GasRecord { get; set; } + } +} diff --git a/Models/GasRecord/GasRecordViewModelContainer.cs b/Models/GasRecord/GasRecordViewModelContainer.cs new file mode 100644 index 0000000..3efd468 --- /dev/null +++ b/Models/GasRecord/GasRecordViewModelContainer.cs @@ -0,0 +1,8 @@ +namespace CarCareTracker.Models +{ + public class GasRecordViewModelContainer + { + public bool UseKwh { get; set; } + public List GasRecords { get; set; } = new List(); + } +} diff --git a/Models/UserConfig.cs b/Models/UserConfig.cs index ef113e7..b3cd48c 100644 --- a/Models/UserConfig.cs +++ b/Models/UserConfig.cs @@ -3,7 +3,6 @@ public class UserConfig { public bool UseDarkMode { get; set; } - public bool UsekWh { get; set; } public bool EnableCsvImports { get; set; } public bool UseMPG { get; set; } public bool UseDescending { get; set; } diff --git a/Models/Vehicle.cs b/Models/Vehicle.cs index 2924195..cd01f98 100644 --- a/Models/Vehicle.cs +++ b/Models/Vehicle.cs @@ -8,5 +8,6 @@ public string Make { get; set; } public string Model { get; set; } public string LicensePlate { get; set; } + public bool IsElectric { get; set; } = false; } } diff --git a/Views/Home/_Settings.cshtml b/Views/Home/_Settings.cshtml index 39778c4..ee79800 100644 --- a/Views/Home/_Settings.cshtml +++ b/Views/Home/_Settings.cshtml @@ -18,10 +18,6 @@ -
- - -
@@ -80,8 +76,7 @@ useDarkMode: $("#enableDarkMode").is(':checked'), enableCsvImports: $("#enableCsvImports").is(':checked'), useMPG: $("#useMPG").is(':checked'), - useDescending: $("#useDescending").is(':checked'), - usekWh: $("#usekWh").is(':checked') + useDescending: $("#useDescending").is(':checked') } $.post('/Home/WriteToSettings', { userConfig: userConfigObject}, function(data){ if (data) { diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index 7bcd2c9..5c07e4a 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -1,8 +1,9 @@ @inject IConfiguration Configuration +@model GasRecordViewModelContainer @{ var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]); var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]); - var useKwh = bool.Parse(Configuration[nameof(UserConfig.UsekWh)]); + var useKwh = Model.UseKwh; string consumptionUnit; string fuelEconomyUnit; if (useKwh) @@ -15,19 +16,18 @@ fuelEconomyUnit = useMPG ? "mpg" : "l/100km"; } } -@model List
- @($"# of Gas Records: {Model.Count()}") - @if (Model.Where(x=>x.MilesPerGallon > 0).Any()) + @($"# of Gas Records: {Model.GasRecords.Count()}") + @if (Model.GasRecords.Where(x => x.MilesPerGallon > 0).Any()) { - @($"Average Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Average(x => x.MilesPerGallon).ToString("F") ?? "0"}") - @($"Min Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Min(x => x.MilesPerGallon).ToString("F") ?? "0"}") - @($"Max Fuel Economy: {Model.Max(x => x.MilesPerGallon).ToString("F") ?? "0"}") + @($"Average Fuel Economy: {Model.GasRecords.Where(y => y.MilesPerGallon > 0)?.Average(x => x.MilesPerGallon).ToString("F") ?? "0"}") + @($"Min Fuel Economy: {Model.GasRecords.Where(y => y.MilesPerGallon > 0)?.Min(x => x.MilesPerGallon).ToString("F") ?? "0"}") + @($"Max Fuel Economy: {Model.GasRecords.Max(x => x.MilesPerGallon).ToString("F") ?? "0"}") } - @($"Total Fuel Consumed: {Model.Sum(x=>x.Gallons).ToString("F")}") - @($"Total Cost: {Model.Sum(x => x.Cost).ToString("C3")}") + @($"Total Fuel Consumed: {Model.GasRecords.Sum(x => x.Gallons).ToString("F")}") + @($"Total Cost: {Model.GasRecords.Sum(x => x.Cost).ToString("C3")}")
@if (enableCsvImports) { @@ -59,7 +59,7 @@ - @foreach (GasRecordViewModel gasRecord in Model) + @foreach (GasRecordViewModel gasRecord in Model.GasRecords) { @gasRecord.Date diff --git a/Views/Vehicle/_GasModal.cshtml b/Views/Vehicle/_GasModal.cshtml index 2a13626..858f7db 100644 --- a/Views/Vehicle/_GasModal.cshtml +++ b/Views/Vehicle/_GasModal.cshtml @@ -1,9 +1,9 @@ @inject IConfiguration Configuration -@model GasRecordInput +@model GasRecordInputContainer @{ var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]); - var useKwh = bool.Parse(Configuration[nameof(UserConfig.UsekWh)]); - var isNew = Model.Id == 0; + var useKwh = Model.UseKwh; + var isNew = Model.GasRecord.Id == 0; string consumptionUnit; if (useKwh) { @@ -26,26 +26,26 @@
- +
- + - +
- +
- +
- @if (Model.Files.Any()) + @if (Model.GasRecord.Files.Any()) {
- @foreach (UploadedFiles filesUploaded in Model.Files) + @foreach (UploadedFiles filesUploaded in Model.GasRecord.Files) {
@filesUploaded.Name @@ -69,7 +69,7 @@