Update Inv
This commit is contained in:
parent
01ba9a6629
commit
d039e7d7da
@ -247,38 +247,58 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteManufacturer(manufacturerId) {
|
async deleteManufacturer(manufacturerId) {
|
||||||
if (!confirm("Are you sure you want to delete this manufacturer?")) {
|
if (!confirm("Are you sure you want to delete this manufacturer?")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/InvMainAPI/DeleteManufacturer/${manufacturerId}`, {
|
const response = await fetch(`/InvMainAPI/DeleteManufacturer/${manufacturerId}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const result = await response.json();
|
|
||||||
|
|
||||||
if (result.success) {
|
// Check if the response was successful (status 2xx)
|
||||||
alert(result.message);
|
if (response.ok) {
|
||||||
// Remove the row from DataTables
|
const result = await response.json(); // Expect a success JSON object
|
||||||
this.manufacturerDatatable
|
alert(result.message);
|
||||||
.row($(`.delete-btn[data-id="${manufacturerId}"]`).closest('tr'))
|
// Remove the row from DataTables
|
||||||
.remove()
|
this.manufacturerDatatable
|
||||||
.draw();
|
.row($(`.delete-btn[data-id="${manufacturerId}"]`).closest('tr'))
|
||||||
} else {
|
.remove()
|
||||||
alert(result.message);
|
.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.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
alert(errorMessage);
|
||||||
catch (error) {
|
|
||||||
console.error("Error deleting manufacturer:", error);
|
|
||||||
alert("An error occurred while deleting the manufacturer.");
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,7 +25,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@* Supplier Gender *@
|
@* Supplier Address *@
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-3">Supplier Address:</label>
|
<label class="col-sm-3">Supplier Address:</label>
|
||||||
<div class="col-sm-9">
|
<div class="col-sm-9">
|
||||||
@ -101,7 +101,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@* Supplier Gender *@
|
@* Supplier Address *@
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label class="col-sm-3">Supplier Address:</label>
|
<label class="col-sm-3">Supplier Address:</label>
|
||||||
<div class="col-sm-9">
|
<div class="col-sm-9">
|
||||||
@ -199,7 +199,7 @@
|
|||||||
supplierPIC : null,
|
supplierPIC : null,
|
||||||
suppliers: null,
|
suppliers: null,
|
||||||
supplierDatatable: null,
|
supplierDatatable: null,
|
||||||
gender: ["Male", "Female", "Helicopter"],
|
gender: ["Male", "Female"],
|
||||||
registerSupplierForm: false,
|
registerSupplierForm: false,
|
||||||
editSection: false,
|
editSection: false,
|
||||||
supplierId: null,
|
supplierId: null,
|
||||||
@ -244,7 +244,6 @@
|
|||||||
$('#loadingModal').modal('show');
|
$('#loadingModal').modal('show');
|
||||||
// Create the payload
|
// Create the payload
|
||||||
const formData = {
|
const formData = {
|
||||||
supplierId: this.supplierId,
|
|
||||||
supplierCompName: this.supplierCompName,
|
supplierCompName: this.supplierCompName,
|
||||||
supplierEmail: this.supplierEmail,
|
supplierEmail: this.supplierEmail,
|
||||||
supplierPIC: this.supplierPIC,
|
supplierPIC: this.supplierPIC,
|
||||||
|
|||||||
@ -14,6 +14,7 @@ using System.Data;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using static System.Collections.Specialized.BitVector32;
|
using static System.Collections.Specialized.BitVector32;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
namespace PSTW_CentralSystem.Controllers.API.Inventory
|
namespace PSTW_CentralSystem.Controllers.API.Inventory
|
||||||
{
|
{
|
||||||
@ -152,13 +153,35 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
|
|||||||
var manufacturer = await _centralDbContext.Manufacturers.FindAsync(id);
|
var manufacturer = await _centralDbContext.Manufacturers.FindAsync(id);
|
||||||
if (manufacturer == null)
|
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);
|
try
|
||||||
await _centralDbContext.SaveChangesAsync();
|
{
|
||||||
|
_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
|
#endregion Manufacturer
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user