added functions to add and remove collaborators.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using CarCareTracker.External.Interfaces;
|
using CarCareTracker.External.Interfaces;
|
||||||
|
using CarCareTracker.Filter;
|
||||||
using CarCareTracker.Helper;
|
using CarCareTracker.Helper;
|
||||||
using CarCareTracker.Models;
|
using CarCareTracker.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@@ -53,6 +54,7 @@ namespace CarCareTracker.Controllers
|
|||||||
var result = _dataAccess.GetVehicles();
|
var result = _dataAccess.GetVehicles();
|
||||||
return Json(result);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/servicerecords")]
|
[Route("/api/vehicle/servicerecords")]
|
||||||
public IActionResult ServiceRecords(int vehicleId)
|
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() });
|
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);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/repairrecords")]
|
[Route("/api/vehicle/repairrecords")]
|
||||||
public IActionResult RepairRecords(int vehicleId)
|
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() });
|
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);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/upgraderecords")]
|
[Route("/api/vehicle/upgraderecords")]
|
||||||
public IActionResult UpgradeRecords(int vehicleId)
|
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() });
|
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);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/taxrecords")]
|
[Route("/api/vehicle/taxrecords")]
|
||||||
public IActionResult TaxRecords(int vehicleId)
|
public IActionResult TaxRecords(int vehicleId)
|
||||||
@@ -84,6 +89,7 @@ namespace CarCareTracker.Controllers
|
|||||||
var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
|
var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
|
||||||
return Json(result);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/gasrecords")]
|
[Route("/api/vehicle/gasrecords")]
|
||||||
public IActionResult GasRecords(int vehicleId, bool useMPG, bool useUKMPG)
|
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()});
|
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);
|
return Json(result);
|
||||||
}
|
}
|
||||||
|
[TypeFilter(typeof(CollaboratorFilter))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("/api/vehicle/reminders")]
|
[Route("/api/vehicle/reminders")]
|
||||||
public IActionResult Reminders(int vehicleId)
|
public IActionResult Reminders(int vehicleId)
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ namespace CarCareTracker.Controllers
|
|||||||
public class AdminController : Controller
|
public class AdminController : Controller
|
||||||
{
|
{
|
||||||
private ILoginLogic _loginLogic;
|
private ILoginLogic _loginLogic;
|
||||||
public AdminController(ILoginLogic loginLogic)
|
private IUserLogic _userLogic;
|
||||||
|
public AdminController(ILoginLogic loginLogic, IUserLogic userLogic)
|
||||||
{
|
{
|
||||||
_loginLogic = loginLogic;
|
_loginLogic = loginLogic;
|
||||||
|
_userLogic = userLogic;
|
||||||
}
|
}
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
@@ -36,7 +38,7 @@ namespace CarCareTracker.Controllers
|
|||||||
}
|
}
|
||||||
public IActionResult DeleteUser(int userId)
|
public IActionResult DeleteUser(int userId)
|
||||||
{
|
{
|
||||||
var result =_loginLogic.DeleteUser(userId);
|
var result =_userLogic.DeleteAllAccessToUser(userId) && _loginLogic.DeleteUser(userId);
|
||||||
return Json(result);
|
return Json(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -688,6 +688,20 @@ namespace CarCareTracker.Controllers
|
|||||||
return PartialView("_Collaborators", result);
|
return PartialView("_Collaborators", result);
|
||||||
}
|
}
|
||||||
[TypeFilter(typeof(CollaboratorFilter))]
|
[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]
|
[HttpGet]
|
||||||
public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0)
|
public IActionResult GetCostMakeUpForVehicle(int vehicleId, int year = 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ namespace CarCareTracker.Logic
|
|||||||
{
|
{
|
||||||
List<UserCollaborator> GetCollaboratorsForVehicle(int vehicleId);
|
List<UserCollaborator> GetCollaboratorsForVehicle(int vehicleId);
|
||||||
bool AddUserAccessToVehicle(int userId, int vehicleId);
|
bool AddUserAccessToVehicle(int userId, int vehicleId);
|
||||||
|
bool DeleteCollaboratorFromVehicle(int userId, int vehicleId);
|
||||||
OperationResponse AddCollaboratorToVehicle(int vehicleId, string username);
|
OperationResponse AddCollaboratorToVehicle(int vehicleId, string username);
|
||||||
List<Vehicle> FilterUserVehicles(List<Vehicle> results, int userId);
|
List<Vehicle> FilterUserVehicles(List<Vehicle> results, int userId);
|
||||||
bool UserCanAccessVehicle(int userId, int vehicleId);
|
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" };
|
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)
|
public bool AddUserAccessToVehicle(int userId, int vehicleId)
|
||||||
{
|
{
|
||||||
if (userId == -1)
|
if (userId == -1)
|
||||||
|
|||||||
@@ -1,12 +1,72 @@
|
|||||||
@model List<UserCollaborator>
|
@model List<UserCollaborator>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-12">
|
<div class="col-8">
|
||||||
<span class="lead">Collaborators</span>
|
<span class="lead">Collaborators</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-4">
|
||||||
|
<button onclick="addCollaborator()" class="btn btn-link btn-sm"><i class="bi bi-person-add"></i></button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<ul>
|
<div class="row">
|
||||||
|
<table class="table table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr class="d-flex">
|
||||||
|
<th scope="col" class="col-8">Username</th>
|
||||||
|
<th scope="col" class="col-4">Delete</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
@foreach (UserCollaborator user in Model)
|
@foreach (UserCollaborator user in Model)
|
||||||
{
|
{
|
||||||
<li>@user.UserName</li>
|
<tr class="d-flex">
|
||||||
|
<td class="col-8">@user.UserName</td>
|
||||||
|
<td class="col-4">
|
||||||
|
@if(User.Identity.Name != user.UserName)
|
||||||
|
{
|
||||||
|
<button onclick="deleteCollaborator(@user.UserVehicle.UserId, @user.UserVehicle.VehicleId)" class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
|
||||||
}
|
}
|
||||||
</ul>
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
function deleteCollaborator(userId, vehicleId) {
|
||||||
|
$.post('/Vehicle/DeleteCollaboratorFromVehicle', {userId: userId, vehicleId: vehicleId}, function(data){
|
||||||
|
if (data) {
|
||||||
|
refreshCollaborators();
|
||||||
|
} else {
|
||||||
|
errorToast("An error occurred, please try again later");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
function addCollaborator() {
|
||||||
|
Swal.fire({
|
||||||
|
title: 'Add Collaborator',
|
||||||
|
html: `
|
||||||
|
<input type="text" id="inputUserName" class="swal2-input" placeholder="Username">
|
||||||
|
`,
|
||||||
|
confirmButtonText: 'Add',
|
||||||
|
focusConfirm: false,
|
||||||
|
preConfirm: () => {
|
||||||
|
const userName = $("#inputUserName").val();
|
||||||
|
if (!userName) {
|
||||||
|
Swal.showValidationMessage(`Please enter a username`);
|
||||||
|
}
|
||||||
|
return { userName }
|
||||||
|
},
|
||||||
|
}).then(function (result) {
|
||||||
|
if (result.isConfirmed) {
|
||||||
|
var vehicleId = GetVehicleId().vehicleId;
|
||||||
|
$.post('/Vehicle/AddCollaboratorsToVehicle', { username: result.value.userName, vehicleId: vehicleId }, function (data) {
|
||||||
|
if (data.success) {
|
||||||
|
refreshCollaborators();
|
||||||
|
} else {
|
||||||
|
errorToast(data.message)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
<button onclick="generateVehicleHistoryReport()" class="btn btn-secondary btn-md mt-1 mb-1"><i class="bi bi-pencil-square me-2"></i>Generate and Print Vehicle Maintenance Report</button>
|
<button onclick="generateVehicleHistoryReport()" class="btn btn-secondary btn-md mt-1 mb-1"><i class="bi bi-pencil-square me-2"></i>Generate and Print Vehicle Maintenance Report</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3 col-12">
|
<div class="col-md-3 col-12" id="collaboratorContent">
|
||||||
@await Html.PartialAsync("_Collaborators", Model.Collaborators)
|
@await Html.PartialAsync("_Collaborators", Model.Collaborators)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -153,4 +153,10 @@
|
|||||||
refreshBarChart();
|
refreshBarChart();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
function refreshCollaborators(){
|
||||||
|
var vehicleId = GetVehicleId().vehicleId;
|
||||||
|
$.get(`/Vehicle/GetCollaboratorsForVehicle?vehicleId=${vehicleId}`, function (data) {
|
||||||
|
$("#collaboratorContent").html(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
Reference in New Issue
Block a user