diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 0b9e775..8d35c08 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -19,6 +19,7 @@ namespace CarCareTracker.Controllers private readonly INoteDataAccess _noteDataAccess; private readonly IServiceRecordDataAccess _serviceRecordDataAccess; private readonly IGasRecordDataAccess _gasRecordDataAccess; + private readonly ICollisionRecordDataAccess _collisionRecordDataAccess; private readonly IWebHostEnvironment _webEnv; private readonly IFileHelper _fileHelper; @@ -28,6 +29,7 @@ namespace CarCareTracker.Controllers INoteDataAccess noteDataAccess, IServiceRecordDataAccess serviceRecordDataAccess, IGasRecordDataAccess gasRecordDataAccess, + ICollisionRecordDataAccess collisionRecordDataAccess, IWebHostEnvironment webEnv) { _logger = logger; @@ -36,6 +38,7 @@ namespace CarCareTracker.Controllers _fileHelper = fileHelper; _serviceRecordDataAccess = serviceRecordDataAccess; _gasRecordDataAccess = gasRecordDataAccess; + _collisionRecordDataAccess = collisionRecordDataAccess; _webEnv = webEnv; } [HttpGet] @@ -227,6 +230,50 @@ namespace CarCareTracker.Controllers return Json(result); } #endregion - + #region "Collision Records" + [HttpGet] + public IActionResult GetCollisionRecordsByVehicleId(int vehicleId) + { + var result = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId); + return PartialView("_CollisionRecords", result); + } + [HttpPost] + public IActionResult SaveCollisionRecordToVehicleId(CollisionRecordInput serviceRecord) + { + //move files from temp. + serviceRecord.Files = serviceRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList(); + var result = _collisionRecordDataAccess.SaveCollisionRecordToVehicle(serviceRecord.ToCollisionRecord()); + return Json(result); + } + [HttpGet] + public IActionResult GetAddCollisionRecordPartialView() + { + return PartialView("_CollisionRecordModal", new CollisionRecordInput()); + } + [HttpGet] + public IActionResult GetCollisionRecordForEditById(int serviceRecordId) + { + var result = _collisionRecordDataAccess.GetCollisionRecordById(serviceRecordId); + //convert to Input object. + var convertedResult = new CollisionRecordInput + { + Id = result.Id, + Cost = result.Cost, + Date = result.Date.ToShortDateString(), + Description = result.Description, + Mileage = result.Mileage, + Notes = result.Notes, + VehicleId = result.VehicleId, + Files = result.Files + }; + return PartialView("_CollisionRecordModal", convertedResult); + } + [HttpPost] + public IActionResult DeleteCollisionRecordById(int serviceRecordId) + { + var result = _collisionRecordDataAccess.DeleteCollisionRecordById(serviceRecordId); + return Json(result); + } + #endregion } } diff --git a/External/Implementations/CollisionRecordDataAccess.cs b/External/Implementations/CollisionRecordDataAccess.cs new file mode 100644 index 0000000..68a9d9a --- /dev/null +++ b/External/Implementations/CollisionRecordDataAccess.cs @@ -0,0 +1,56 @@ +using CarCareTracker.External.Interfaces; +using CarCareTracker.Models; +using LiteDB; + +namespace CarCareTracker.External.Implementations +{ + public class CollisionRecordDataAccess : ICollisionRecordDataAccess + { + private static string dbName = "cartracker.db"; + private static string tableName = "collisionrecords"; + public List GetCollisionRecordsByVehicleId(int vehicleId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + var collisionRecords = table.Find(Query.EQ(nameof(CollisionRecord.VehicleId), vehicleId)).OrderBy(x => x.Date); + return collisionRecords.ToList() ?? new List(); + }; + } + public CollisionRecord GetCollisionRecordById(int collisionRecordId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + return table.FindById(collisionRecordId); + }; + } + public bool DeleteCollisionRecordById(int collisionRecordId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + table.Delete(collisionRecordId); + return true; + }; + } + public bool SaveCollisionRecordToVehicle(CollisionRecord collisionRecord) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + table.Upsert(collisionRecord); + return true; + }; + } + public bool DeleteAllCollisionRecordsByVehicleId(int vehicleId) + { + using (var db = new LiteDatabase(dbName)) + { + var table = db.GetCollection(tableName); + var collisionRecords = table.DeleteMany(Query.EQ(nameof(CollisionRecord.VehicleId), vehicleId)); + return true; + }; + } + } +} diff --git a/External/Interfaces/ICollisionRecordDataAccess.cs b/External/Interfaces/ICollisionRecordDataAccess.cs new file mode 100644 index 0000000..6922c10 --- /dev/null +++ b/External/Interfaces/ICollisionRecordDataAccess.cs @@ -0,0 +1,13 @@ +using CarCareTracker.Models; + +namespace CarCareTracker.External.Interfaces +{ + public interface ICollisionRecordDataAccess + { + public List GetCollisionRecordsByVehicleId(int vehicleId); + public CollisionRecord GetCollisionRecordById(int serviceRecordId); + public bool DeleteCollisionRecordById(int serviceRecordId); + public bool SaveCollisionRecordToVehicle(CollisionRecord serviceRecord); + public bool DeleteAllCollisionRecordsByVehicleId(int vehicleId); + } +} diff --git a/Program.cs b/Program.cs index 7fb0e27..f2e5a09 100644 --- a/Program.cs +++ b/Program.cs @@ -10,6 +10,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index fbca0bd..a14d3c5 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -1,5 +1,9 @@  - +@inject IConfiguration Configuration +@{ + var useDarkMode = bool.Parse(Configuration["DarkMode"]); +} + diff --git a/Views/Vehicle/Index.cshtml b/Views/Vehicle/Index.cshtml index 8cfc576..4c96efd 100644 --- a/Views/Vehicle/Index.cshtml +++ b/Views/Vehicle/Index.cshtml @@ -2,7 +2,7 @@ ViewData["Title"] = "LubeLogger - View Vehicle"; } @model Vehicle -@section Scripts{ +@section Scripts { @@ -34,12 +34,15 @@ -
-
+
222
@@ -58,13 +61,6 @@
333
-
-
-
- -
-
-
\ No newline at end of file diff --git a/Views/_ViewImports.cshtml b/Views/_ViewImports.cshtml index cedcfab..fab8dc1 100644 --- a/Views/_ViewImports.cshtml +++ b/Views/_ViewImports.cshtml @@ -1,3 +1,4 @@ @using CarCareTracker @using CarCareTracker.Models +@using Microsoft.Extensions.Options @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/appsettings.json b/appsettings.json index 10f68b8..2aa9e16 100644 --- a/appsettings.json +++ b/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "DarkMode": true }