From 0fa59761f8d201683bdf4d46d462b43852fde5f5 Mon Sep 17 00:00:00 2001 From: ivancheahhh Date: Mon, 1 Jan 2024 13:26:40 -0700 Subject: [PATCH] Added edit functionality for service records --- Controllers/VehicleController.cs | 15 ++++++ .../ServiceRecordDataAccess.cs | 8 ++++ .../Interfaces/IServiceRecordDataAccess.cs | 1 + Views/Vehicle/Index.cshtml | 2 +- Views/Vehicle/_ServiceRecords.cshtml | 20 +++++--- wwwroot/js/shared.js | 8 ++++ wwwroot/js/vehicle.js | 48 +++++++++++++++++++ 7 files changed, 95 insertions(+), 7 deletions(-) diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 6903f2f..4b14d40 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -73,5 +73,20 @@ namespace CarCareTracker.Controllers var result = _serviceRecordDataAccess.SaveServiceRecordToVehicle(serviceRecord.ToServiceRecord()); return Json(result); } + [HttpGet] + public IActionResult GetServiceRecordById(int serviceRecordId) + { + var result = _serviceRecordDataAccess.GetServiceRecordById(serviceRecordId); + //convert to Input object. + var convertedResult = new ServiceRecordInput { Id = result.Id, + Cost = result.Cost, + Date = result.Date.ToShortDateString(), + Description = result.Description, + Mileage = result.Mileage, + Notes = result.Notes, + VehicleId = result.VehicleId + }; + return Json(convertedResult); + } } } diff --git a/External/Implementations/ServiceRecordDataAccess.cs b/External/Implementations/ServiceRecordDataAccess.cs index fbc2339..066441b 100644 --- a/External/Implementations/ServiceRecordDataAccess.cs +++ b/External/Implementations/ServiceRecordDataAccess.cs @@ -17,6 +17,14 @@ namespace CarCareTracker.External.Implementations return serviceRecords.ToList() ?? new List(); }; } + public ServiceRecord GetServiceRecordById(int serviceRecordId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + return table.FindById(serviceRecordId); + }; + } public bool SaveServiceRecordToVehicle(ServiceRecord serviceRecord) { using (var db = new LiteDatabase(dbName)) diff --git a/External/Interfaces/IServiceRecordDataAccess.cs b/External/Interfaces/IServiceRecordDataAccess.cs index a2770cf..f82d5bb 100644 --- a/External/Interfaces/IServiceRecordDataAccess.cs +++ b/External/Interfaces/IServiceRecordDataAccess.cs @@ -5,6 +5,7 @@ namespace CarCareTracker.External.Interfaces public interface IServiceRecordDataAccess { public List GetServiceRecordsByVehicleId(int vehicleId); + public ServiceRecord GetServiceRecordById(int serviceRecordId); public bool SaveServiceRecordToVehicle(ServiceRecord serviceRecord); } } diff --git a/Views/Vehicle/Index.cshtml b/Views/Vehicle/Index.cshtml index cdbd773..48aca1b 100644 --- a/Views/Vehicle/Index.cshtml +++ b/Views/Vehicle/Index.cshtml @@ -46,7 +46,7 @@ -
+
111
diff --git a/Views/Vehicle/_ServiceRecords.cshtml b/Views/Vehicle/_ServiceRecords.cshtml index dbb504d..bec4181 100644 --- a/Views/Vehicle/_ServiceRecords.cshtml +++ b/Views/Vehicle/_ServiceRecords.cshtml @@ -4,22 +4,29 @@ - + + - @foreach(ServiceRecord serviceRecord in Model) + @foreach (ServiceRecord serviceRecord in Model) { - - + + - + + } @@ -54,7 +61,8 @@ diff --git a/wwwroot/js/shared.js b/wwwroot/js/shared.js index 40c7395..2087d96 100644 --- a/wwwroot/js/shared.js +++ b/wwwroot/js/shared.js @@ -27,4 +27,12 @@ function errorToast(message) { toast.onmouseleave = Swal.resumeTimer; } }) +} +function genericSwal(title, message) { + Swal.fire({ + showConfirmButton: true, + title: title, + text: message, + icon: "info" + }) } \ No newline at end of file diff --git a/wwwroot/js/vehicle.js b/wwwroot/js/vehicle.js index 96cfe25..92f0b48 100644 --- a/wwwroot/js/vehicle.js +++ b/wwwroot/js/vehicle.js @@ -49,12 +49,53 @@ function DeleteVehicle(vehicleId) { } }) } +var serviceRecordEditId = 0; +function showEditServiceRecordModal(serviceRecordId) { + //retrieve service record object. + $.get(`/Vehicle/GetServiceRecordById?serviceRecordId=${serviceRecordId}`, function (data) { + if (data) { + //UI elements. + $("#addServiceRecordButton").hide(); + $("#editServiceRecordButton").show(); + //pre-populate fields. + $("#serviceRecordDate").val(data.date); + $("#serviceRecordMileage").val(data.mileage); + $("#serviceRecordDescription").val(data.description); + $("#serviceRecordCost").val(data.cost); + $("#serviceRecordNotes").val(data.notes); + serviceRecordEditId = serviceRecordId; //set global var. + $('#addServiceRecordModal').modal('show'); + } + }); +} function showAddServiceRecordModal() { + serviceRecordEditId = 0; + $("#addServiceRecordButton").show(); + $("#editServiceRecordButton").hide(); $('#addServiceRecordModal').modal('show'); } function hideAddServiceRecordModal() { + serviceRecordEditId = 0; $('#addServiceRecordModal').modal('hide'); } +function editServiceRecordToVehicle() { + var formValues = getAndValidateServiceRecordValues(); + //validate + if (formValues.hasError) { + errorToast("Please check the form data"); + return; + } + //save to db. + $.post('/Vehicle/SaveServiceRecordToVehicleId', { serviceRecord: formValues }, function (data) { + if (data) { + successToast("Service Record updated."); + hideAddServiceRecordModal(); + getVehicleServiceRecords(formValues.vehicleId); + } else { + errorToast("An error has occurred, please try again later."); + } + }) +} function addServiceRecordToVehicle() { //get values var formValues = getAndValidateServiceRecordValues(); @@ -108,6 +149,7 @@ function getAndValidateServiceRecordValues() { $("#serviceRecordCost").removeClass("is-invalid"); } return { + id: serviceRecordEditId, hasError: hasError, vehicleId: vehicleId, date: serviceDate, @@ -116,4 +158,10 @@ function getAndValidateServiceRecordValues() { cost: serviceCost, notes: serviceNotes } +} +function showServiceRecordNotes(note) { + if (note.trim() == '') { + return; + } + genericSwal("Note", note); } \ No newline at end of file
DateDate Mileage Description Cost NotesActions
@serviceRecord.Date.ToShortDateString()@serviceRecord.Mileage@serviceRecord.Date.ToShortDateString()@serviceRecord.Mileage @serviceRecord.Description @serviceRecord.Cost.ToString("C")@serviceRecord.Notes@serviceRecord.Notes +
+ + +
+