From 2b5527a76094771d58b2483ad8d1270d98e8b415 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Fri, 10 Oct 2025 10:22:16 +0200 Subject: [PATCH] Fixed Asset delete wrong HTTP method, Added Asset creation in /Home/Assets --- src/Controllers/AssetsController.cs | 2 +- src/Views/Home/Assets.cshtml | 283 ++++++++++++++++++++++++---- 2 files changed, 243 insertions(+), 42 deletions(-) diff --git a/src/Controllers/AssetsController.cs b/src/Controllers/AssetsController.cs index 463a23d..584e3ae 100644 --- a/src/Controllers/AssetsController.cs +++ b/src/Controllers/AssetsController.cs @@ -38,7 +38,7 @@ public class AssetsController : Controller } [HttpPost("Create")] - public async Task Create(AssetsCreateRequestModel assetModel) + public async Task Create([FromBody]AssetsCreateRequestModel assetModel) { AssetsCreateResponseModel result; if (assetModel is null) diff --git a/src/Views/Home/Assets.cshtml b/src/Views/Home/Assets.cshtml index 4673f82..708dafe 100644 --- a/src/Views/Home/Assets.cshtml +++ b/src/Views/Home/Assets.cshtml @@ -12,7 +12,9 @@
- +
@@ -20,11 +22,11 @@ - - - - - + + + + + @@ -104,7 +106,7 @@ try { const response = await fetch(url, { - method: 'GET', + method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' @@ -161,45 +163,244 @@ } }); -@* *@ + - + // Delegate delete button click + attributesContainer.addEventListener('click', (e) => { + if (e.target.classList.contains('btn-remove-attribute')) { + e.target.closest('.attribute-row').remove(); + } + }); + + // 🔧 Hook into the existing submit handler to include attributes + const createForm = document.getElementById('createAssetForm'); + const originalHandler = createForm.onsubmit; + createForm.addEventListener('submit', (e) => { + // Let’s patch the JSON generation just before sending + const formData = new FormData(createForm); + const jsonData = {}; + + for (const [key, value] of formData.entries()) { + if (!value) continue; + const keys = key.split('.'); + let target = jsonData; + for (let i = 0; i < keys.length - 1; i++) { + target[keys[i]] = target[keys[i]] || {}; + target = target[keys[i]]; + } + target[keys[keys.length - 1]] = value; + } + + // Add Attributes as Dictionary + const attributes = {}; + document.querySelectorAll('#attributesContainer .attribute-row').forEach(row => { + const name = row.querySelector('[data-attr-name]').value.trim(); + const value = row.querySelector('[data-attr-value]').value.trim(); + if (name) attributes[name] = value; + }); + + if (Object.keys(attributes).length > 0) { + jsonData.Description = jsonData.Description || {}; + jsonData.Description.Attributes = attributes; + } + + // Replace the body before sending + e.preventDefault(); // stop default form submit + + fetch('/Assets/Create', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: JSON.stringify(jsonData) + }) + .then(res => res.json()) + .then(result => { + const createModalEl = document.getElementById('createAssetModal'); + if (result.success) { + bootstrap.Modal.getInstance(createModalEl).hide(); + createForm.reset(); + attributesContainer.innerHTML = ''; + showToast('✅ Asset created successfully', 'success'); + } else { + showToast(`❌ ${result.reason || 'Error creating asset'}`, 'danger'); + } + }) + .catch(err => { + console.error(err); + showToast('Error contacting server', 'danger'); + }); + }); + }); + \ No newline at end of file
UserAsset IDAsset NameLocationAction@T["Owner"]@T["Asset ID"]@T["Asset Name"]@T["Location"]@T["Action"]