From 2ae334d06d1162ed9489c8cee63b53d4adca5520 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 13 Jan 2024 21:18:58 -0700 Subject: [PATCH] added functions to add and remove collaborators. --- Controllers/APIController.cs | 7 +++ Controllers/AdminController.cs | 6 ++- Controllers/VehicleController.cs | 14 ++++++ Logic/UserLogic.cs | 6 +++ Views/Vehicle/_Collaborators.cshtml | 72 ++++++++++++++++++++++++++--- Views/Vehicle/_Report.cshtml | 8 +++- 6 files changed, 104 insertions(+), 9 deletions(-) diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index 0062f44..a353d9f 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -1,4 +1,5 @@ using CarCareTracker.External.Interfaces; +using CarCareTracker.Filter; using CarCareTracker.Helper; using CarCareTracker.Models; using Microsoft.AspNetCore.Authorization; @@ -53,6 +54,7 @@ namespace CarCareTracker.Controllers var result = _dataAccess.GetVehicles(); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/servicerecords")] public IActionResult ServiceRecords(int vehicleId) @@ -61,6 +63,7 @@ namespace CarCareTracker.Controllers var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/repairrecords")] public IActionResult RepairRecords(int vehicleId) @@ -69,6 +72,7 @@ namespace CarCareTracker.Controllers var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/upgraderecords")] public IActionResult UpgradeRecords(int vehicleId) @@ -77,6 +81,7 @@ namespace CarCareTracker.Controllers var result = vehicleRecords.Select(x => new ServiceRecordExportModel { Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString() }); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/taxrecords")] public IActionResult TaxRecords(int vehicleId) @@ -84,6 +89,7 @@ namespace CarCareTracker.Controllers var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/gasrecords")] public IActionResult GasRecords(int vehicleId, bool useMPG, bool useUKMPG) @@ -92,6 +98,7 @@ namespace CarCareTracker.Controllers var result = _gasHelper.GetGasRecordViewModels(vehicleRecords, useMPG, useUKMPG).Select(x => new GasRecordExportModel { Date = x.Date, Odometer = x.Mileage.ToString(), Cost = x.Cost.ToString(), FuelConsumed = x.Gallons.ToString(), FuelEconomy = x.MilesPerGallon.ToString()}); return Json(result); } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] [Route("/api/vehicle/reminders")] public IActionResult Reminders(int vehicleId) diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index 4a530a5..e41a530 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -11,9 +11,11 @@ namespace CarCareTracker.Controllers public class AdminController : Controller { private ILoginLogic _loginLogic; - public AdminController(ILoginLogic loginLogic) + private IUserLogic _userLogic; + public AdminController(ILoginLogic loginLogic, IUserLogic userLogic) { _loginLogic = loginLogic; + _userLogic = userLogic; } public IActionResult Index() { @@ -36,7 +38,7 @@ namespace CarCareTracker.Controllers } public IActionResult DeleteUser(int userId) { - var result =_loginLogic.DeleteUser(userId); + var result =_userLogic.DeleteAllAccessToUser(userId) && _loginLogic.DeleteUser(userId); return Json(result); } } diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index cbb8392..149fa0f 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -688,6 +688,20 @@ namespace CarCareTracker.Controllers return PartialView("_Collaborators", result); } [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + public IActionResult AddCollaboratorsToVehicle(int vehicleId, string username) + { + var result = _userLogic.AddCollaboratorToVehicle(vehicleId, username); + return Json(result); + } + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + public IActionResult DeleteCollaboratorFromVehicle(int userId, int vehicleId) + { + var result = _userLogic.DeleteCollaboratorFromVehicle(userId, vehicleId); + return Json(result); + } + [TypeFilter(typeof(CollaboratorFilter))] [HttpGet] public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0) { diff --git a/Logic/UserLogic.cs b/Logic/UserLogic.cs index a5b3889..dd910d9 100644 --- a/Logic/UserLogic.cs +++ b/Logic/UserLogic.cs @@ -9,6 +9,7 @@ namespace CarCareTracker.Logic { List GetCollaboratorsForVehicle(int vehicleId); bool AddUserAccessToVehicle(int userId, int vehicleId); + bool DeleteCollaboratorFromVehicle(int userId, int vehicleId); OperationResponse AddCollaboratorToVehicle(int vehicleId, string username); List FilterUserVehicles(List results, int userId); bool UserCanAccessVehicle(int userId, int vehicleId); @@ -57,6 +58,11 @@ namespace CarCareTracker.Logic } return new OperationResponse { Success = false, Message = $"Unable to find user {username} in the system" }; } + public bool DeleteCollaboratorFromVehicle(int userId, int vehicleId) + { + var result = _userAccess.DeleteUserAccess(userId, vehicleId); + return result; + } public bool AddUserAccessToVehicle(int userId, int vehicleId) { if (userId == -1) diff --git a/Views/Vehicle/_Collaborators.cshtml b/Views/Vehicle/_Collaborators.cshtml index ef793e9..20fc53e 100644 --- a/Views/Vehicle/_Collaborators.cshtml +++ b/Views/Vehicle/_Collaborators.cshtml @@ -1,12 +1,72 @@ @model List
-
+
Collaborators
+
+ +
-
    - @foreach (UserCollaborator user in Model) - { -
  • @user.UserName
  • +
    + + + + + + + + + @foreach (UserCollaborator user in Model) + { + + + + + } + +
    UsernameDelete
    @user.UserName + @if(User.Identity.Name != user.UserName) + { + + } +
    +
    + \ No newline at end of file diff --git a/Views/Vehicle/_Report.cshtml b/Views/Vehicle/_Report.cshtml index 97a3a2f..9cb0a6b 100644 --- a/Views/Vehicle/_Report.cshtml +++ b/Views/Vehicle/_Report.cshtml @@ -77,7 +77,7 @@
-
+
@await Html.PartialAsync("_Collaborators", Model.Collaborators)
@@ -153,4 +153,10 @@ refreshBarChart(); }) } + function refreshCollaborators(){ + var vehicleId = GetVehicleId().vehicleId; + $.get(`/Vehicle/GetCollaboratorsForVehicle?vehicleId=${vehicleId}`, function (data) { + $("#collaboratorContent").html(data); + }); + } \ No newline at end of file