Full functionality for gas records.

This commit is contained in:
ivancheahhh
2024-01-03 06:45:39 -07:00
parent 8dfaf49e05
commit 12dff07e19
4 changed files with 143 additions and 2 deletions

View File

@@ -129,6 +129,28 @@ namespace CarCareTracker.Controllers
{
return PartialView("_GasModal", new GasRecordInput());
}
[HttpGet]
public IActionResult GetGasRecordForEditById(int gasRecordId)
{
var result = _gasRecordDataAccess.GetGasRecordById(gasRecordId);
var convertedResult = new GasRecordInput
{
Id = result.Id,
Mileage = result.Mileage,
VehicleId = result.VehicleId,
Cost = result.Cost,
Date = result.Date.ToShortDateString(),
Files = result.Files,
Gallons = result.Gallons
};
return PartialView("_GasModal", convertedResult);
}
[HttpPost]
public IActionResult DeleteGasRecordById(int gasRecordId)
{
var result = _gasRecordDataAccess.DeleteGasRecordById(gasRecordId);
return Json(result);
}
#endregion
#region "Service Records"
[HttpGet]

View File

@@ -6,7 +6,7 @@
{
<div class="col-xl-2 col-lg-4 col-md-4 col-sm-4 col-4">
<div class="card" onclick="viewVehicle(@vehicle.Id)">
<img src="@vehicle.ImageLocation" style="height:145px" />
<img src="@vehicle.ImageLocation" style="height:145px; object-fit:scale-down;" />
<div class="card-body">
<h5 class="card-title text-truncate">@($"{vehicle.Year}")</h5>
<h5 class="card-title text-truncate">@($"{vehicle.Make}")</h5>

View File

@@ -33,7 +33,7 @@
<tbody>
@foreach (GasRecordViewModel gasRecord in Model)
{
<tr class="d-flex" style="cursor:pointer;" onclick="">
<tr class="d-flex" style="cursor:pointer;" onclick="showEditGasRecordModal(@gasRecord.Id)">
<td class="col-2">@gasRecord.Date</td>
<td class="col-2">@gasRecord.Mileage</td>
<td class="col-2">@gasRecord.Gallons.ToString("F")</td>

119
wwwroot/js/gasrecord.js Normal file
View File

@@ -0,0 +1,119 @@
function showAddGasRecordModal() {
$.get('/Vehicle/GetAddGasRecordPartialView', function (data) {
if (data) {
$("#gasRecordModalContent").html(data);
//initiate datepicker
$('#gasRecordDate').datepicker({
endDate: "+0d"
});
$('#gasRecordModal').modal('show');
}
});
}
function showEditGasRecordModal(gasRecordId) {
$.get(`/Vehicle/GetGasRecordForEditById?gasRecordId=${gasRecordId}`, function (data) {
if (data) {
$("#gasRecordModalContent").html(data);
//initiate datepicker
$('#gasRecordDate').datepicker({
endDate: "+0d"
});
$('#gasRecordModal').modal('show');
}
});
}
function hideAddGasRecordModal() {
$('#gasRecordModal').modal('hide');
}
function deleteGasRecord(gasRecordId) {
$("#workAroundInput").show();
Swal.fire({
title: "Confirm Deletion?",
text: "Deleted Gas Records cannot be restored.",
showCancelButton: true,
confirmButtonText: "Delete",
confirmButtonColor: "#dc3545"
}).then((result) => {
if (result.isConfirmed) {
$.post(`/Vehicle/DeleteGasRecordById?gasRecordId=${gasRecordId}`, function (data) {
if (data) {
hideAddGasRecordModal();
successToast("Gas Record deleted");
var vehicleId = GetVehicleId().vehicleId;
getVehicleGasRecords(vehicleId);
} else {
errorToast("An error has occurred, please try again later.");
}
});
} else {
$("#workAroundInput").hide();
}
});
}
function saveGasRecordToVehicle(isEdit) {
//get values
var formValues = getAndValidateGasRecordValues();
//validate
if (formValues.hasError) {
errorToast("Please check the form data");
return;
}
//save to db.
$.post('/Vehicle/SaveGasRecordToVehicleId', { gasRecord: formValues }, function (data) {
if (data) {
successToast(isEdit ? "Gas Record Updated" : "Gas Record Added.");
hideAddGasRecordModal();
getVehicleGasRecords(formValues.vehicleId);
} else {
errorToast("An error has occurred, please try again later.");
}
})
}
function getAndValidateGasRecordValues() {
var gasDate = $("#gasRecordDate").val();
var gasMileage = $("#gasRecordMileage").val();
var gasGallons = $("#gasRecordGallons").val();
var gasCost = $("#gasRecordCost").val();
var vehicleId = GetVehicleId().vehicleId;
var gasRecordId = getGasRecordModelData().id;
//validation
var hasError = false;
if (gasDate.trim() == '') { //eliminates whitespace.
hasError = true;
$("#gasRecordDate").addClass("is-invalid");
} else {
$("#gasRecordDate").removeClass("is-invalid");
}
if (gasMileage.trim() == '' || parseInt(gasMileage) < 0) {
hasError = true;
$("#gasRecordMileage").addClass("is-invalid");
} else {
$("#gasRecordMileage").removeClass("is-invalid");
}
if (gasGallons.trim() == '' || parseInt(gasGallons) < 0) {
hasError = true;
$("#gasRecordGallons").addClass("is-invalid");
} else {
$("#gasRecordGallons").removeClass("is-invalid");
}
if (gasCost.trim() == '') {
hasError = true;
$("#gasRecordCost").addClass("is-invalid");
} else {
$("#gasRecordCost").removeClass("is-invalid");
}
return {
id: gasRecordId,
hasError: hasError,
vehicleId: vehicleId,
date: gasDate,
mileage: gasMileage,
gallons: gasGallons,
cost: gasCost,
files: uploadedFiles
}
}
function deleteGasRecordFile(fileLocation, event) {
event.parentElement.remove();
uploadedFiles = uploadedFiles.filter(x => x.location != fileLocation);
}