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 System.Text.Json;
|
||||||
using Novell.Directory.Ldap;
|
using Novell.Directory.Ldap;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
@@ -18,20 +19,34 @@ public class AssetsController : Controller
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("Index")]
|
[HttpGet("GetAll")]
|
||||||
public async Task<IEnumerable<AssetModel>> Index()
|
public async Task<AssetsIndexResponseModel> GetAllAssetModelAsync()
|
||||||
{
|
{
|
||||||
var list = await _ldap.ListDeviceAsync();
|
AssetsIndexResponseModel result;
|
||||||
return list;
|
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")]
|
[HttpPost("Create")]
|
||||||
public async Task<bool> Create(AssetsCreateRequestModel assetModel)
|
public async Task<AssetsCreateResponseModel> Create(AssetsCreateRequestModel assetModel)
|
||||||
{
|
{
|
||||||
|
AssetsCreateResponseModel result;
|
||||||
if (assetModel is null)
|
if (assetModel is null)
|
||||||
{
|
{
|
||||||
_logger.LogError("Unable to create an asset because the AssetModel is null.");
|
result = new AssetsCreateResponseModel(
|
||||||
return false;
|
successful: false,
|
||||||
|
exception: "Unable to create an asset because the AssetsCreateRequestModel is null.");
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -97,52 +112,58 @@ public class AssetsController : Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _ldap.CreateAsset(attributeSet);
|
await _ldap.CreateAsset(attributeSet);
|
||||||
|
result = new AssetsCreateResponseModel(successful: true);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.LogError($"Unable to create an asset because of the exception: {e.Message}", e);
|
result = new AssetsCreateResponseModel(successful: false, exception: e.Message);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("Delete")]
|
return result;
|
||||||
public async Task<AssetsDeleteResponseModel> Delete(string cn)
|
}
|
||||||
|
|
||||||
|
[HttpDelete("Delete")]
|
||||||
|
public async Task<AssetsDeleteResponseModel> Delete([BindRequired] string cn)
|
||||||
{
|
{
|
||||||
AssetsDeleteResponseModel response;
|
AssetsDeleteResponseModel response;
|
||||||
return await Task.Run(async () =>
|
return await Task.Run(async () =>
|
||||||
{
|
{
|
||||||
if (cn is null)
|
if (cn is null)
|
||||||
{
|
{
|
||||||
response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.CnIsNull);
|
response = new AssetsDeleteResponseModel(successful: false, exception: "Cn was not provided.");
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _ldap.DeleteAssetAsync(cn);
|
await _ldap.DeleteAssetAsync(cn);
|
||||||
response = new AssetsDeleteResponseModel(true, AssetsDeleteErrorEnum.None);
|
response = new AssetsDeleteResponseModel(true);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.UnableToDeleteAsset, e.Message);
|
response = new AssetsDeleteResponseModel(successful: false, exception: e.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("Update")]
|
[HttpPatch("Update")]
|
||||||
public async Task<bool> Update(AssetsModifyRequestModel requestModel)
|
public async Task<AssetsUpdateResponseModel> Update(AssetsModifyRequestModel requestModel)
|
||||||
{
|
{
|
||||||
|
AssetsUpdateResponseModel result;
|
||||||
if (requestModel is null)
|
if (requestModel is null)
|
||||||
{
|
{
|
||||||
_logger.LogError("Unable to update an asset because the AssetsModifyRequestModel is null");
|
result = new AssetsUpdateResponseModel(
|
||||||
return false;
|
successful: false,
|
||||||
}
|
exception: "Unable to update an asset because the AssetsModifyRequestModel is null");
|
||||||
string cn = requestModel.Cn;
|
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cn = requestModel.Cn;
|
||||||
if (requestModel.NewCn is not null)
|
if (requestModel.NewCn is not null)
|
||||||
{
|
{
|
||||||
await _ldap.UpdateAsset(cn, "cn", requestModel.NewCn);
|
await _ldap.UpdateAsset(cn, "cn", requestModel.NewCn);
|
||||||
@@ -188,6 +209,14 @@ public class AssetsController : Controller
|
|||||||
}
|
}
|
||||||
await _ldap.UpdateAsset(cn, "description", JsonSerializer.Serialize(requestModel.Description));
|
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