diff --git a/Areas/Inventory/Views/InventoryMaster/ManifacturerRegistration.cshtml b/Areas/Inventory/Views/InventoryMaster/ManifacturerRegistration.cshtml index 6a7f51a..5c826c4 100644 --- a/Areas/Inventory/Views/InventoryMaster/ManifacturerRegistration.cshtml +++ b/Areas/Inventory/Views/InventoryMaster/ManifacturerRegistration.cshtml @@ -247,38 +247,58 @@ } }, - async deleteManufacturer(manufacturerId) { - if (!confirm("Are you sure you want to delete this manufacturer?")) { - return; - } - try { - const response = await fetch(`/InvMainAPI/DeleteManufacturer/${manufacturerId}`, { - method: 'DELETE', - headers: { - 'Content-Type': 'application/json', - }, - }); - const result = await response.json(); + async deleteManufacturer(manufacturerId) { + if (!confirm("Are you sure you want to delete this manufacturer?")) { + return; + } + try { + const response = await fetch(`/InvMainAPI/DeleteManufacturer/${manufacturerId}`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + }); - if (result.success) { - alert(result.message); - // Remove the row from DataTables - this.manufacturerDatatable - .row($(`.delete-btn[data-id="${manufacturerId}"]`).closest('tr')) - .remove() - .draw(); - } else { - alert(result.message); + // Check if the response was successful (status 2xx) + if (response.ok) { + const result = await response.json(); // Expect a success JSON object + alert(result.message); + // Remove the row from DataTables + this.manufacturerDatatable + .row($(`.delete-btn[data-id="${manufacturerId}"]`).closest('tr')) + .remove() + .draw(); + } else { + // If response is not OK, try to parse JSON error message + let errorMessage = "An unknown error occurred."; + try { + const errorData = await response.json(); + errorMessage = errorData.message || errorMessage; + } catch (jsonError) { + // If parsing JSON fails, it might be a raw error message + errorMessage = await response.text(); // Get the raw text + console.error("Failed to parse error response as JSON:", jsonError, "Raw response:", errorMessage); + // A more user-friendly message for generic errors: + if (response.status === 404) { + errorMessage = "Manufacturer not found."; + } else if (response.status === 400) { + // This will catch the "This manufacturer cannot be deleted..." message from your API + errorMessage = errorMessage; // Use the message directly if it's already friendly + } else { + errorMessage = "An unexpected error occurred on the server."; + } } - } - catch (error) { - console.error("Error deleting manufacturer:", error); - alert("An error occurred while deleting the manufacturer."); + alert(errorMessage); } - finally { - this.loading = false; - } - }, + } + catch (error) { + console.error("Error deleting manufacturer:", error); + alert("An error occurred while trying to delete the manufacturer. Please check your network connection or try again."); + } + finally { + this.loading = false; + } + }, } }); diff --git a/Areas/Inventory/Views/InventoryMaster/SupplierRegistration.cshtml b/Areas/Inventory/Views/InventoryMaster/SupplierRegistration.cshtml index de8c89a..0434e0e 100644 --- a/Areas/Inventory/Views/InventoryMaster/SupplierRegistration.cshtml +++ b/Areas/Inventory/Views/InventoryMaster/SupplierRegistration.cshtml @@ -25,7 +25,7 @@ - @* Supplier Gender *@ + @* Supplier Address *@
@@ -101,7 +101,7 @@
- @* Supplier Gender *@ + @* Supplier Address *@
@@ -199,7 +199,7 @@ supplierPIC : null, suppliers: null, supplierDatatable: null, - gender: ["Male", "Female", "Helicopter"], + gender: ["Male", "Female"], registerSupplierForm: false, editSection: false, supplierId: null, @@ -244,7 +244,6 @@ $('#loadingModal').modal('show'); // Create the payload const formData = { - supplierId: this.supplierId, supplierCompName: this.supplierCompName, supplierEmail: this.supplierEmail, supplierPIC: this.supplierPIC, diff --git a/Controllers/API/Inventory/InvMainAPI.cs b/Controllers/API/Inventory/InvMainAPI.cs index bb5f902..27c7998 100644 --- a/Controllers/API/Inventory/InvMainAPI.cs +++ b/Controllers/API/Inventory/InvMainAPI.cs @@ -14,6 +14,7 @@ using System.Data; using System.Diagnostics; using System.Reflection; using static System.Collections.Specialized.BitVector32; +using System.Data.SqlClient; namespace PSTW_CentralSystem.Controllers.API.Inventory { @@ -152,13 +153,35 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory var manufacturer = await _centralDbContext.Manufacturers.FindAsync(id); if (manufacturer == null) { - return NotFound(new { success = false, message = "Manufacturer not found" }); + return NotFound(new { success = false, message = "Manufacturer not found." }); } - _centralDbContext.Manufacturers.Remove(manufacturer); - await _centralDbContext.SaveChangesAsync(); + try + { + _centralDbContext.Manufacturers.Remove(manufacturer); + await _centralDbContext.SaveChangesAsync(); - return Ok(new { success = true, message = "Manufacturer deleted successfully" }); + return Ok(new { success = true, message = "Manufacturer deleted successfully." }); + } + catch (Microsoft.EntityFrameworkCore.DbUpdateException ex) + { + Console.Error.WriteLine($"DbUpdateException: {ex.Message}"); + if (ex.InnerException != null) + { + Console.Error.WriteLine($"Inner Exception: {ex.InnerException.Message}"); + // Using the newer Microsoft.Data.SqlClient namespace + if (ex.InnerException is Microsoft.Data.SqlClient.SqlException sqlEx && sqlEx.Number == 547) + { + return BadRequest(new { success = false, message = "This manufacturer cannot be deleted as it is associated with other items (e.g., products). Please remove all associated items first." }); + } + } + return BadRequest(new { success = false, message = "This manufacturer cannot be deleted due to existing related data." }); + } + catch (Exception ex) + { + Console.Error.WriteLine($"General Exception: {ex.Message}"); + return StatusCode(500, new { success = false, message = "An error occurred while deleting the manufacturer: " + ex.Message }); + } } #endregion Manufacturer