From 12dff07e19b39910cb6ff3e5ff807a8f55663089 Mon Sep 17 00:00:00 2001 From: ivancheahhh Date: Wed, 3 Jan 2024 06:45:39 -0700 Subject: [PATCH] Full functionality for gas records. --- Controllers/VehicleController.cs | 22 ++++++ Views/Home/_GarageDisplay.cshtml | 2 +- Views/Vehicle/_Gas.cshtml | 2 +- wwwroot/js/gasrecord.js | 119 +++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 wwwroot/js/gasrecord.js diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 2fed8c8..984b8df 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -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] diff --git a/Views/Home/_GarageDisplay.cshtml b/Views/Home/_GarageDisplay.cshtml index e397d2a..2cec156 100644 --- a/Views/Home/_GarageDisplay.cshtml +++ b/Views/Home/_GarageDisplay.cshtml @@ -6,7 +6,7 @@ {
- +
@($"{vehicle.Year}")
@($"{vehicle.Make}")
diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index 4656a9c..7909a96 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -33,7 +33,7 @@ @foreach (GasRecordViewModel gasRecord in Model) { - + @gasRecord.Date @gasRecord.Mileage @gasRecord.Gallons.ToString("F") diff --git a/wwwroot/js/gasrecord.js b/wwwroot/js/gasrecord.js new file mode 100644 index 0000000..06809bc --- /dev/null +++ b/wwwroot/js/gasrecord.js @@ -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); +} \ No newline at end of file