From 5d3746f168c2f57cd6161e613f8274461887274f Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Sat, 26 Apr 2025 08:56:05 -0600 Subject: [PATCH] add POST api endpoint for basic reminders. --- Controllers/APIController.cs | 76 +++++++++++++++++++++++++++++++++++- Logic/VehicleLogic.cs | 4 +- Models/Shared/ImportModel.cs | 1 + 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index 3fbc865..a4a8c5b 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -1516,7 +1516,7 @@ namespace CarCareTracker.Controllers } var currentMileage = _vehicleLogic.GetMaxMileage(vehicleId); var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId); - var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString()}); + var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString(), Tags = string.Join(' ', x.Tags) }); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant")) { return Json(results, StaticHelper.GetInvariantOption()); @@ -1526,6 +1526,80 @@ namespace CarCareTracker.Controllers return Json(results); } } + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/reminders/add")] + [Consumes("application/json")] + public IActionResult AddReminderRecordJson(int vehicleId, [FromBody] ReminderExportModel input) => AddReminderRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/reminders/add")] + public IActionResult AddReminderRecord(int vehicleId, ReminderExportModel input) + { + if (vehicleId == default) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Must provide a valid vehicle id")); + } + if (string.IsNullOrWhiteSpace(input.Description) || + string.IsNullOrWhiteSpace(input.Metric)) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, Description and Metric cannot be empty.")); + } + bool validMetric = Enum.TryParse(input.Metric, out ReminderMetric parsedMetric); + bool validDate = DateTime.TryParse(input.DueDate, out DateTime parsedDate); + bool validOdometer = int.TryParse(input.DueOdometer, out int parsedOdometer); + if (!validMetric) + { + Response.StatusCode = 400; + return Json(OperationResponse.Failed("Input object invalid, values for Metric(Date, Odometer, Both) is invalid.")); + } + //validate metrics + switch (parsedMetric) + { + case ReminderMetric.Both: + //validate due date and odometer + if (!validDate || !validOdometer) + { + return Json(OperationResponse.Failed("Input object invalid, DueDate and DueOdometer must be valid if Metric is Both")); + } + break; + case ReminderMetric.Date: + if (!validDate) + { + return Json(OperationResponse.Failed("Input object invalid, DueDate must be valid if Metric is Date")); + } + break; + case ReminderMetric.Odometer: + if (!validOdometer) + { + return Json(OperationResponse.Failed("Input object invalid, DueOdometer must be valid if Metric is Odometer")); + } + break; + } + try + { + var reminderRecord = new ReminderRecord() + { + VehicleId = vehicleId, + Description = input.Description, + Mileage = parsedOdometer, + Date = parsedDate, + Metric = parsedMetric, + Notes = input.Notes, + Tags = string.IsNullOrWhiteSpace(input.Tags) ? new List() : input.Tags.Split(' ').Distinct().ToList() + }; + _reminderRecordDataAccess.SaveReminderRecordToVehicle(reminderRecord); + StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromReminderRecord(reminderRecord, "reminderrecord.add.api", User.Identity.Name)); + return Json(OperationResponse.Succeed("Reminder Record Added")); + } + catch (Exception ex) + { + Response.StatusCode = 500; + return Json(OperationResponse.Failed(ex.Message)); + } + } [HttpGet] [Route("/api/calendar")] public IActionResult Calendar() diff --git a/Logic/VehicleLogic.cs b/Logic/VehicleLogic.cs index 30dc2c1..653e641 100644 --- a/Logic/VehicleLogic.cs +++ b/Logic/VehicleLogic.cs @@ -303,11 +303,11 @@ namespace CarCareTracker.Logic //set next reminder if (results.Any(x => (x.Metric == ReminderMetric.Date || x.Metric == ReminderMetric.Both) && x.Date >= DateTime.Now.Date)) { - resultToAdd.NextReminder = results.Where(x => x.Date >= DateTime.Now.Date).OrderBy(x => x.Date).Select(x => new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString() }).First(); + resultToAdd.NextReminder = results.Where(x => x.Date >= DateTime.Now.Date).OrderBy(x => x.Date).Select(x => new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString(), Tags = string.Join(' ', x.Tags) }).First(); } else if (results.Any(x => (x.Metric == ReminderMetric.Odometer || x.Metric == ReminderMetric.Both) && x.Mileage >= currentMileage)) { - resultToAdd.NextReminder = results.Where(x => x.Mileage >= currentMileage).OrderBy(x => x.Mileage).Select(x => new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString() }).First(); + resultToAdd.NextReminder = results.Where(x => x.Mileage >= currentMileage).OrderBy(x => x.Mileage).Select(x => new ReminderExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString(), Tags = string.Join(' ', x.Tags) }).First(); } apiResult.Add(resultToAdd); } diff --git a/Models/Shared/ImportModel.cs b/Models/Shared/ImportModel.cs index 9533344..a4e5059 100644 --- a/Models/Shared/ImportModel.cs +++ b/Models/Shared/ImportModel.cs @@ -125,6 +125,7 @@ namespace CarCareTracker.Models public string DueDate { get; set; } [JsonConverter(typeof(FromIntOptional))] public string DueOdometer { get; set; } + public string Tags { get; set; } } public class PlanRecordExportModel {