From 125bc44d2eed7a7f809f08c337d278b150f3f300 Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Tue, 4 Feb 2025 13:15:57 -0700 Subject: [PATCH] add put and delete api endpoints. --- Controllers/APIController.cs | 101 +++++++++++++++++++++++++++++++++++ Views/API/Index.cshtml | 63 ++++++++++++++++++++++ 2 files changed, 164 insertions(+) diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index 2e8e965..068e787 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -275,6 +275,107 @@ namespace CarCareTracker.Controllers return Json(OperationResponse.Failed(ex.Message)); } } + [HttpDelete] + [Route("/api/vehicle/planrecords/delete")] + public IActionResult DeletePlanRecord(int id) + { + var existingRecord = _planRecordDataAccess.GetPlanRecordById(id); + if (existingRecord == null || existingRecord.Id == default) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Invalid Record Id")); + } + //security check. + if (!_userLogic.UserCanEditVehicle(GetUserID(), existingRecord.VehicleId)) + { + Response.StatusCode = 401; + return Json(OperationResponse.Failed("Access Denied, you don't have access to this vehicle.")); + } + //restore any requisitioned supplies. + if (existingRecord.RequisitionHistory.Any()) + { + _vehicleLogic.RestoreSupplyRecordsByUsage(existingRecord.RequisitionHistory, existingRecord.Description); + } + var result = _planRecordDataAccess.DeletePlanRecordById(existingRecord.Id); + if (result) + { + StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromPlanRecord(existingRecord, "planrecord.delete.api", User.Identity.Name)); + } + return Json(OperationResponse.Conditional(result, "Plan Record Deleted")); + } + [HttpPut] + [Route("/api/vehicle/planrecords/update")] + [Consumes("application/json")] + public IActionResult UpdatePlanRecordJson([FromBody] PlanRecordExportModel input) => UpdatePlanRecord(input); + [HttpPut] + [Route("/api/vehicle/planrecords/update")] + public IActionResult UpdatePlanRecord(PlanRecordExportModel input) + { + if (string.IsNullOrWhiteSpace(input.Id) || + string.IsNullOrWhiteSpace(input.Description) || + string.IsNullOrWhiteSpace(input.Cost) || + string.IsNullOrWhiteSpace(input.Type) || + string.IsNullOrWhiteSpace(input.Priority) || + string.IsNullOrWhiteSpace(input.Progress)) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, Id, Description, Cost, Type, Priority, and Progress cannot be empty.")); + } + bool validType = Enum.TryParse(input.Type, out ImportMode parsedType); + bool validPriority = Enum.TryParse(input.Priority, out PlanPriority parsedPriority); + bool validProgress = Enum.TryParse(input.Progress, out PlanProgress parsedProgress); + if (!validType || !validPriority || !validProgress) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, values for Type(ServiceRecord, RepairRecord, UpgradeRecord), Priority(Critical, Normal, Low), or Progress(Backlog, InProgress, Testing) is invalid.")); + } + if (parsedType != ImportMode.ServiceRecord && parsedType != ImportMode.RepairRecord && parsedType != ImportMode.UpgradeRecord) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, Type can only ServiceRecord, RepairRecord, or UpgradeRecord")); + } + if (parsedProgress == PlanProgress.Done) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, Progress cannot be set to Done.")); + } + try + { + //retrieve existing record + var existingRecord = _planRecordDataAccess.GetPlanRecordById(int.Parse(input.Id)); + if (existingRecord != null && existingRecord.Id == int.Parse(input.Id)) + { + //check if user has access to the vehicleId + if (!_userLogic.UserCanEditVehicle(GetUserID(), existingRecord.VehicleId)) + { + Response.StatusCode = 401; + return Json(OperationResponse.Failed("Access Denied, you don't have access to this vehicle.")); + } + existingRecord.DateModified = DateTime.Now; + existingRecord.Description = input.Description; + existingRecord.Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes; + existingRecord.Cost = decimal.Parse(input.Cost); + existingRecord.ImportMode = parsedType; + existingRecord.Priority = parsedPriority; + existingRecord.Progress = parsedProgress; + existingRecord.Files = input.Files; + existingRecord.ExtraFields = input.ExtraFields; + _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord); + StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromPlanRecord(existingRecord, "planrecord.update.api", User.Identity.Name)); + } + else + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Invalid Record Id")); + } + return Json(OperationResponse.Succeed("Plan Record Updated")); + } + catch (Exception ex) + { + Response.StatusCode = 500; + return Json(OperationResponse.Failed(ex.Message)); + } + } #endregion #region ServiceRecord [TypeFilter(typeof(CollaboratorFilter))] diff --git a/Views/API/Index.cshtml b/Views/API/Index.cshtml index 4000fc1..8477a4e 100644 --- a/Views/API/Index.cshtml +++ b/Views/API/Index.cshtml @@ -187,6 +187,69 @@ vehicleId - Id of Vehicle +
+
+ POST +
+
+ /api/vehicle/planrecords/add +
+
+ Adds Plan Record to the vehicle +
+
+ vehicleId - Id of Vehicle +
+ Body(form-data): {
+ description - Description
+ cost - Cost
+ type - ServiceRecord/RepairRecord/UpgradeRecord
+ priority - Low/Normal/Critical
+ progress - Backlog/InProgress/Testing
+ notes - notes(optional)
+ extrafields - extrafields(optional)
+ files - attachments(optional)
+ } +
+
+
+
+ PUT +
+
+ /api/vehicle/planrecords/update +
+
+ Updates Plan Record +
+
+ Body(form-data): {
+ Id - Id of Plan Record
+ description - Description
+ cost - Cost
+ type - ServiceRecord/RepairRecord/UpgradeRecord
+ priority - Low/Normal/Critical
+ progress - Backlog/InProgress/Testing
+ notes - notes(optional)
+ extrafields - extrafields(optional)
+ files - attachments(optional)
+ } +
+
+
+
+ DELETE +
+
+ /api/vehicle/planrecords/delete +
+
+ Deletes Plan Record +
+
+ Id - Id of Plan Record +
+
GET