Merge pull request #98 from LD-Reborn/97-feature-create-locations-table

Implemented /Home/Location table and delete action
This commit is contained in:
LD50
2025-10-10 09:36:16 +02:00
committed by GitHub
6 changed files with 67 additions and 37 deletions

View File

@@ -54,9 +54,21 @@ public class HomeController : Controller
}
[HttpGet("Locations")]
public ActionResult Locations()
public async Task<ActionResult> LocationsAsync()
{
return View();
IEnumerable<LocationModel> locations = await _ldap.ListLocationsAsync();
List<LocationTableViewModel> LocationsTableViewModels = [];
foreach (LocationModel location in locations)
{
LocationsTableViewModels.Add(new()
{
LocationID = location.Location,
LocationName = location.Description?.Location ?? "",
RoomNumber = location.Description?.RoomNumber ?? "",
Seat = location.Description?.Seat ?? ""
});
}
return View(new LocationsIndexViewModel() { LocationTableViewModels = LocationsTableViewModels });
}
[HttpGet("Users")]

View File

@@ -40,7 +40,7 @@ public class LocationsController : Controller
new LdapAttribute("l", location),
new LdapAttribute("description", JsonSerializer.Serialize(room))
];
await _ldap.CreateLocation(attributeSet);
return true;
}
@@ -52,22 +52,20 @@ public class LocationsController : Controller
}
[HttpGet("Delete")]
public async Task<bool> Delete(string cn)
public async Task<LocationsDeleteRequestModel> DeleteAsync(string cn)
{
if (cn is null) { return false; }
return await Task.Run(async () =>
if (cn is null) { return new(false); }
try
{
try
{
await _ldap.DeleteLocationAsync(cn);
return true;
}
catch (Exception)
{
return false;
}
});
await _ldap.DeleteLocationAsync(cn);
return new(true);
}
catch (Exception ex)
{
return new(false, ex.Message);
}
}
[HttpPost("Update")]
public async Task<bool> Update(LocationsModifyRequestModel requestModel)
{

View File

@@ -25,4 +25,12 @@ public class LocationsDescription
public string? Location { get; set; }
public string? RoomNumber { get; set; }
public string? Seat { get; set; }
}
public class LocationTableViewModel
{
public required string LocationID { get; set; }
public required string LocationName { get; set; }
public required string RoomNumber { get; set; }
public required string Seat { get; set; }
}

View File

@@ -9,4 +9,11 @@ public class LocationsModifyRequestModel
{
public required string Location { get; set; }
public required LocationsDescription Description { get; set; }
}
public class LocationsDeleteRequestModel(bool successful, string exception = "None")
{
public bool Success { get; set; } = successful;
public string? Exception { get; set; } = exception;
}

View File

@@ -0,0 +1,5 @@
namespace Berufsschule_HAM.Models;
public class LocationsIndexViewModel
{
public required IEnumerable<LocationTableViewModel> LocationTableViewModels { get; set; }
}

View File

@@ -1,6 +1,6 @@
@using Microsoft.AspNetCore.Mvc.Localization
@using Berufsschule_HAM.Models
@model HomeIndexViewModel
@model LocationsIndexViewModel
@inject IViewLocalizer T
@{
ViewData["Title"] = T["Locations"];
@@ -28,19 +28,19 @@
</tr>
</thead>
<tbody>
@* @{
foreach (AssetsTableViewModel assetsTableViewModel in Model.AssetsTableViewModels)
@{
foreach (LocationTableViewModel locationTableViewModel in Model.LocationTableViewModels)
{
<tr>
<td>@assetsTableViewModel.UserUID</td>
<td>@assetsTableViewModel.AssetCn</td>
<td>@assetsTableViewModel.AssetName</td>
<td>@assetsTableViewModel.LocationName</td>
<td>@locationTableViewModel.LocationID</td>
<td>@locationTableViewModel.LocationName</td>
<td>@locationTableViewModel.RoomNumber</td>
<td>@locationTableViewModel.Seat</td>
<td>
<div class="d-flex gap-2">
<button class="btn btn-sm btn-primary">Update</button>
<button class="btn btn-sm btn-danger btn-delete"
data-asset-id="@assetsTableViewModel.AssetCn"
data-location-id="@locationTableViewModel.LocationID"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
🗑️ Delete
@@ -49,13 +49,13 @@
</td>
</tr>
}
} *@
}
</tbody>
</table>
</div>
</div>
<!-- Asset Delete Confirmation Modal -->
<!-- Location Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
@@ -64,7 +64,7 @@
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete the asset <strong id="assetName"></strong> (ID: <span id="assetId"></span>)?</p>
<p>Are you sure you want to delete the location <strong id="locationName"></strong> (ID: <span id="locationId"></span>)?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
@@ -77,21 +77,21 @@
</div>
@* <script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const deleteModal = document.getElementById('deleteModal');
let currentButton = null; // The delete button that opened the modal
deleteModal.addEventListener('show.bs.modal', event => {
currentButton = event.relatedTarget; // Button that triggered the modal
const assetId = currentButton.getAttribute('data-asset-id');
const assetName = currentButton.getAttribute('data-asset-name');
const locationId = currentButton.getAttribute('data-location-id');
const locationName = currentButton.getAttribute('data-location-name');
deleteModal.querySelector('#assetId').textContent = assetId;
deleteModal.querySelector('#assetName').textContent = assetName;
deleteModal.querySelector('#locationId').textContent = locationId;
deleteModal.querySelector('#locationName').textContent = locationName;
// Store the delete URL for later use
deleteModal.querySelector('#deleteForm').dataset.url = `/Assets/Delete?cn=${assetId}`;
deleteModal.querySelector('#deleteForm').dataset.url = `/Locations/Delete?cn=${locationId}`;
});
// Handle submit of deleteForm via fetch()
@@ -100,7 +100,7 @@
e.preventDefault();
const url = deleteForm.dataset.url;
const assetId = deleteModal.querySelector('#assetId').textContent;
const locationId = deleteModal.querySelector('#locationId').textContent;
try {
const response = await fetch(url, {
@@ -109,7 +109,7 @@
'Content-Type': 'application/json',
'Accept': 'application/json'
}//,
//body: JSON.stringify({ id: assetId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
//body: JSON.stringify({ id: locationId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
});
const result = await response.json();
@@ -124,7 +124,7 @@
row.classList.add('table-danger');
setTimeout(() => row.remove(), 300);
showToast('Asset deleted successfully', 'success');
showToast('Location deleted successfully', 'success');
} else {
showToast(`❌ ${result.reason}: ${result.exception || 'Unknown error'}`, 'danger');
}
@@ -160,4 +160,4 @@
return container;
}
});
</script> *@
</script>