mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #89 from LD-Reborn/feature/Create_AssetsResponseModel
Feature: Add return model to AssetsController
This commit is contained in:
@@ -4,6 +4,7 @@ using Berufsschule_HAM.Services;
|
||||
using System.Text.Json;
|
||||
using Novell.Directory.Ldap;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
@@ -18,20 +19,34 @@ public class AssetsController : Controller
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("Index")]
|
||||
public async Task<IEnumerable<AssetModel>> Index()
|
||||
[HttpGet("GetAll")]
|
||||
public async Task<AssetsIndexResponseModel> GetAllAssetModelAsync()
|
||||
{
|
||||
var list = await _ldap.ListDeviceAsync();
|
||||
return list;
|
||||
AssetsIndexResponseModel result;
|
||||
try
|
||||
{
|
||||
var assetList = await _ldap.ListDeviceAsync();
|
||||
result = new AssetsIndexResponseModel(successful: true, assetsModel: assetList);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = new AssetsIndexResponseModel(successful: false, exception: e.Message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[HttpPost("Create")]
|
||||
public async Task<bool> Create(AssetsCreateRequestModel assetModel)
|
||||
public async Task<AssetsCreateResponseModel> Create(AssetsCreateRequestModel assetModel)
|
||||
{
|
||||
AssetsCreateResponseModel result;
|
||||
if (assetModel is null)
|
||||
{
|
||||
_logger.LogError("Unable to create an asset because the AssetModel is null.");
|
||||
return false;
|
||||
result = new AssetsCreateResponseModel(
|
||||
successful: false,
|
||||
exception: "Unable to create an asset because the AssetsCreateRequestModel is null.");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
@@ -97,52 +112,58 @@ public class AssetsController : Controller
|
||||
}
|
||||
|
||||
await _ldap.CreateAsset(attributeSet);
|
||||
|
||||
return true;
|
||||
result = new AssetsCreateResponseModel(successful: true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError($"Unable to create an asset because of the exception: {e.Message}", e);
|
||||
return false;
|
||||
}
|
||||
result = new AssetsCreateResponseModel(successful: false, exception: e.Message);
|
||||
}
|
||||
|
||||
[HttpGet("Delete")]
|
||||
public async Task<AssetsDeleteResponseModel> Delete(string cn)
|
||||
return result;
|
||||
}
|
||||
|
||||
[HttpDelete("Delete")]
|
||||
public async Task<AssetsDeleteResponseModel> Delete([BindRequired] string cn)
|
||||
{
|
||||
AssetsDeleteResponseModel response;
|
||||
return await Task.Run(async () =>
|
||||
{
|
||||
if (cn is null)
|
||||
{
|
||||
response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.CnIsNull);
|
||||
response = new AssetsDeleteResponseModel(successful: false, exception: "Cn was not provided.");
|
||||
return response;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _ldap.DeleteAssetAsync(cn);
|
||||
response = new AssetsDeleteResponseModel(true, AssetsDeleteErrorEnum.None);
|
||||
response = new AssetsDeleteResponseModel(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.UnableToDeleteAsset, e.Message);
|
||||
response = new AssetsDeleteResponseModel(successful: false, exception: e.Message);
|
||||
}
|
||||
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("Update")]
|
||||
public async Task<bool> Update(AssetsModifyRequestModel requestModel)
|
||||
[HttpPatch("Update")]
|
||||
public async Task<AssetsUpdateResponseModel> Update(AssetsModifyRequestModel requestModel)
|
||||
{
|
||||
AssetsUpdateResponseModel result;
|
||||
if (requestModel is null)
|
||||
{
|
||||
_logger.LogError("Unable to update an asset because the AssetsModifyRequestModel is null");
|
||||
return false;
|
||||
}
|
||||
string cn = requestModel.Cn;
|
||||
result = new AssetsUpdateResponseModel(
|
||||
successful: false,
|
||||
exception: "Unable to update an asset because the AssetsModifyRequestModel is null");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var cn = requestModel.Cn;
|
||||
if (requestModel.NewCn is not null)
|
||||
{
|
||||
await _ldap.UpdateAsset(cn, "cn", requestModel.NewCn);
|
||||
@@ -188,6 +209,14 @@ public class AssetsController : Controller
|
||||
}
|
||||
await _ldap.UpdateAsset(cn, "description", JsonSerializer.Serialize(requestModel.Description));
|
||||
}
|
||||
return true;
|
||||
|
||||
result = new AssetsUpdateResponseModel(successful: true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result = new AssetsUpdateResponseModel(successful: false, exception: e.Message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
public class AssetsDeleteResponseModel(bool successful, AssetsDeleteErrorEnum errorReason, string exception = "none")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
public AssetsDeleteErrorEnum Reason { get; set; } = errorReason;
|
||||
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public enum AssetsDeleteErrorEnum
|
||||
{
|
||||
None,
|
||||
CnIsNull,
|
||||
UnableToDeleteAsset
|
||||
}
|
||||
32
src/Models/AssetsResponseModel.cs
Normal file
32
src/Models/AssetsResponseModel.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Berufsschule_HAM.Models;
|
||||
|
||||
public class AssetsCreateResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class AssetsUpdateResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class AssetsDeleteResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class AssetsIndexResponseModel(bool successful, IEnumerable<AssetModel>? assetsModel = null, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
|
||||
public IEnumerable<AssetModel>? AssetsModel { get; set; } = assetsModel;
|
||||
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user