Update Inv
This commit is contained in:
parent
01ba9a6629
commit
d039e7d7da
@ -258,9 +258,10 @@
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
// 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
|
||||
@ -268,12 +269,31 @@
|
||||
.remove()
|
||||
.draw();
|
||||
} else {
|
||||
alert(result.message);
|
||||
// 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.";
|
||||
}
|
||||
}
|
||||
alert(errorMessage);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error deleting manufacturer:", error);
|
||||
alert("An error occurred while deleting the manufacturer.");
|
||||
alert("An error occurred while trying to delete the manufacturer. Please check your network connection or try again.");
|
||||
}
|
||||
finally {
|
||||
this.loading = false;
|
||||
|
||||
@ -25,7 +25,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@* Supplier Gender *@
|
||||
@* Supplier Address *@
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-3">Supplier Address:</label>
|
||||
<div class="col-sm-9">
|
||||
@ -101,7 +101,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@* Supplier Gender *@
|
||||
@* Supplier Address *@
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-3">Supplier Address:</label>
|
||||
<div class="col-sm-9">
|
||||
@ -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,
|
||||
|
||||
@ -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." });
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user