diff --git a/src/Controllers/HomeController.cs b/src/Controllers/HomeController.cs index 1f2d816..dd5be8f 100644 --- a/src/Controllers/HomeController.cs +++ b/src/Controllers/HomeController.cs @@ -54,9 +54,21 @@ public class HomeController : Controller } [HttpGet("Locations")] - public ActionResult Locations() + public async Task LocationsAsync() { - return View(); + IEnumerable locations = await _ldap.ListLocationsAsync(); + List 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")] diff --git a/src/Controllers/LocationsController.cs b/src/Controllers/LocationsController.cs index d8bb9dc..4909cee 100644 --- a/src/Controllers/LocationsController.cs +++ b/src/Controllers/LocationsController.cs @@ -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 Delete(string cn) + public async Task 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 Update(LocationsModifyRequestModel requestModel) { diff --git a/src/Models/LocationsModels.cs b/src/Models/LocationsModels.cs index bb6766e..048d03d 100644 --- a/src/Models/LocationsModels.cs +++ b/src/Models/LocationsModels.cs @@ -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; } } \ No newline at end of file diff --git a/src/Models/LocationsRequestModels.cs b/src/Models/LocationsRequestModels.cs index 1056783..141c1d2 100644 --- a/src/Models/LocationsRequestModels.cs +++ b/src/Models/LocationsRequestModels.cs @@ -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; } \ No newline at end of file diff --git a/src/Models/LocationsViewModels.cs b/src/Models/LocationsViewModels.cs new file mode 100644 index 0000000..b7d2ee2 --- /dev/null +++ b/src/Models/LocationsViewModels.cs @@ -0,0 +1,5 @@ +namespace Berufsschule_HAM.Models; +public class LocationsIndexViewModel +{ + public required IEnumerable LocationTableViewModels { get; set; } +} \ No newline at end of file diff --git a/src/Views/Home/Locations.cshtml b/src/Views/Home/Locations.cshtml index c3a1337..2f598df 100644 --- a/src/Views/Home/Locations.cshtml +++ b/src/Views/Home/Locations.cshtml @@ -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 @@ - @* @{ - foreach (AssetsTableViewModel assetsTableViewModel in Model.AssetsTableViewModels) + @{ + foreach (LocationTableViewModel locationTableViewModel in Model.LocationTableViewModels) { - @assetsTableViewModel.UserUID - @assetsTableViewModel.AssetCn - @assetsTableViewModel.AssetName - @assetsTableViewModel.LocationName + @locationTableViewModel.LocationID + @locationTableViewModel.LocationName + @locationTableViewModel.RoomNumber + @locationTableViewModel.Seat
- +