Added Locations Create CRUD element, Updated Locations LDAP service methods

This commit is contained in:
2025-10-08 21:06:04 +02:00
parent 0ed9397280
commit ba7cde8adb
5 changed files with 82 additions and 54 deletions

View File

@@ -3,6 +3,8 @@ using Berufsschule_HAM.Models;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Novell.Directory.Ldap;
using Berufsschule_HAM.Helpers;
[Authorize]
[Route("[controller]")]
@@ -24,6 +26,31 @@ public class LocationsController : Controller
return list;
}
[HttpGet("Create")]
public async Task<bool> Create(LocationsCreateRequestModel model)
{
try
{
LocationsDescription room = model.LocationsDescription;
string location = StringHelpers.Slugify(room.Location + " " + room.RoomNumber + " " + room.Seat);
LdapAttributeSet attributeSet =
[
new LdapAttribute("objectClass", "locality"),
new LdapAttribute("objectClass", "top"),
new LdapAttribute("l", location),
new LdapAttribute("description", JsonSerializer.Serialize(room))
];
await _ldap.CreateLocation(attributeSet);
return true;
}
catch (Exception ex)
{
_logger.LogError("Unable to create location: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
return false;
}
}
[HttpGet("Delete")]
public async Task<bool> Delete(string cn)
{
@@ -49,37 +76,20 @@ public class LocationsController : Controller
_logger.LogError("Unable to update a location because the LocationsModifyRequestModel is null");
return false;
}
string cn = requestModel.Cn;
try
{
string location = requestModel.Location;
LocationsDescription room = requestModel.Description;
string newLocation = StringHelpers.Slugify(room.Location + " " + room.RoomNumber + " " + room.Seat); // TODO: fix DRY violation
if (requestModel.NewCn is not null)
{
await _ldap.UpdateLocation(cn, "cn", requestModel.NewCn);
cn = requestModel.NewCn;
await _ldap.UpdateLocation(location, "description", JsonSerializer.Serialize(room));
await _ldap.UpdateLocation(location, "l", newLocation);
return true;
}
if (requestModel.Location is not null)
catch (Exception ex)
{
await _ldap.UpdateLocation(cn, "location", requestModel.Location);
_logger.LogError("Unable to update location: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
return false;
}
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;
}
}