mirror of
https://github.com/hargata/lubelog.git
synced 2025-12-10 00:46:08 -06:00
commit
2652e47018
@ -57,7 +57,7 @@ namespace CarCareTracker.Controllers
|
||||
IUserLogic userLogic,
|
||||
IVehicleLogic vehicleLogic,
|
||||
IOdometerLogic odometerLogic,
|
||||
IWebHostEnvironment webEnv)
|
||||
IWebHostEnvironment webEnv)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_noteDataAccess = noteDataAccess;
|
||||
@ -92,6 +92,26 @@ namespace CarCareTracker.Controllers
|
||||
return int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("/api/whoami")]
|
||||
public IActionResult WhoAmI()
|
||||
{
|
||||
var result = new UserExportModel
|
||||
{
|
||||
Username = User.FindFirstValue(ClaimTypes.Name),
|
||||
EmailAddress = User.IsInRole(nameof(UserData.IsRootUser)) ? _config.GetUserConfig(User).DefaultReminderEmail : User.FindFirstValue(ClaimTypes.Email),
|
||||
IsAdmin = User.IsInRole(nameof(UserData.IsAdmin)).ToString(),
|
||||
IsRoot = User.IsInRole(nameof(UserData.IsRootUser)).ToString()
|
||||
};
|
||||
if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
|
||||
{
|
||||
return Json(result, StaticHelper.GetInvariantOption());
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json(result);
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("/api/vehicles")]
|
||||
public IActionResult Vehicles()
|
||||
{
|
||||
@ -153,6 +173,210 @@ namespace CarCareTracker.Controllers
|
||||
return Json(convertedOdometer);
|
||||
}
|
||||
}
|
||||
#region PlanRecord
|
||||
[TypeFilter(typeof(CollaboratorFilter))]
|
||||
[HttpGet]
|
||||
[Route("/api/vehicle/planrecords")]
|
||||
public IActionResult PlanRecords(int vehicleId)
|
||||
{
|
||||
if (vehicleId == default)
|
||||
{
|
||||
var response = OperationResponse.Failed("Must provide a valid vehicle id");
|
||||
Response.StatusCode = 400;
|
||||
return Json(response);
|
||||
}
|
||||
var vehicleRecords = _planRecordDataAccess.GetPlanRecordsByVehicleId(vehicleId);
|
||||
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(),
|
||||
Notes = x.Notes,
|
||||
Type = x.ImportMode.ToString(),
|
||||
Priority = x.Priority.ToString(),
|
||||
Progress = x.Progress.ToString(),
|
||||
ExtraFields = x.ExtraFields,
|
||||
Files = x.Files });
|
||||
if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
|
||||
{
|
||||
return Json(result, StaticHelper.GetInvariantOption());
|
||||
}
|
||||
else
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
[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))]
|
||||
[HttpGet]
|
||||
|
||||
@ -6,7 +6,7 @@ namespace CarCareTracker.Controllers
|
||||
{
|
||||
public IActionResult Unauthorized()
|
||||
{
|
||||
if (!User.IsInRole("CookieAuth"))
|
||||
if (User.IsInRole("APIAuth"))
|
||||
{
|
||||
Response.StatusCode = 403;
|
||||
return new EmptyResult();
|
||||
|
||||
@ -12,7 +12,7 @@ namespace CarCareTracker.Helper
|
||||
/// </summary>
|
||||
public static class StaticHelper
|
||||
{
|
||||
public const string VersionNumber = "1.4.4";
|
||||
public const string VersionNumber = "1.4.5";
|
||||
public const string DbName = "data/cartracker.db";
|
||||
public const string UserConfigPath = "data/config/userConfig.json";
|
||||
public const string LegacyUserConfigPath = "config/userConfig.json";
|
||||
|
||||
@ -75,7 +75,9 @@ namespace CarCareTracker.Middleware
|
||||
var userIdentity = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Name, splitString[0]),
|
||||
new(ClaimTypes.NameIdentifier, userData.Id.ToString())
|
||||
new(ClaimTypes.NameIdentifier, userData.Id.ToString()),
|
||||
new(ClaimTypes.Email, userData.EmailAddress),
|
||||
new(ClaimTypes.Role, "APIAuth")
|
||||
};
|
||||
if (userData.IsAdmin)
|
||||
{
|
||||
|
||||
@ -126,14 +126,29 @@ namespace CarCareTracker.Models
|
||||
}
|
||||
public class PlanRecordExportModel
|
||||
{
|
||||
[JsonConverter(typeof(FromIntOptional))]
|
||||
public string Id { get; set; }
|
||||
[JsonConverter(typeof(FromDateOptional))]
|
||||
public string DateCreated { get; set; }
|
||||
[JsonConverter(typeof(FromDateOptional))]
|
||||
public string DateModified { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Notes { get; set; }
|
||||
public string Type { get; set; }
|
||||
public string Priority { get; set; }
|
||||
public string Progress { get; set; }
|
||||
[JsonConverter(typeof(FromDecimalOptional))]
|
||||
public string Cost { get; set; }
|
||||
public List<ExtraField> ExtraFields { get; set; } = new List<ExtraField>();
|
||||
public List<UploadedFiles> Files { get; set; } = new List<UploadedFiles>();
|
||||
}
|
||||
public class UserExportModel
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public string EmailAddress { get; set; }
|
||||
[JsonConverter(typeof(FromBoolOptional))]
|
||||
public string IsAdmin { get; set; }
|
||||
[JsonConverter(typeof(FromBoolOptional))]
|
||||
public string IsRoot { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,20 @@
|
||||
<h6>Parameters</h6>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge bg-success">GET</span>
|
||||
</div>
|
||||
<div class="col-5 copyable testable">
|
||||
<code>/api/whoami</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Returns information for current user
|
||||
</div>
|
||||
<div class="col-3">
|
||||
No Params
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge bg-success">GET</span>
|
||||
@ -159,6 +173,83 @@
|
||||
Id - Id of Odometer Record
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge bg-success">GET</span>
|
||||
</div>
|
||||
<div class="col-5 copyable">
|
||||
<code>/api/vehicle/planrecords</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Returns a list of plan records for the vehicle
|
||||
</div>
|
||||
<div class="col-3">
|
||||
vehicleId - Id of Vehicle
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge bg-primary">POST</span>
|
||||
</div>
|
||||
<div class="col-5 copyable">
|
||||
<code>/api/vehicle/planrecords/add</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Adds Plan Record to the vehicle
|
||||
</div>
|
||||
<div class="col-3">
|
||||
vehicleId - Id of Vehicle
|
||||
<br />
|
||||
Body(form-data): {<br />
|
||||
description - Description<br />
|
||||
cost - Cost<br />
|
||||
type - ServiceRecord/RepairRecord/UpgradeRecord<br />
|
||||
priority - Low/Normal/Critical<br />
|
||||
progress - Backlog/InProgress/Testing<br />
|
||||
notes - notes(optional)<br />
|
||||
extrafields - <a class="link-body-emphasis link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover reminder-calendar-item" onclick="showExtraFieldsInfo()">extrafields(optional)</a><br />
|
||||
files - <a class="link-body-emphasis link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover reminder-calendar-item" onclick="showAttachmentsInfo()">attachments(optional)</a><br />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge text-bg-warning">PUT</span>
|
||||
</div>
|
||||
<div class="col-5 copyable">
|
||||
<code>/api/vehicle/planrecords/update</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Updates Plan Record
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Body(form-data): {<br />
|
||||
Id - Id of Plan Record<br />
|
||||
description - Description<br />
|
||||
cost - Cost<br />
|
||||
type - ServiceRecord/RepairRecord/UpgradeRecord<br />
|
||||
priority - Low/Normal/Critical<br />
|
||||
progress - Backlog/InProgress/Testing<br />
|
||||
notes - notes(optional)<br />
|
||||
extrafields - <a class="link-body-emphasis link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover reminder-calendar-item" onclick="showExtraFieldsInfo()">extrafields(optional)</a><br />
|
||||
files - <a class="link-body-emphasis link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover reminder-calendar-item" onclick="showAttachmentsInfo()">attachments(optional)</a><br />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge text-bg-danger">DELETE</span>
|
||||
</div>
|
||||
<div class="col-5 copyable">
|
||||
<code>/api/vehicle/planrecords/delete</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Deletes Plan Record
|
||||
</div>
|
||||
<div class="col-3">
|
||||
Id - Id of Plan Record
|
||||
</div>
|
||||
</div>
|
||||
<div class="row api-method">
|
||||
<div class="col-1">
|
||||
<span class="badge bg-success">GET</span>
|
||||
@ -408,7 +499,7 @@
|
||||
<div class="col-1">
|
||||
<span class="badge bg-success">GET</span>
|
||||
</div>
|
||||
<div class="col-5 copyable">
|
||||
<div class="col-5 copyable testable">
|
||||
<code>/api/vehicle/taxrecords/check</code>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link @(Model == "settings" ? "active" : "")" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings-tab-pane" type="button" role="tab"><span class="ms-2 display-3"><i class="bi bi-gear me-2"></i>@translator.Translate(userLanguage,"Settings")</span></button>
|
||||
</li>
|
||||
@if (User.IsInRole("CookieAuth"))
|
||||
@if (User.IsInRole("CookieAuth") || User.IsInRole("APIAuth"))
|
||||
{
|
||||
@if (User.IsInRole(nameof(UserData.IsAdmin)))
|
||||
{
|
||||
@ -84,7 +84,7 @@
|
||||
<li class="nav-item ms-auto" role="presentation">
|
||||
<button class="nav-link resizable-nav-link @(Model == "settings" ? "active" : "")" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings-tab-pane" type="button" role="tab"><i class="bi bi-gear"></i><span class="ms-2 d-sm-none d-md-inline">@translator.Translate(userLanguage, "Settings")</span></button>
|
||||
</li>
|
||||
@if (User.IsInRole("CookieAuth"))
|
||||
@if (User.IsInRole("CookieAuth") || User.IsInRole("APIAuth"))
|
||||
{
|
||||
<li class="nav-item dropdown" role="presentation">
|
||||
<a class="nav-link resizable-nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false"><i class="bi bi-person"></i><span class="ms-2 d-sm-none d-md-inline">@User.Identity.Name</span></a>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user