Add POST API Endpoint to add plan records.
This commit is contained in:
@@ -57,7 +57,7 @@ namespace CarCareTracker.Controllers
|
||||
IUserLogic userLogic,
|
||||
IVehicleLogic vehicleLogic,
|
||||
IOdometerLogic odometerLogic,
|
||||
IWebHostEnvironment webEnv)
|
||||
IWebHostEnvironment webEnv)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_noteDataAccess = noteDataAccess;
|
||||
@@ -186,17 +186,17 @@ namespace CarCareTracker.Controllers
|
||||
return Json(response);
|
||||
}
|
||||
var vehicleRecords = _planRecordDataAccess.GetPlanRecordsByVehicleId(vehicleId);
|
||||
var result = vehicleRecords.Select(x => new PlanRecordExportModel {
|
||||
Id = x.Id.ToString(),
|
||||
var result = vehicleRecords.Select(x => new PlanRecordExportModel {
|
||||
Id = x.Id.ToString(),
|
||||
DateCreated = x.DateCreated.ToShortDateString(),
|
||||
DateModified = x.DateModified.ToShortDateString(),
|
||||
Description = x.Description,
|
||||
Cost = x.Cost.ToString(),
|
||||
Description = x.Description,
|
||||
Cost = x.Cost.ToString(),
|
||||
Notes = x.Notes,
|
||||
Type = x.ImportMode.ToString(),
|
||||
Priority = x.Priority.ToString(),
|
||||
Progress = x.Progress.ToString(),
|
||||
ExtraFields = x.ExtraFields,
|
||||
ExtraFields = x.ExtraFields,
|
||||
Files = x.Files });
|
||||
if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
|
||||
{
|
||||
@@ -207,6 +207,74 @@ namespace CarCareTracker.Controllers
|
||||
return Json(result);
|
||||
}
|
||||
}
|
||||
[TypeFilter(typeof(CollaboratorFilter))]
|
||||
[HttpPost]
|
||||
[Route("/api/vehicle/planrecords/add")]
|
||||
[Consumes("application/json")]
|
||||
public IActionResult AddPlanRecordJson(int vehicleId, [FromBody] PlanRecordExportModel input) => AddPlanRecord(vehicleId, input);
|
||||
[TypeFilter(typeof(CollaboratorFilter))]
|
||||
[HttpPost]
|
||||
[Route("/api/vehicle/planrecords/add")]
|
||||
public IActionResult AddPlanRecord(int vehicleId, PlanRecordExportModel 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.Cost) ||
|
||||
string.IsNullOrWhiteSpace(input.Type) ||
|
||||
string.IsNullOrWhiteSpace(input.Priority) ||
|
||||
string.IsNullOrWhiteSpace(input.Progress))
|
||||
{
|
||||
Response.StatusCode = 400;
|
||||
return Json(OperationResponse.Failed("Input object invalid, 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
|
||||
{
|
||||
var planRecord = new PlanRecord()
|
||||
{
|
||||
VehicleId = vehicleId,
|
||||
DateCreated = DateTime.Now,
|
||||
DateModified = DateTime.Now,
|
||||
Description = input.Description,
|
||||
Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes,
|
||||
Cost = decimal.Parse(input.Cost),
|
||||
ImportMode = parsedType,
|
||||
Priority = parsedPriority,
|
||||
Progress = parsedProgress,
|
||||
ExtraFields = input.ExtraFields,
|
||||
Files = input.Files
|
||||
};
|
||||
_planRecordDataAccess.SavePlanRecordToVehicle(planRecord);
|
||||
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromPlanRecord(planRecord, "planrecord.add.api", User.Identity.Name));
|
||||
return Json(OperationResponse.Succeed("Plan Record Added"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Response.StatusCode = 500;
|
||||
return Json(OperationResponse.Failed(ex.Message));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region ServiceRecord
|
||||
[TypeFilter(typeof(CollaboratorFilter))]
|
||||
|
||||
Reference in New Issue
Block a user