added functions to add and remove collaborators.

This commit is contained in:
DESKTOP-GENO133\IvanPlex
2024-01-13 21:18:58 -07:00
parent 4388df71f3
commit 2ae334d06d
6 changed files with 104 additions and 9 deletions

View File

@@ -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)

View File

@@ -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);
}
}

View File

@@ -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)
{

View File

@@ -9,6 +9,7 @@ namespace CarCareTracker.Logic
{
List<UserCollaborator> GetCollaboratorsForVehicle(int vehicleId);
bool AddUserAccessToVehicle(int userId, int vehicleId);
bool DeleteCollaboratorFromVehicle(int userId, int vehicleId);
OperationResponse AddCollaboratorToVehicle(int vehicleId, string username);
List<Vehicle> FilterUserVehicles(List<Vehicle> 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)

View File

@@ -1,12 +1,72 @@
@model List<UserCollaborator>
<div class="row">
<div class="col-12">
<div class="col-8">
<span class="lead">Collaborators</span>
</div>
<div class="col-4">
<button onclick="addCollaborator()" class="btn btn-link btn-sm"><i class="bi bi-person-add"></i></button>
</div>
</div>
<ul>
@foreach (UserCollaborator user in Model)
{
<li>@user.UserName</li>
<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)
{
<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>
}
</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");
}
})
}
</ul>
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>

View File

@@ -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>
</div>
</div>
<div class="col-md-3 col-12">
<div class="col-md-3 col-12" id="collaboratorContent">
@await Html.PartialAsync("_Collaborators", Model.Collaborators)
</div>
</div>
@@ -153,4 +153,10 @@
refreshBarChart();
})
}
function refreshCollaborators(){
var vehicleId = GetVehicleId().vehicleId;
$.get(`/Vehicle/GetCollaboratorsForVehicle?vehicleId=${vehicleId}`, function (data) {
$("#collaboratorContent").html(data);
});
}
</script>