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")] [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")] [HttpGet("Users")]

View File

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

View File

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