Added Locations update CRUD element, fixed locations index missing cn attribute

This commit is contained in:
2025-10-03 22:13:39 +02:00
parent 7eff665f81
commit 4d0d3ac473
5 changed files with 107 additions and 5 deletions

View File

@@ -1,13 +1,19 @@
// Controllers/LocationsController.cs
using Berufsschule_HAM.Services; using Berufsschule_HAM.Services;
using Berufsschule_HAM.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks; using System.Text.Json;
[Route("[controller]")] [Route("[controller]")]
public class LocationsController : Controller public class LocationsController : Controller
{ {
private readonly LdapService _ldap; private readonly LdapService _ldap;
public LocationsController(LdapService ldap) => _ldap = ldap; private readonly ILogger<LocationsController> _logger;
public LocationsController(LdapService ldap, ILogger<LocationsController> logger)
{
_ldap = ldap;
_logger = logger;
}
[HttpGet("Index")] [HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index() public async Task<IEnumerable<Dictionary<string, string>>> Index()
@@ -33,5 +39,45 @@ public class LocationsController : Controller
} }
}); });
} }
[HttpPost("Update")]
public async Task<bool> Update(LocationsModifyRequestModel requestModel)
{
if (requestModel is null)
{
_logger.LogError("Unable to update a location because the LocationsModifyRequestModel is null");
return false;
}
string cn = requestModel.Cn;
if (requestModel.NewCn is not null)
{
await _ldap.UpdateLocation(cn, "cn", requestModel.NewCn);
cn = requestModel.NewCn;
}
if (requestModel.Location is not null)
{
await _ldap.UpdateLocation(cn, "location", requestModel.Location);
}
if (requestModel.Street is not null)
{
await _ldap.UpdateLocation(cn, "street", requestModel.Street);
}
if (requestModel.Description is not null)
{
LocationsDescription description = requestModel.Description;
LocationModel? location = null;
if (description.Seat is null)
{
location ??= await _ldap.GetLocationByCnAsync(cn);
description.Seat = location.Description?.Seat;
}
else if (description.RoomNumber is null)
{
location ??= await _ldap.GetLocationByCnAsync(cn);
description.RoomNumber = location.Description?.RoomNumber;
}
await _ldap.UpdateLocation(cn, "description", JsonSerializer.Serialize(requestModel.Description));
}
return true;
}
} }

View File

@@ -1,3 +1,5 @@
namespace Berufsschule_HAM.Exceptions; namespace Berufsschule_HAM.Exceptions;
public class GroupModelConfigurationException : Exception { } public class GroupModelConfigurationException : Exception { }
public class LocationModelConfigurationException : Exception { }

View File

@@ -0,0 +1,31 @@
namespace Berufsschule_HAM.Models;
using System.Text.Json;
using Berufsschule_HAM.Exceptions;
public class LocationModel
{
public required string Cn { get; set; }
public LocationsDescription? Description { get; set; }
public string? Location { get; set; }
public string? Street { get; set; }
public LocationModel(Dictionary<string, string> ldapData)
{
Cn = ldapData.GetValueOrDefault("cn") ?? throw new LocationModelConfigurationException();
Location = ldapData.GetValueOrDefault("l");
Street = ldapData.GetValueOrDefault("street");
string? descriptionValue = ldapData.GetValueOrDefault("description");
if (descriptionValue is null)
{
Description = new();
}
else
{
Description = JsonSerializer.Deserialize<LocationsDescription>(descriptionValue);
}
}
}
public class LocationsDescription
{
public string? RoomNumber { get; set; }
public string? Seat { get; set; }
}

View File

@@ -0,0 +1,10 @@
namespace Berufsschule_HAM.Models;
public class LocationsModifyRequestModel
{
public required string Cn { get; set; }
public string? NewCn { get; set; } = null;
public LocationsDescription? Description { get; set; } = null;
public string? Location { get; set; } = null;
public string? Street { get; set; } = null;
}

View File

@@ -46,7 +46,7 @@ public partial class LdapService : IDisposable
public string GroupsBaseDn => string.IsNullOrEmpty(_opts.GroupsOu) ? _opts.BaseDn : $"{_opts.GroupsOu},{_opts.BaseDn}"; public string GroupsBaseDn => string.IsNullOrEmpty(_opts.GroupsOu) ? _opts.BaseDn : $"{_opts.GroupsOu},{_opts.BaseDn}";
public string MigrationsBaseDn => string.IsNullOrEmpty(_opts.MigrationsOu) ? _opts.BaseDn : $"{_opts.MigrationsOu},{_opts.BaseDn}"; public string MigrationsBaseDn => string.IsNullOrEmpty(_opts.MigrationsOu) ? _opts.BaseDn : $"{_opts.MigrationsOu},{_opts.BaseDn}";
public string[] UsersAttributes => ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]; public string[] UsersAttributes => ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"];
public string[] LocationsAttributes => ["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<Dictionary<string, string>>> ListLocationsAsync()
{ {
@@ -139,6 +139,14 @@ public partial class LdapService : IDisposable
return new GroupModel((await ListObjectBy(GroupsBaseDn, $"cn={cn}", attributes)).First()) { Cn = cn }; return new GroupModel((await ListObjectBy(GroupsBaseDn, $"cn={cn}", attributes)).First()) { Cn = cn };
} }
public async Task<LocationModel> GetLocationByCnAsync(string cn)
{
return await GetLocationByCnAsync(cn, LocationsAttributes);
}
public async Task<LocationModel> GetLocationByCnAsync(string cn, string[] attributes)
{
return new LocationModel((await ListObjectBy(LocationsBaseDn, $"cn={cn}", attributes)).First()) { Cn = cn };
}
public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync() public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync()
{ {
@@ -285,6 +293,11 @@ public partial class LdapService : IDisposable
await UpdateObject(GroupsBaseDn, "cn", cn, attributeName, attributeValue); await UpdateObject(GroupsBaseDn, "cn", cn, attributeName, attributeValue);
} }
public async Task UpdateLocation(string cn, string attributeName, string attributeValue)
{
await UpdateObject(LocationsBaseDn, "cn", cn, attributeName, attributeValue);
}
public async Task UpdateObject(string baseDn, string rdnKey, string rdnValue, string attributeName, string attributeValue) public async Task UpdateObject(string baseDn, string rdnKey, string rdnValue, string attributeName, string attributeValue)
{ {
await ConnectAndBind(); await ConnectAndBind();