Implemented live asset table in /Home/Index

This commit is contained in:
2025-10-05 21:29:17 +02:00
parent ee44b7d8c8
commit 43cac9e304
7 changed files with 60 additions and 50 deletions

View File

@@ -19,7 +19,7 @@ public class AssetsController : Controller
} }
[HttpGet("Index")] [HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index() public async Task<IEnumerable<AssetModel>> Index()
{ {
var list = await _ldap.ListDeviceAsync(); var list = await _ldap.ListDeviceAsync();
return list; return list;

View File

@@ -22,9 +22,23 @@ public class HomeController : Controller
[Authorize] [Authorize]
[HttpGet("Index")] [HttpGet("Index")]
[HttpGet("/")] [HttpGet("/")]
public IActionResult Index() public async Task<IActionResult> Index()
{ {
return View(); IEnumerable<UserModel> users = await _ldap.ListUsersAsync();
IEnumerable<AssetModel> assets = await _ldap.ListDeviceAsync();
IEnumerable<LocationModel> locations = await _ldap.ListLocationsAsync();
List<AssetsTableViewModel> assetsTableViewModels = [];
foreach (AssetModel asset in assets)
{
assetsTableViewModels.Add(new()
{
AssetCn = asset.Cn,
AssetName = asset.Name,
LocationName = asset.Location,
UserUID = asset.Owner?.Split('=')[1],
});
}
return View(new HomeIndexViewModel() { AssetsTableViewModels = assetsTableViewModels });
} }
[HttpPost("Login")] [HttpPost("Login")]

View File

@@ -18,9 +18,9 @@ public class LocationsController : Controller
} }
[HttpGet("Index")] [HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index() public async Task<IEnumerable<LocationModel>> Index()
{ {
IEnumerable<Dictionary<string, string>> list = await _ldap.ListLocationsAsync(); IEnumerable<LocationModel> list = await _ldap.ListLocationsAsync();
return list; return list;
} }

View File

@@ -44,3 +44,11 @@ public class AssetPurchase
public string? PurchasedAt { get; set; } public string? PurchasedAt { get; set; }
public string? PurchasedBy { get; set; } public string? PurchasedBy { get; set; }
} }
public class AssetsTableViewModel
{
public string? UserUID { get; set; }
public required string AssetCn { get; set; }
public string? AssetName { get; set; }
public string? LocationName { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace Berufsschule_HAM.Models;
public class HomeIndexViewModel
{
public required IEnumerable<AssetsTableViewModel> AssetsTableViewModels { get; set; }
}

View File

@@ -49,14 +49,20 @@ public partial class LdapService : IDisposable
public string[] AssetsAttributes => ["CN", "description", "l", "owner", "serialNumber", "name"]; public string[] AssetsAttributes => ["CN", "description", "l", "owner", "serialNumber", "name"];
public string[] LocationsAttributes => ["cn", "l", "street", "description"]; public string[] LocationsAttributes => ["cn", "l", "street", "description"];
public string[] GroupsAttributes => ["cn", "gidNumber", "description"]; public string[] GroupsAttributes => ["cn", "gidNumber", "description"];
public async Task<IEnumerable<Dictionary<string, string>>> ListLocationsAsync() public async Task<IEnumerable<LocationModel>> ListLocationsAsync()
{ {
return await ListObjectBy(LocationsBaseDn, "", LocationsAttributes); IEnumerable<Dictionary<string, string>> locations = await ListObjectBy(LocationsBaseDn, "", LocationsAttributes);
List<LocationModel> models = [];
locations.ToList().ForEach(x => models.Add(new LocationModel(x) {Cn = x["cn"]}));
return models;
} }
public async Task<IEnumerable<Dictionary<string, string>>> ListUsersAsync() public async Task<IEnumerable<UserModel>> ListUsersAsync()
{ {
return await ListObjectBy(UsersBaseDn, "", UsersAttributes); IEnumerable<Dictionary<string, string>> users = await ListObjectBy(UsersBaseDn, "", UsersAttributes);
List<UserModel> models = [];
users.ToList().ForEach(x => models.Add(new UserModel(x) {Uid = x["uid"]}));
return models;
} }
public async Task<MigrationModel> GetMigrationVersionAsync() public async Task<MigrationModel> GetMigrationVersionAsync()
@@ -158,9 +164,12 @@ public partial class LdapService : IDisposable
return new AssetModel((await ListObjectBy(AssetsBaseDn, $"cn={cn}", attributes)).First()) { Cn = cn }; return new AssetModel((await ListObjectBy(AssetsBaseDn, $"cn={cn}", attributes)).First()) { Cn = cn };
} }
public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync() public async Task<IEnumerable<AssetModel>> ListDeviceAsync()
{ {
return await ListObjectBy(AssetsBaseDn, "(objectClass=device)", AssetsAttributes); IEnumerable<Dictionary<string, string>> devices = await ListObjectBy(AssetsBaseDn, "(objectClass=device)", AssetsAttributes);
List<AssetModel> models = [];
devices.ToList().ForEach(x => models.Add(new AssetModel(x) {Cn = x["cn"]}));
return models;
} }
public async Task CreateUser(string uid, LdapAttributeSet attributeSet) public async Task CreateUser(string uid, LdapAttributeSet attributeSet)

View File

@@ -1,4 +1,6 @@
@using Microsoft.AspNetCore.Mvc.Localization @using Microsoft.AspNetCore.Mvc.Localization
@using Berufsschule_HAM.Models
@model HomeIndexViewModel
@inject IViewLocalizer T @inject IViewLocalizer T
@{ @{
ViewData["Title"] = T["Home Page"]; ViewData["Title"] = T["Home Page"];
@@ -32,50 +34,19 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@{
foreach (AssetsTableViewModel assetsTableViewModel in Model.AssetsTableViewModels)
{
<tr> <tr>
<td>John Doe</td> <td>@assetsTableViewModel.UserUID</td>
<td>4827391</td> <td>@assetsTableViewModel.AssetCn</td>
<td>Lorem Ipsum dolor sit amet</td> <td>@assetsTableViewModel.AssetName</td>
<td>BTG Raum 317</td> <td>@assetsTableViewModel.LocationName</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="4827391" data-asset-id="@assetsTableViewModel.AssetCn"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
🗑️ Delete
</button>
</div>
</td>
</tr>
<tr>
<td>Max Mustermann</td>
<td>9150732</td>
<td>Lorem Ipsum dolor sit amet</td>
<td>BTG Raum 317</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="9150732"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
🗑️ Delete
</button>
</div>
</td>
</tr>
<tr>
<td>Otto Normalverbraucher</td>
<td>1642235</td>
<td>Lorem Ipsum dolor sit amet</td>
<td>BTG Raum 317</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="1642235"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#deleteModal"> data-bs-target="#deleteModal">
🗑️ Delete 🗑️ Delete
@@ -83,6 +54,8 @@
</div> </div>
</td> </td>
</tr> </tr>
}
}
</tbody> </tbody>
</table> </table>
</div> </div>