mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #203 from LD-Reborn/feature/issue_82_ReturnModels_Location
Add dedicated Models and improved logging for loactions Controller
This commit is contained in:
@@ -20,14 +20,22 @@ public class LocationsController : Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("Index")]
|
[HttpGet("Index")]
|
||||||
public async Task<IEnumerable<LocationModel>> Index()
|
public async Task<LocationsIndexResponseModel> Index()
|
||||||
{
|
{
|
||||||
IEnumerable<LocationModel> list = await _ldap.ListLocationsAsync();
|
try
|
||||||
return list;
|
{
|
||||||
|
IEnumerable<LocationModel> list = await _ldap.ListLocationsAsync();
|
||||||
|
return new LocationsIndexResponseModel { Locations = list };
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to list locations: {Message} - {StackTrace}", ex.Message, ex.StackTrace);
|
||||||
|
return new LocationsIndexResponseModel { Locations = [] };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("Create")]
|
[HttpPost("Create")]
|
||||||
public async Task<bool> Create([FromBody]LocationsCreateRequestModel model)
|
public async Task<LocationsCreateResponseModel> Create([FromBody]LocationsCreateRequestModel model)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -42,19 +50,23 @@ public class LocationsController : Controller
|
|||||||
];
|
];
|
||||||
|
|
||||||
await _ldap.CreateLocation(attributeSet);
|
await _ldap.CreateLocation(attributeSet);
|
||||||
return true;
|
return new LocationsCreateResponseModel { Success = true };
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError("Unable to create location: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
|
_logger.LogError("Unable to create location: {Message} - {StackTrace}", ex.Message, ex.StackTrace);
|
||||||
return false;
|
return new LocationsCreateResponseModel { Success = false, ErrorMessage = ex.Message };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("Delete")]
|
[HttpGet("Delete")]
|
||||||
public async Task<LocationsDeleteRequestModel> DeleteAsync(string cn)
|
public async Task<LocationsDeleteResponseModel> DeleteAsync(string cn)
|
||||||
{
|
{
|
||||||
if (cn is null) { return new(false); }
|
if (cn is null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to delete location: cn parameter is null");
|
||||||
|
return new(false, "cn parameter is null");
|
||||||
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _ldap.DeleteLocationAsync(cn);
|
await _ldap.DeleteLocationAsync(cn);
|
||||||
@@ -62,17 +74,18 @@ public class LocationsController : Controller
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
_logger.LogError("Unable to delete location {Cn}: {Message} - {StackTrace}", cn, ex.Message, ex.StackTrace);
|
||||||
return new(false, ex.Message);
|
return new(false, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("Update")]
|
[HttpPost("Update")]
|
||||||
public async Task<bool> Update([FromBody]LocationsModifyRequestModel requestModel)
|
public async Task<LocationsUpdateResponseModel> Update([FromBody]LocationsModifyRequestModel requestModel)
|
||||||
{
|
{
|
||||||
if (requestModel is null)
|
if (requestModel is null)
|
||||||
{
|
{
|
||||||
_logger.LogError("Unable to update a location because the LocationsModifyRequestModel is null");
|
_logger.LogError("Unable to update a location because the LocationsModifyRequestModel is null");
|
||||||
return false;
|
return new LocationsUpdateResponseModel { Success = false, ErrorMessage = "Request model is null" };
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -82,12 +95,12 @@ public class LocationsController : Controller
|
|||||||
|
|
||||||
await _ldap.UpdateLocation(location, "description", JsonSerializer.Serialize(room));
|
await _ldap.UpdateLocation(location, "description", JsonSerializer.Serialize(room));
|
||||||
await _ldap.UpdateLocation(location, "l", newLocation);
|
await _ldap.UpdateLocation(location, "l", newLocation);
|
||||||
return true;
|
return new LocationsUpdateResponseModel { Success = true };
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError("Unable to update location: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
|
_logger.LogError("Unable to update location {Location}: {Message} - {StackTrace}", requestModel?.Location ?? "unknown", ex.Message, ex.StackTrace);
|
||||||
return false;
|
return new LocationsUpdateResponseModel { Success = false, ErrorMessage = ex.Message };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,8 @@ public class LocationsModifyRequestModel
|
|||||||
public required LocationsDescription Description { get; set; }
|
public required LocationsDescription Description { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LocationsDeleteRequestModel(bool successful, string exception = "None")
|
public class LocationsDeleteRequestModel
|
||||||
{
|
{
|
||||||
public bool Success { get; set; } = successful;
|
[JsonPropertyName("cn")]
|
||||||
|
public required string Cn { get; set; }
|
||||||
public string? Exception { get; set; } = exception;
|
|
||||||
}
|
}
|
||||||
25
src/Models/LocationsResponseModels.cs
Normal file
25
src/Models/LocationsResponseModels.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
namespace Berufsschule_HAM.Models;
|
||||||
|
|
||||||
|
public class LocationsDeleteResponseModel(bool successful, string exception = "None")
|
||||||
|
{
|
||||||
|
public bool Success { get; set; } = successful;
|
||||||
|
|
||||||
|
public string? Exception { get; set; } = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LocationsIndexResponseModel
|
||||||
|
{
|
||||||
|
public required IEnumerable<LocationModel> Locations { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LocationsCreateResponseModel
|
||||||
|
{
|
||||||
|
public required bool Success { get; set; }
|
||||||
|
public string? ErrorMessage { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LocationsUpdateResponseModel
|
||||||
|
{
|
||||||
|
public required bool Success { get; set; }
|
||||||
|
public string? ErrorMessage { get; set; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user