mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
CRUD - Assets create
This commit is contained in:
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Berufsschule_HAM.Models;
|
||||
using Berufsschule_HAM.Services;
|
||||
using System.Text.Json;
|
||||
using Novell.Directory.Ldap;
|
||||
|
||||
[Route("[controller]")]
|
||||
public class AssetsController : Controller
|
||||
@@ -22,6 +23,55 @@ public class AssetsController : Controller
|
||||
return list;
|
||||
}
|
||||
|
||||
[HttpPost("Create")]
|
||||
public async Task<bool> Create(AssetsCreateRequestModel assetModel)
|
||||
{
|
||||
if (assetModel is null)
|
||||
{
|
||||
_logger.LogError("Unable to create an asset because the AssetModel is null.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var ownerDn = $"uid={assetModel.Owner},ou=people,dc=example,dc=com";
|
||||
|
||||
var attributeSet = new LdapAttributeSet();
|
||||
attributeSet.Add(new LdapAttribute("objectClass", new[] {
|
||||
"top",
|
||||
"device",
|
||||
"extensibleObject"
|
||||
}));
|
||||
attributeSet.Add(new LdapAttribute("cn", assetModel.Cn));
|
||||
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber));
|
||||
attributeSet.Add(new LdapAttribute("l", assetModel.Location));
|
||||
attributeSet.Add(new LdapAttribute("owner", ownerDn));
|
||||
attributeSet.Add(new LdapAttribute("name", assetModel.Name));
|
||||
attributeSet.Add(new LdapAttribute(
|
||||
"description",
|
||||
JsonSerializer.Serialize(new AssetDescription()
|
||||
{
|
||||
Type = assetModel.Description.Type,
|
||||
Purchase = new AssetPurchase
|
||||
{
|
||||
PurchasedAt = assetModel.Description.Purchase.PurchasedAt,
|
||||
PurchaseDate = assetModel.Description.Purchase.PurchaseDate,
|
||||
PurchaseValue = assetModel.Description.Purchase.PurchaseValue
|
||||
}
|
||||
})
|
||||
));
|
||||
|
||||
await _ldap.CreateAsset(attributeSet);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Unable to create an asset because the Exception: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("Delete")]
|
||||
public async Task<bool> Delete(string cn)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user