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.Models;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Text.Json;
[Route("[controller]")]
public class LocationsController : Controller
{
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")]
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;
}
}