use settings for defining sort order and add methods to calculate metric fuel consumption.

This commit is contained in:
ivancheahhh
2024-01-04 14:12:30 -07:00
parent 98f14c1cc0
commit 3306fefd4d
8 changed files with 68 additions and 26 deletions

View File

@@ -20,6 +20,8 @@ namespace CarCareTracker.Controllers
private readonly ICollisionRecordDataAccess _collisionRecordDataAccess;
private readonly ITaxRecordDataAccess _taxRecordDataAccess;
private readonly IWebHostEnvironment _webEnv;
private readonly bool _useDescending;
private readonly IConfiguration _config;
private readonly IFileHelper _fileHelper;
public VehicleController(ILogger<HomeController> logger,
@@ -30,7 +32,8 @@ namespace CarCareTracker.Controllers
IGasRecordDataAccess gasRecordDataAccess,
ICollisionRecordDataAccess collisionRecordDataAccess,
ITaxRecordDataAccess taxRecordDataAccess,
IWebHostEnvironment webEnv)
IWebHostEnvironment webEnv,
IConfiguration config)
{
_logger = logger;
_dataAccess = dataAccess;
@@ -41,6 +44,8 @@ namespace CarCareTracker.Controllers
_collisionRecordDataAccess = collisionRecordDataAccess;
_taxRecordDataAccess = taxRecordDataAccess;
_webEnv = webEnv;
_config = config;
_useDescending = bool.Parse(config[nameof(UserConfig.UseDescending)]);
}
[HttpGet]
public IActionResult Index(int vehicleId)
@@ -168,6 +173,10 @@ namespace CarCareTracker.Controllers
public IActionResult GetGasRecordsByVehicleId(int vehicleId)
{
var result = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
//need it in ascending order to perform computation.
result = result.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList();
//check if the user uses MPG or Liters per 100km.
bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]);
var computedResults = new List<GasRecordViewModel>();
int previousMileage = 0;
//perform computation.
@@ -186,7 +195,7 @@ namespace CarCareTracker.Controllers
Gallons = currentObject.Gallons,
Cost = currentObject.Cost,
DeltaMileage = deltaMileage,
MilesPerGallon = deltaMileage / currentObject.Gallons,
MilesPerGallon = useMPG ? (deltaMileage / currentObject.Gallons) : 100 / (deltaMileage / currentObject.Gallons),
CostPerGallon = (currentObject.Cost / currentObject.Gallons)
});
} else
@@ -206,6 +215,10 @@ namespace CarCareTracker.Controllers
}
previousMileage = result[i].Mileage;
}
if (_useDescending)
{
computedResults = computedResults.OrderByDescending(x => DateTime.Parse(x.Date)).ThenByDescending(x => x.Mileage).ToList();
}
return PartialView("_Gas", computedResults);
}
[HttpPost]
@@ -248,6 +261,14 @@ namespace CarCareTracker.Controllers
public IActionResult GetServiceRecordsByVehicleId(int vehicleId)
{
var result = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId);
if (_useDescending)
{
result = result.OrderByDescending(x => x.Date).ThenByDescending(x => x.Mileage).ToList();
}
else
{
result = result.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList();
}
return PartialView("_ServiceRecords", result);
}
[HttpPost]
@@ -291,6 +312,14 @@ namespace CarCareTracker.Controllers
public IActionResult GetCollisionRecordsByVehicleId(int vehicleId)
{
var result = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId);
if (_useDescending)
{
result = result.OrderByDescending(x => x.Date).ThenByDescending(x => x.Mileage).ToList();
}
else
{
result = result.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList();
}
return PartialView("_CollisionRecords", result);
}
[HttpPost]
@@ -336,6 +365,14 @@ namespace CarCareTracker.Controllers
public IActionResult GetTaxRecordsByVehicleId(int vehicleId)
{
var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
if (_useDescending)
{
result = result.OrderByDescending(x => x.Date).ToList();
}
else
{
result = result.OrderBy(x => x.Date).ToList();
}
return PartialView("_TaxRecords", result);
}
[HttpPost]
@@ -382,24 +419,18 @@ namespace CarCareTracker.Controllers
return PartialView("_Report");
}
[HttpGet]
public IActionResult GetCostMakeUpForVehicle(int vehicleId, string startDate = "", string endDate = "")
public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0)
{
var serviceRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId);
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
var collisionRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId);
var taxRecords = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
if (!string.IsNullOrWhiteSpace(startDate) &&
!string.IsNullOrWhiteSpace(endDate) &&
DateTime.TryParse(startDate, out DateTime parsedStartDate) &&
DateTime.TryParse(endDate, out DateTime parsedEndDate)
)
if (year != default)
{
parsedEndDate = parsedEndDate.AddDays(1).AddSeconds(-1);
//if start and end dates are provided then we need to filter the data down.
serviceRecords.RemoveAll(x => x.Date < parsedStartDate || x.Date > parsedEndDate);
gasRecords.RemoveAll(x => x.Date < parsedStartDate || x.Date > parsedEndDate);
collisionRecords.RemoveAll(x => x.Date < parsedStartDate || x.Date > parsedEndDate);
taxRecords.RemoveAll(x => x.Date < parsedStartDate || x.Date > parsedEndDate);
serviceRecords.RemoveAll(x => x.Date.Year != year);
gasRecords.RemoveAll(x => x.Date.Year != year);
collisionRecords.RemoveAll(x => x.Date.Year != year);
taxRecords.RemoveAll(x => x.Date.Year != year);
}
var viewModel = new CostMakeUpForVehicle
{

View File

@@ -13,7 +13,7 @@ namespace CarCareTracker.External.Implementations
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<CollisionRecord>(tableName);
var collisionRecords = table.Find(Query.EQ(nameof(CollisionRecord.VehicleId), vehicleId)).OrderBy(x => x.Date).ThenBy(x=>x.Mileage);
var collisionRecords = table.Find(Query.EQ(nameof(CollisionRecord.VehicleId), vehicleId));
return collisionRecords.ToList() ?? new List<CollisionRecord>();
};
}

View File

@@ -13,7 +13,7 @@ namespace CarCareTracker.External.Implementations
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<GasRecord>(tableName);
var gasRecords = table.Find(Query.EQ(nameof(GasRecord.VehicleId), vehicleId)).OrderBy(x => x.Date).ThenBy(x=>x.Mileage);
var gasRecords = table.Find(Query.EQ(nameof(GasRecord.VehicleId), vehicleId));
return gasRecords.ToList() ?? new List<GasRecord>();
};
}

View File

@@ -13,7 +13,7 @@ namespace CarCareTracker.External.Implementations
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<ServiceRecord>(tableName);
var serviceRecords = table.Find(Query.EQ(nameof(ServiceRecord.VehicleId), vehicleId)).OrderBy(x => x.Date).ThenBy(x => x.Mileage);
var serviceRecords = table.Find(Query.EQ(nameof(ServiceRecord.VehicleId), vehicleId));
return serviceRecords.ToList() ?? new List<ServiceRecord>();
};
}

View File

@@ -13,7 +13,7 @@ namespace CarCareTracker.External.Implementations
using (var db = new LiteDatabase(dbName))
{
var table = db.GetCollection<TaxRecord>(tableName);
var taxRecords = table.Find(Query.EQ(nameof(TaxRecord.VehicleId), vehicleId)).OrderBy(x => x.Date);
var taxRecords = table.Find(Query.EQ(nameof(TaxRecord.VehicleId), vehicleId));
return taxRecords.ToList() ?? new List<TaxRecord>();
};
}

View File

@@ -36,8 +36,14 @@ namespace CarCareTracker.Middleware
return AuthenticateResult.Success(ticket);
} else
{
//auth is enabled by user, we will have to authenticate the user via a ticket.
return AuthenticateResult.Fail("Invalid credentials");
}
}
protected override Task HandleChallengeAsync(AuthenticationProperties properties)
{
Response.Redirect("/Login/Index");
return Task.CompletedTask;
}
}
}

View File

@@ -1,6 +1,7 @@
@inject IConfiguration Configuration
@{
var enableCsvImports = bool.Parse(Configuration["EnableCsvImports"]);
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]);
}
@model List<GasRecordViewModel>
<div class="row">
@@ -38,9 +39,9 @@
<thead>
<tr class="d-flex">
<th scope="col" class="col-2">Date Refueled</th>
<th scope="col" class="col-2">Odometer</th>
<th scope="col" class="col-2">Consumption</th>
<th scope="col" class="col-2">Fuel Economy</th>
<th scope="col" class="col-2">Odometer(@(useMPG ? "mi." : "km"))</th>
<th scope="col" class="col-2">Consumption(@(useMPG ? "gal" : "l"))</th>
<th scope="col" class="col-2">Fuel Economy(@(useMPG ? "mpg" : "l/100km"))</th>
<th scope="col" class="col-2">Cost</th>
<th scope="col" class="col-2">Unit Cost</th>
</tr>
@@ -54,7 +55,7 @@
<td class="col-2">@gasRecord.Gallons.ToString("F")</td>
<td class="col-2">@gasRecord.MilesPerGallon.ToString("F")</td>
<td class="col-2">@gasRecord.Cost.ToString("C")</td>
<td class="col-2">@gasRecord.CostPerGallon.ToString("F")</td>
<td class="col-2">@gasRecord.CostPerGallon.ToString("C")</td>
</tr>
}
</tbody>

View File

@@ -1,4 +1,8 @@
@model GasRecordInput
@inject IConfiguration Configuration
@{
var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]);
}
@model GasRecordInput
<div class="modal-header">
<h5 class="modal-title">@(Model.Id == 0 ? "Add New Gas Record" : "Edit Gas Record")</h5>
<button type="button" class="btn-close" onclick="hideAddGasRecordModal()" aria-label="Close"></button>
@@ -14,9 +18,9 @@
<input type="text" id="gasRecordDate" class="form-control" value="@Model.Date">
<span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
</div>
<label for="gasRecordMileage">Odometer Reading</label>
<label for="gasRecordMileage">Odometer Reading(@(useMPG ? "miles" : "kilometers"))</label>
<input type="number" id="gasRecordMileage" class="form-control" value="@Model.Mileage">
<label for="gasRecordGallons">Fuel Consumption(gals/litres)</label>
<label for="gasRecordGallons">Fuel Consumption(@(useMPG ? "gallons" : "liters"))</label>
<input type="text" id="gasRecordGallons" class="form-control" value="@Model.Gallons">
<label for="GasRecordCost">Cost</label>
<input type="number" id="gasRecordCost" class="form-control" value="@Model.Cost">