Skip to content

Commit

Permalink
Merge pull request #18 from hargata/electric.vehicle
Browse files Browse the repository at this point in the history
moved electric vehicle flag to vehicle level.
  • Loading branch information
hargata authored Jan 7, 2024
2 parents 80504e7 + ecd2b83 commit 54d20b5
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 37 deletions.
3 changes: 1 addition & 2 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public IActionResult Settings()
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);
}
Expand Down
16 changes: 14 additions & 2 deletions Controllers/VehicleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,13 @@ public IActionResult GetGasRecordsByVehicleId(int vehicleId)
{
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)
Expand Down Expand Up @@ -332,7 +338,13 @@ public IActionResult GetGasRecordForEditById(int gasRecordId)
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)
Expand Down
8 changes: 8 additions & 0 deletions Models/GasRecord/GasRecordInputContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CarCareTracker.Models
{
public class GasRecordInputContainer
{
public bool UseKwh { get; set; }
public GasRecordInput GasRecord { get; set; }
}
}
8 changes: 8 additions & 0 deletions Models/GasRecord/GasRecordViewModelContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CarCareTracker.Models
{
public class GasRecordViewModelContainer
{
public bool UseKwh { get; set; }
public List<GasRecordViewModel> GasRecords { get; set; } = new List<GasRecordViewModel>();
}
}
1 change: 0 additions & 1 deletion Models/UserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions Models/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class Vehicle
public string Make { get; set; }
public string Model { get; set; }
public string LicensePlate { get; set; }
public bool IsElectric { get; set; } = false;
}
}
7 changes: 1 addition & 6 deletions Views/Home/_Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<input class="form-check-input" onChange="updateSettings()" type="checkbox" role="switch" id="useMPG" checked="@Model.UseMPG">
<label class="form-check-label" for="useMPG">Use Imperial Calculation for Fuel Economy Calculations(MPG)<br /><small class="text-body-secondary">This Will Also Change Units to Miles and Gallons</small></label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" onChange="updateSettings()" type="checkbox" role="switch" id="usekWh" checked="@Model.UsekWh">
<label class="form-check-label" for="usekWh">Electric Car(Gas Consumption Units will be replaced with kWh)</label>
</div>
</div>
<div class="col-12 col-md-6">
<div class="form-check form-switch">
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions Views/Vehicle/_Gas.cshtml
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -15,19 +16,18 @@
fuelEconomyUnit = useMPG ? "mpg" : "l/100km";
}
}
@model List<GasRecordViewModel>
<div class="row">
<div class="d-flex justify-content-between">
<div class="d-flex align-items-center flex-wrap">
<span class="ms-2 badge bg-success">@($"# of Gas Records: {Model.Count()}")</span>
@if (Model.Where(x=>x.MilesPerGallon > 0).Any())
<span class="ms-2 badge bg-success">@($"# of Gas Records: {Model.GasRecords.Count()}")</span>
@if (Model.GasRecords.Where(x => x.MilesPerGallon > 0).Any())
{
<span class="ms-2 badge bg-primary">@($"Average Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Average(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Min Fuel Economy: {Model.Where(y => y.MilesPerGallon > 0)?.Min(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Max Fuel Economy: {Model.Max(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Average Fuel Economy: {Model.GasRecords.Where(y => y.MilesPerGallon > 0)?.Average(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Min Fuel Economy: {Model.GasRecords.Where(y => y.MilesPerGallon > 0)?.Min(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
<span class="ms-2 badge bg-primary">@($"Max Fuel Economy: {Model.GasRecords.Max(x => x.MilesPerGallon).ToString("F") ?? "0"}")</span>
}
<span class="ms-2 badge bg-success">@($"Total Fuel Consumed: {Model.Sum(x=>x.Gallons).ToString("F")}")</span>
<span class="ms-2 badge bg-success">@($"Total Cost: {Model.Sum(x => x.Cost).ToString("C3")}")</span>
<span class="ms-2 badge bg-success">@($"Total Fuel Consumed: {Model.GasRecords.Sum(x => x.Gallons).ToString("F")}")</span>
<span class="ms-2 badge bg-success">@($"Total Cost: {Model.GasRecords.Sum(x => x.Cost).ToString("C3")}")</span>
</div>
@if (enableCsvImports)
{
Expand Down Expand Up @@ -59,7 +59,7 @@
</tr>
</thead>
<tbody>
@foreach (GasRecordViewModel gasRecord in Model)
@foreach (GasRecordViewModel gasRecord in Model.GasRecords)
{
<tr class="d-flex" style="cursor:pointer;" onclick="showEditGasRecordModal(@gasRecord.Id)">
<td class="col-2">@gasRecord.Date</td>
Expand Down
26 changes: 13 additions & 13 deletions Views/Vehicle/_GasModal.cshtml
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -26,26 +26,26 @@
<input type="text" id="workAroundInput" style="height:0px; width:0px; display:none;">
<label for="gasRecordDate">Date</label>
<div class="input-group">
<input type="text" id="gasRecordDate" placeholder="Date refueled" class="form-control" value="@Model.Date">
<input type="text" id="gasRecordDate" placeholder="Date refueled" class="form-control" value="@Model.GasRecord.Date">
<span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
</div>
<label for="gasRecordMileage">Odometer Reading(@(useMPG ? "miles" : "kilometers"))</label>
<input type="number" id="gasRecordMileage" class="form-control" placeholder="Odometer reading when refueled" value="@(isNew ? "" : Model.Mileage)">
<input type="number" id="gasRecordMileage" class="form-control" placeholder="Odometer reading when refueled" value="@(isNew ? "" : Model.GasRecord.Mileage)">
<label for="gasRecordGallons">Fuel Consumption(@(consumptionUnit))</label>
<input type="text" id="gasRecordGallons" class="form-control" placeholder="Amount of gas refueled" value="@(isNew ? "" : Model.Gallons)">
<input type="text" id="gasRecordGallons" class="form-control" placeholder="Amount of gas refueled" value="@(isNew ? "" : Model.GasRecord.Gallons)">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="gasIsFillToFull" checked="@Model.IsFillToFull">
<input class="form-check-input" type="checkbox" role="switch" id="gasIsFillToFull" checked="@Model.GasRecord.IsFillToFull">
<label class="form-check-label" for="gasIsFillToFull">Is Filled To Full</label>
</div>
<label for="GasRecordCost">Cost</label>
<input type="number" id="gasRecordCost" class="form-control" placeholder="Cost of gas refueled" value="@(isNew ? "" : Model.Cost)">
<input type="number" id="gasRecordCost" class="form-control" placeholder="Cost of gas refueled" value="@(isNew ? "" : Model.GasRecord.Cost)">
</div>
<div class="col-md-6 col-12">
@if (Model.Files.Any())
@if (Model.GasRecord.Files.Any())
{
<div>
<label>Uploaded Documents</label>
@foreach (UploadedFiles filesUploaded in Model.Files)
@foreach (UploadedFiles filesUploaded in Model.GasRecord.Files)
{
<div class="d-flex justify-content-between">
<a type="button" class="btn btn-link" href="@filesUploaded.Location" target="_blank">@filesUploaded.Name</a>
Expand All @@ -69,7 +69,7 @@
<div class="modal-footer">
@if (!isNew)
{
<button type="button" class="btn btn-danger" onclick="deleteGasRecord(@Model.Id)" style="margin-right:auto;">Delete</button>
<button type="button" class="btn btn-danger" onclick="deleteGasRecord(@Model.GasRecord.Id)" style="margin-right:auto;">Delete</button>
}
<button type="button" class="btn btn-secondary" onclick="hideAddGasRecordModal()">Cancel</button>
@if (isNew)
Expand All @@ -85,12 +85,12 @@
var uploadedFiles = [];
getUploadedFilesFromModel();
function getUploadedFilesFromModel() {
@foreach (UploadedFiles filesUploaded in Model.Files)
@foreach (UploadedFiles filesUploaded in Model.GasRecord.Files)
{
@:uploadedFiles.push({ name: "@filesUploaded.Name", location: "@filesUploaded.Location" });
}
}
function getGasRecordModelData(){
return {id: @Model.Id}
return { id: @Model.GasRecord.Id}
}
</script>
4 changes: 4 additions & 0 deletions Views/Vehicle/_VehicleModal.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<input type="text" id="inputModel" class="form-control" placeholder="Model" value="@Model.Model">
<label for="inputLicensePlate">License Plate</label>
<input type="text" id="inputLicensePlate" class="form-control" placeholder="License Plate" value="@Model.LicensePlate">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="inputIsElectric" checked="@Model.IsElectric">
<label class="form-check-label" for="inputIsElectric">Electric Vehicle</label>
</div>
@if (!string.IsNullOrWhiteSpace(Model.ImageLocation))
{
<label for="inputImage">Replace picture(optional)</label>
Expand Down
3 changes: 1 addition & 2 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"UseDescending": false,
"EnableAuth": false,
"UserNameHash": "",
"UserPasswordHash": "",
"UsekWh": false
"UserPasswordHash": ""
}
4 changes: 3 additions & 1 deletion wwwroot/js/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function saveVehicle(isEdit) {
var vehicleMake = $("#inputMake").val();
var vehicleModel = $("#inputModel").val();
var vehicleLicensePlate = $("#inputLicensePlate").val();
var vehicleIsElectric = $("#inputIsElectric").is(":checked");
//validate
var hasError = false;
if (vehicleYear.trim() == '' || parseInt(vehicleYear) < 1900) {
Expand Down Expand Up @@ -72,7 +73,8 @@ function saveVehicle(isEdit) {
year: vehicleYear,
make: vehicleMake,
model: vehicleModel,
licensePlate: vehicleLicensePlate
licensePlate: vehicleLicensePlate,
isElectric: vehicleIsElectric
}, function (data) {
if (data) {
if (!isEdit) {
Expand Down

0 comments on commit 54d20b5

Please sign in to comment.