added backend for saving and retrieving plan record templates.
This commit is contained in:
@@ -27,6 +27,7 @@ namespace CarCareTracker.Controllers
|
|||||||
private readonly IUpgradeRecordDataAccess _upgradeRecordDataAccess;
|
private readonly IUpgradeRecordDataAccess _upgradeRecordDataAccess;
|
||||||
private readonly ISupplyRecordDataAccess _supplyRecordDataAccess;
|
private readonly ISupplyRecordDataAccess _supplyRecordDataAccess;
|
||||||
private readonly IPlanRecordDataAccess _planRecordDataAccess;
|
private readonly IPlanRecordDataAccess _planRecordDataAccess;
|
||||||
|
private readonly IPlanRecordTemplateDataAccess _planRecordTemplateDataAccess;
|
||||||
private readonly IOdometerRecordDataAccess _odometerRecordDataAccess;
|
private readonly IOdometerRecordDataAccess _odometerRecordDataAccess;
|
||||||
private readonly IWebHostEnvironment _webEnv;
|
private readonly IWebHostEnvironment _webEnv;
|
||||||
private readonly IConfigHelper _config;
|
private readonly IConfigHelper _config;
|
||||||
@@ -51,6 +52,7 @@ namespace CarCareTracker.Controllers
|
|||||||
IUpgradeRecordDataAccess upgradeRecordDataAccess,
|
IUpgradeRecordDataAccess upgradeRecordDataAccess,
|
||||||
ISupplyRecordDataAccess supplyRecordDataAccess,
|
ISupplyRecordDataAccess supplyRecordDataAccess,
|
||||||
IPlanRecordDataAccess planRecordDataAccess,
|
IPlanRecordDataAccess planRecordDataAccess,
|
||||||
|
IPlanRecordTemplateDataAccess planRecordTemplateDataAccess,
|
||||||
IOdometerRecordDataAccess odometerRecordDataAccess,
|
IOdometerRecordDataAccess odometerRecordDataAccess,
|
||||||
IUserLogic userLogic,
|
IUserLogic userLogic,
|
||||||
IWebHostEnvironment webEnv,
|
IWebHostEnvironment webEnv,
|
||||||
@@ -71,6 +73,7 @@ namespace CarCareTracker.Controllers
|
|||||||
_upgradeRecordDataAccess = upgradeRecordDataAccess;
|
_upgradeRecordDataAccess = upgradeRecordDataAccess;
|
||||||
_supplyRecordDataAccess = supplyRecordDataAccess;
|
_supplyRecordDataAccess = supplyRecordDataAccess;
|
||||||
_planRecordDataAccess = planRecordDataAccess;
|
_planRecordDataAccess = planRecordDataAccess;
|
||||||
|
_planRecordTemplateDataAccess = planRecordTemplateDataAccess;
|
||||||
_odometerRecordDataAccess = odometerRecordDataAccess;
|
_odometerRecordDataAccess = odometerRecordDataAccess;
|
||||||
_userLogic = userLogic;
|
_userLogic = userLogic;
|
||||||
_webEnv = webEnv;
|
_webEnv = webEnv;
|
||||||
@@ -142,6 +145,7 @@ namespace CarCareTracker.Controllers
|
|||||||
_reminderRecordDataAccess.DeleteAllReminderRecordsByVehicleId(vehicleId) &&
|
_reminderRecordDataAccess.DeleteAllReminderRecordsByVehicleId(vehicleId) &&
|
||||||
_upgradeRecordDataAccess.DeleteAllUpgradeRecordsByVehicleId(vehicleId) &&
|
_upgradeRecordDataAccess.DeleteAllUpgradeRecordsByVehicleId(vehicleId) &&
|
||||||
_planRecordDataAccess.DeleteAllPlanRecordsByVehicleId(vehicleId) &&
|
_planRecordDataAccess.DeleteAllPlanRecordsByVehicleId(vehicleId) &&
|
||||||
|
_planRecordTemplateDataAccess.DeleteAllPlanRecordTemplatesByVehicleId(vehicleId) &&
|
||||||
_supplyRecordDataAccess.DeleteAllSupplyRecordsByVehicleId(vehicleId) &&
|
_supplyRecordDataAccess.DeleteAllSupplyRecordsByVehicleId(vehicleId) &&
|
||||||
_userLogic.DeleteAllAccessToVehicle(vehicleId) &&
|
_userLogic.DeleteAllAccessToVehicle(vehicleId) &&
|
||||||
_dataAccess.DeleteVehicle(vehicleId);
|
_dataAccess.DeleteVehicle(vehicleId);
|
||||||
@@ -1467,6 +1471,21 @@ namespace CarCareTracker.Controllers
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
#region "Supply Records"
|
#region "Supply Records"
|
||||||
|
private List<string> CheckSupplyRecordsAvailability(List<SupplyUsage> supplyUsage)
|
||||||
|
{
|
||||||
|
//returns empty string if all supplies are available
|
||||||
|
var result = new List<string>();
|
||||||
|
foreach (SupplyUsage supply in supplyUsage)
|
||||||
|
{
|
||||||
|
//get supply record.
|
||||||
|
var supplyData = _supplyRecordDataAccess.GetSupplyRecordById(supply.SupplyId);
|
||||||
|
if (supply.Quantity > supplyData.Quantity)
|
||||||
|
{
|
||||||
|
result.Add($"Insufficient Quantity for {supplyData.Description}, need: {supply.Quantity}, available: {supplyData.Quantity}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
private void RequisitionSupplyRecordsByUsage(List<SupplyUsage> supplyUsage)
|
private void RequisitionSupplyRecordsByUsage(List<SupplyUsage> supplyUsage)
|
||||||
{
|
{
|
||||||
foreach(SupplyUsage supply in supplyUsage)
|
foreach(SupplyUsage supply in supplyUsage)
|
||||||
@@ -1581,6 +1600,52 @@ namespace CarCareTracker.Controllers
|
|||||||
}
|
}
|
||||||
return Json(result);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult SavePlanRecordTemplateToVehicleId(PlanRecordInput planRecord)
|
||||||
|
{
|
||||||
|
//check if template name already taken.
|
||||||
|
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"});
|
||||||
|
}
|
||||||
|
planRecord.Files = planRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
|
||||||
|
var result = _planRecordTemplateDataAccess.SavePlanRecordTemplateToVehicle(planRecord);
|
||||||
|
return Json(new OperationResponse { Success = result, Message = result ? "Template Added" : StaticHelper.GenericErrorMessage });
|
||||||
|
}
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetPlanRecordTemplatesForVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
var result = _planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(vehicleId);
|
||||||
|
return PartialView("_PlanRecordTemplateModal", result);
|
||||||
|
}
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplateId)
|
||||||
|
{
|
||||||
|
var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplateById(planRecordTemplateId);
|
||||||
|
if (existingRecord.Id == default)
|
||||||
|
{
|
||||||
|
return Json(new OperationResponse { Success = false, Message = "Unable to find template" });
|
||||||
|
}
|
||||||
|
if (existingRecord.Supplies.Any())
|
||||||
|
{
|
||||||
|
//check if all supplies are available
|
||||||
|
var supplyAvailability = CheckSupplyRecordsAvailability(existingRecord.Supplies);
|
||||||
|
if (supplyAvailability.Any())
|
||||||
|
{
|
||||||
|
return Json(new OperationResponse { Success = false, Message = string.Join("<br>", supplyAvailability) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//populate createdDate
|
||||||
|
existingRecord.DateCreated = DateTime.Now.ToString("G");
|
||||||
|
existingRecord.DateModified = DateTime.Now.ToString("G");
|
||||||
|
var result = _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord.ToPlanRecord());
|
||||||
|
if (result && existingRecord.Supplies.Any())
|
||||||
|
{
|
||||||
|
RequisitionSupplyRecordsByUsage(existingRecord.Supplies);
|
||||||
|
}
|
||||||
|
return Json(new OperationResponse { Success = result, Message = result ? "Plan Record Added" : StaticHelper.GenericErrorMessage });
|
||||||
|
}
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult GetAddPlanRecordPartialView()
|
public IActionResult GetAddPlanRecordPartialView()
|
||||||
{
|
{
|
||||||
|
|||||||
57
External/Implementations/PlanRecordTemplateDataAccess.cs
vendored
Normal file
57
External/Implementations/PlanRecordTemplateDataAccess.cs
vendored
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Helper;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using LiteDB;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Implementations
|
||||||
|
{
|
||||||
|
public class PlanRecordTemplateDataAccess : IPlanRecordTemplateDataAccess
|
||||||
|
{
|
||||||
|
private static string dbName = StaticHelper.DbName;
|
||||||
|
private static string tableName = "planrecordtemplates";
|
||||||
|
public List<PlanRecordInput> GetPlanRecordTemplatesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
using (var db = new LiteDatabase(dbName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecordInput>(tableName);
|
||||||
|
var planRecords = table.Find(Query.EQ(nameof(PlanRecordInput.VehicleId), vehicleId));
|
||||||
|
return planRecords.ToList() ?? new List<PlanRecordInput>();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public PlanRecordInput GetPlanRecordTemplateById(int planRecordId)
|
||||||
|
{
|
||||||
|
using (var db = new LiteDatabase(dbName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecordInput>(tableName);
|
||||||
|
return table.FindById(planRecordId);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public bool DeletePlanRecordTemplateById(int planRecordId)
|
||||||
|
{
|
||||||
|
using (var db = new LiteDatabase(dbName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecordInput>(tableName);
|
||||||
|
table.Delete(planRecordId);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public bool SavePlanRecordTemplateToVehicle(PlanRecordInput planRecord)
|
||||||
|
{
|
||||||
|
using (var db = new LiteDatabase(dbName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecordInput>(tableName);
|
||||||
|
table.Upsert(planRecord);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public bool DeleteAllPlanRecordTemplatesByVehicleId(int vehicleId)
|
||||||
|
{
|
||||||
|
using (var db = new LiteDatabase(dbName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecord>(tableName);
|
||||||
|
var planRecords = table.DeleteMany(Query.EQ(nameof(PlanRecordInput.VehicleId), vehicleId));
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
External/Interfaces/IPlanRecordTemplateDataAccess.cs
vendored
Normal file
13
External/Interfaces/IPlanRecordTemplateDataAccess.cs
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using CarCareTracker.Models;
|
||||||
|
|
||||||
|
namespace CarCareTracker.External.Interfaces
|
||||||
|
{
|
||||||
|
public interface IPlanRecordTemplateDataAccess
|
||||||
|
{
|
||||||
|
public List<PlanRecordInput> GetPlanRecordTemplatesByVehicleId(int vehicleId);
|
||||||
|
public PlanRecordInput GetPlanRecordTemplateById(int planRecordId);
|
||||||
|
public bool DeletePlanRecordTemplateById(int planRecordId);
|
||||||
|
public bool SavePlanRecordTemplateToVehicle(PlanRecordInput planRecord);
|
||||||
|
public bool DeleteAllPlanRecordTemplatesByVehicleId(int vehicleId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ builder.Services.AddSingleton<IUserAccessDataAccess, UserAccessDataAccess>();
|
|||||||
builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
|
builder.Services.AddSingleton<IUserConfigDataAccess, UserConfigDataAccess>();
|
||||||
builder.Services.AddSingleton<ISupplyRecordDataAccess, SupplyRecordDataAccess>();
|
builder.Services.AddSingleton<ISupplyRecordDataAccess, SupplyRecordDataAccess>();
|
||||||
builder.Services.AddSingleton<IPlanRecordDataAccess, PlanRecordDataAccess>();
|
builder.Services.AddSingleton<IPlanRecordDataAccess, PlanRecordDataAccess>();
|
||||||
|
builder.Services.AddSingleton<IPlanRecordTemplateDataAccess, PlanRecordTemplateDataAccess>();
|
||||||
builder.Services.AddSingleton<IOdometerRecordDataAccess, OdometerRecordDataAccess>();
|
builder.Services.AddSingleton<IOdometerRecordDataAccess, OdometerRecordDataAccess>();
|
||||||
|
|
||||||
//configure helpers
|
//configure helpers
|
||||||
|
|||||||
@@ -100,4 +100,11 @@
|
|||||||
<div class="modal-content" id="planRecordModalContent">
|
<div class="modal-content" id="planRecordModalContent">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal fade" data-bs-focus="false" id="planRecordTemplateModal" tabindex="-1" role="dialog" aria-hidden="true">
|
||||||
|
<div class="modal-dialog modal-lg" role="document">
|
||||||
|
<div class="modal-content" id="planRecordTemplateModalContent">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
Reference in New Issue
Block a user