Fix productSupplier table name case sensitivity and add run configurations
This commit is contained in:
@@ -6,6 +6,7 @@ import org.example.petshopdesktop.DTOs.ProductDTO;
|
||||
import org.example.petshopdesktop.DTOs.ProductSupplierDTO;
|
||||
import org.example.petshopdesktop.models.Product;
|
||||
import org.example.petshopdesktop.models.ProductSupplier;
|
||||
import org.example.petshopdesktop.util.ActivityLogger;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
@@ -23,7 +24,7 @@ public class ProductSupplierDB {
|
||||
//Execute Query
|
||||
Statement stmt = conn.createStatement();
|
||||
String sql = "SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
||||
"FROM productsupplier ps " +
|
||||
"FROM productSupplier ps " +
|
||||
"LEFT JOIN product p " +
|
||||
"ON p.prodId = ps.prodId " +
|
||||
"LEFT JOIN supplier s " +
|
||||
@@ -62,7 +63,7 @@ public class ProductSupplierDB {
|
||||
String sql =
|
||||
"SELECT ps.supId, ps.prodId, s.supCompany, p.prodName, ps.cost " +
|
||||
"FROM product p " +
|
||||
"LEFT JOIN productsupplier ps " +
|
||||
"LEFT JOIN productSupplier ps " +
|
||||
"ON p.prodId = ps.prodId " +
|
||||
"LEFT JOIN supplier s " +
|
||||
"ON s.supId = ps.supId " +
|
||||
@@ -108,7 +109,7 @@ public class ProductSupplierDB {
|
||||
int numRows = 0;
|
||||
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
String sql = "INSERT INTO productsupplier (prodId, supId, cost) " +
|
||||
String sql = "INSERT INTO productSupplier (prodId, supId, cost) " +
|
||||
"VALUES (?, ?, ?)";
|
||||
|
||||
//These are the values from productSupplier to put into query above
|
||||
@@ -121,6 +122,13 @@ public class ProductSupplierDB {
|
||||
numRows = stmt.executeUpdate();
|
||||
conn.close();
|
||||
|
||||
// Log the operation
|
||||
if (numRows > 0) {
|
||||
ActivityLogger.getInstance().logInsert("productSupplier",
|
||||
String.format("ProdID:%d-SupID:%d", productSupplier.getProdId(), productSupplier.getSupId()),
|
||||
String.format("Product-Supplier relation added for Product ID %d, Supplier ID %d", productSupplier.getProdId(), productSupplier.getSupId()));
|
||||
}
|
||||
|
||||
return numRows;
|
||||
}
|
||||
|
||||
@@ -141,7 +149,7 @@ public class ProductSupplierDB {
|
||||
|
||||
try{
|
||||
//Delete old data first
|
||||
String sql = "DELETE FROM productsupplier WHERE supId = ? AND prodId = ?";
|
||||
String sql = "DELETE FROM productSupplier WHERE supId = ? AND prodId = ?";
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, oldSupId);
|
||||
stmt.setInt(2, oldProdId);
|
||||
@@ -149,7 +157,7 @@ public class ProductSupplierDB {
|
||||
|
||||
//Then change the data by inserting a new relation with given keys (only if delete worked)
|
||||
if(numRows > 0){
|
||||
sql = "INSERT INTO productsupplier (prodId, supId, cost) " +
|
||||
sql = "INSERT INTO productSupplier (prodId, supId, cost) " +
|
||||
"VALUES (?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, productSupplier.getProdId());
|
||||
@@ -159,6 +167,13 @@ public class ProductSupplierDB {
|
||||
}
|
||||
//Commit changes if both delete and insert worked
|
||||
conn.commit();
|
||||
|
||||
// Log the operation
|
||||
if (numRows > 0) {
|
||||
ActivityLogger.getInstance().logUpdate("productSupplier",
|
||||
String.format("ProdID:%d-SupID:%d", productSupplier.getProdId(), productSupplier.getSupId()),
|
||||
String.format("Product-Supplier relation updated from ProdID:%d-SupID:%d to ProdID:%d-SupID:%d", oldProdId, oldSupId, productSupplier.getProdId(), productSupplier.getSupId()));
|
||||
}
|
||||
}
|
||||
catch(SQLException e){
|
||||
//Rollback CRUD failed
|
||||
@@ -184,7 +199,7 @@ public class ProductSupplierDB {
|
||||
int numRows = 0;
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
|
||||
String sql = "DELETE FROM productsupplier WHERE supId = ? AND prodId = ?";
|
||||
String sql = "DELETE FROM productSupplier WHERE supId = ? AND prodId = ?";
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, supId);
|
||||
stmt.setInt(2, prodId);
|
||||
@@ -192,6 +207,13 @@ public class ProductSupplierDB {
|
||||
numRows = stmt.executeUpdate();
|
||||
conn.close();
|
||||
|
||||
// Log the operation
|
||||
if (numRows > 0) {
|
||||
ActivityLogger.getInstance().logDelete("productSupplier",
|
||||
String.format("ProdID:%d-SupID:%d", prodId, supId),
|
||||
String.format("Product-Supplier relation deleted for Product ID %d, Supplier ID %d", prodId, supId));
|
||||
}
|
||||
|
||||
return numRows;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user