From 6af0d8b88e0a6d0e68aed38dfee1032c3007f29f Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Mon, 19 Feb 2024 22:32:07 -0700 Subject: [PATCH] added functionality to delete records in bulk. --- Controllers/VehicleController.cs | 89 ++++++++++++++++++++------ Views/Vehicle/_CollisionRecords.cshtml | 2 + Views/Vehicle/_Gas.cshtml | 6 +- Views/Vehicle/_Notes.cshtml | 8 ++- Views/Vehicle/_OdometerRecords.cshtml | 8 ++- Views/Vehicle/_ReminderRecords.cshtml | 8 ++- Views/Vehicle/_ServiceRecords.cshtml | 2 + Views/Vehicle/_SupplyRecords.cshtml | 8 ++- Views/Vehicle/_TaxRecords.cshtml | 8 ++- Views/Vehicle/_UpgradeRecords.cshtml | 2 + wwwroot/js/vehicle.js | 70 ++++++++++++++++++-- 11 files changed, 174 insertions(+), 37 deletions(-) diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 08da45b..2579b23 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -165,18 +165,21 @@ namespace CarCareTracker.Controllers var sourceCollaborators = _userLogic.GetCollaboratorsForVehicle(sourceVehicleId).Select(x => x.UserVehicle.UserId).ToList(); var destCollaborators = _userLogic.GetCollaboratorsForVehicle(destVehicleId).Select(x => x.UserVehicle.UserId).ToList(); sourceCollaborators.RemoveAll(x => destCollaborators.Contains(x)); - if (sourceCollaborators.Any()) { + if (sourceCollaborators.Any()) + { foreach (int collaboratorId in sourceCollaborators) { _userLogic.AddUserAccessToVehicle(collaboratorId, destVehicleId); } - } else + } + else { return Json(new OperationResponse { Success = false, Message = "Both vehicles already have identical collaborators" }); } } - return Json(new OperationResponse { Success = true, Message = "Collaborators Copied"}); - } catch (Exception ex) + return Json(new OperationResponse { Success = true, Message = "Collaborators Copied" }); + } + catch (Exception ex) { _logger.LogError(ex.Message); return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); @@ -816,7 +819,7 @@ namespace CarCareTracker.Controllers { collisionRecord.RequisitionHistory = RequisitionSupplyRecordsByUsage(collisionRecord.Supplies, DateTime.Parse(collisionRecord.Date), collisionRecord.Description); } - var result = _collisionRecordDataAccess.SaveCollisionRecordToVehicle(collisionRecord.ToCollisionRecord()); + var result = _collisionRecordDataAccess.SaveCollisionRecordToVehicle(collisionRecord.ToCollisionRecord()); return Json(result); } [HttpGet] @@ -875,17 +878,19 @@ namespace CarCareTracker.Controllers var recurringFees = result.Where(x => x.IsRecurring); if (recurringFees.Any()) { - foreach(TaxRecord recurringFee in recurringFees) + foreach (TaxRecord recurringFee in recurringFees) { var newDate = new DateTime(); if (recurringFee.RecurringInterval != ReminderMonthInterval.Other) { newDate = recurringFee.Date.AddMonths((int)recurringFee.RecurringInterval); - } else + } + else { newDate = recurringFee.Date.AddMonths(recurringFee.CustomMonthInterval); } - if (DateTime.Now > newDate){ + if (DateTime.Now > newDate) + { recurringFee.IsRecurring = false; var newRecurringFee = new TaxRecord() { @@ -983,7 +988,7 @@ namespace CarCareTracker.Controllers { MonthName = x.Key.MonthName, Cost = x.Sum(y => y.Cost), - DistanceTraveled = x.Any(y=>y.MinMileage != default) ? x.Max(y=>y.MaxMileage) - x.Where(y=>y.MinMileage != default).Min(y=>y.MinMileage) : 0 + DistanceTraveled = x.Any(y => y.MinMileage != default) ? x.Max(y => y.MaxMileage) - x.Where(y => y.MinMileage != default).Min(y => y.MinMileage) : 0 }).ToList(); //get reminders var reminders = GetRemindersAndUrgency(vehicleId, DateTime.Now); @@ -1035,7 +1040,7 @@ namespace CarCareTracker.Controllers { MonthId = x.Key, MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), - Cost = x.Sum(y=>y.Cost) + Cost = x.Sum(y => y.Cost) }).ToList(); viewModel.FuelMileageForVehicleByMonth = monthlyMileageData; return PartialView("_Report", viewModel); @@ -1106,8 +1111,9 @@ namespace CarCareTracker.Controllers public IActionResult GetVehicleAttachments(int vehicleId, List exportTabs) { List attachmentData = new List(); - if (exportTabs.Contains(ImportMode.ServiceRecord)){ - var records = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId).Where(x=>x.Files.Any()); + if (exportTabs.Contains(ImportMode.ServiceRecord)) + { + var records = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId).Where(x => x.Files.Any()); attachmentData.AddRange(records.Select(x => new GenericReportModel { Date = x.Date, @@ -1174,7 +1180,8 @@ namespace CarCareTracker.Controllers return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); } return Json(new OperationResponse { Success = true, Message = result }); - } else + } + else { return Json(new OperationResponse { Success = false, Message = "No Attachments Found" }); } @@ -1548,7 +1555,7 @@ namespace CarCareTracker.Controllers public IActionResult GetPinnedNotesByVehicleId(int vehicleId) { var result = _noteDataAccess.GetNotesByVehicleId(vehicleId); - result = result.Where(x=>x.Pinned).ToList(); + result = result.Where(x => x.Pinned).ToList(); return Json(result); } [HttpPost] @@ -1598,11 +1605,11 @@ namespace CarCareTracker.Controllers private List RequisitionSupplyRecordsByUsage(List supplyUsage, DateTime dateRequisitioned, string usageDescription) { List results = new List(); - foreach(SupplyUsage supply in supplyUsage) + foreach (SupplyUsage supply in supplyUsage) { //get supply record. var result = _supplyRecordDataAccess.GetSupplyRecordById(supply.SupplyId); - var unitCost = (result.Quantity != 0 ) ? result.Cost / result.Quantity : 0; + var unitCost = (result.Quantity != 0) ? result.Cost / result.Quantity : 0; //deduct quantity used. result.Quantity -= supply.Quantity; //deduct cost. @@ -1741,10 +1748,10 @@ namespace CarCareTracker.Controllers public IActionResult SavePlanRecordTemplateToVehicleId(PlanRecordInput planRecord) { //check if template name already taken. - var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(planRecord.VehicleId).Where(x=>x.Description == planRecord.Description).Any(); + var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(planRecord.VehicleId).Where(x => x.Description == planRecord.Description).Any(); if (existingRecord) { - return Json(new OperationResponse { Success = false, Message = "A template with that description already exists for this vehicle"}); + return Json(new OperationResponse { Success = false, Message = "A template with that description already exists for this vehicle" }); } planRecord.Files = planRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList(); var result = _planRecordTemplateDataAccess.SavePlanRecordTemplateToVehicle(planRecord); @@ -1797,7 +1804,7 @@ namespace CarCareTracker.Controllers { existingRecord.RequisitionHistory = RequisitionSupplyRecordsByUsage(existingRecord.Supplies, DateTime.Parse(existingRecord.DateCreated), existingRecord.Description); } - var result = _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord.ToPlanRecord()); + var result = _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord.ToPlanRecord()); return Json(new OperationResponse { Success = result, Message = result ? "Plan Record Added" : StaticHelper.GenericErrorMessage }); } [HttpGet] @@ -1897,7 +1904,8 @@ namespace CarCareTracker.Controllers { _logger.LogError("Unable to update reminder either because the reminder no longer exists or is no longer recurring"); } - } else + } + else { _logger.LogError("Unable to update reminder because it no longer exists."); } @@ -2044,7 +2052,7 @@ namespace CarCareTracker.Controllers { var genericRecord = new GenericRecord(); bool result = false; - foreach(int recordId in recordIds) + foreach (int recordId in recordIds) { //get switch (source) @@ -2091,6 +2099,45 @@ namespace CarCareTracker.Controllers } return Json(result); } + public IActionResult DeleteRecords(List recordIds, ImportMode importMode) + { + var genericRecord = new GenericRecord(); + bool result = false; + foreach (int recordId in recordIds) + { + switch (importMode) + { + case ImportMode.ServiceRecord: + result = _serviceRecordDataAccess.DeleteServiceRecordById(recordId); + break; + case ImportMode.RepairRecord: + result = _collisionRecordDataAccess.DeleteCollisionRecordById(recordId); + break; + case ImportMode.UpgradeRecord: + result = _upgradeRecordDataAccess.DeleteUpgradeRecordById(recordId); + break; + case ImportMode.GasRecord: + result = _gasRecordDataAccess.DeleteGasRecordById(recordId); + break; + case ImportMode.TaxRecord: + result = _taxRecordDataAccess.DeleteTaxRecordById(recordId); + break; + case ImportMode.SupplyRecord: + result = _supplyRecordDataAccess.DeleteSupplyRecordById(recordId); + break; + case ImportMode.NoteRecord: + result = _noteDataAccess.DeleteNoteById(recordId); + break; + case ImportMode.OdometerRecord: + result = _odometerRecordDataAccess.DeleteOdometerRecordById(recordId); + break; + case ImportMode.ReminderRecord: + result = _reminderRecordDataAccess.DeleteReminderRecordById(recordId); + break; + } + } + return Json(result); + } #endregion } diff --git a/Views/Vehicle/_CollisionRecords.cshtml b/Views/Vehicle/_CollisionRecords.cshtml index d6ea871..25b763f 100644 --- a/Views/Vehicle/_CollisionRecords.cshtml +++ b/Views/Vehicle/_CollisionRecords.cshtml @@ -93,4 +93,6 @@
  • @translator.Translate(userLanguage, "Service Records")
  • @translator.Translate(userLanguage, "Upgrades")
  • +
  • +
  • @translator.Translate(userLanguage, "Delete")
  • \ No newline at end of file diff --git a/Views/Vehicle/_Gas.cshtml b/Views/Vehicle/_Gas.cshtml index 54a25aa..00da712 100644 --- a/Views/Vehicle/_Gas.cshtml +++ b/Views/Vehicle/_Gas.cshtml @@ -112,7 +112,7 @@ @foreach (GasRecordViewModel gasRecord in Model.GasRecords) { - + @gasRecord.Date @gasRecord.Mileage @(gasRecord.DeltaMileage == default ? "---" : gasRecord.DeltaMileage) @@ -136,6 +136,10 @@ + +