fix delete category

This commit is contained in:
estherdev03
2025-04-21 17:03:09 -06:00
parent a8745ed94c
commit 0a1db869f7
3 changed files with 21 additions and 25 deletions

View File

@@ -7,7 +7,7 @@ exports.getAllCategoriesWithPagination = async (req, res) => {
try { try {
const [data, _] = await db.execute( const [data, _] = await db.execute(
"SELECT * FROM Category C ORDER BY C.CategoryID ASC LIMIT ? OFFSET ?", "SELECT * FROM Category C ORDER BY C.CategoryID ASC LIMIT ? OFFSET ?",
[limit.toString(), offset.toString()], [limit.toString(), offset.toString()]
); );
const [result] = await db.execute("SELECT COUNT(*) AS count FROM Category"); const [result] = await db.execute("SELECT COUNT(*) AS count FROM Category");
@@ -24,7 +24,7 @@ exports.addCategory = async (req, res) => {
try { try {
const [result] = await db.execute( const [result] = await db.execute(
"INSERT INTO Category (Name) VALUES (?)", "INSERT INTO Category (Name) VALUES (?)",
[name], [name]
); );
res.json({ message: "Adding new category successfully!" }); res.json({ message: "Adding new category successfully!" });
} catch (error) { } catch (error) {
@@ -34,15 +34,23 @@ exports.addCategory = async (req, res) => {
exports.removeCategory = async (req, res) => { exports.removeCategory = async (req, res) => {
const { id } = req.params; const { id } = req.params;
try { try {
if (id == "1") throw Error("You're not allowed to delete this category!");
const [updateResult] = await db.execute(
"UPDATE Product SET CategoryID = 1 WHERE CategoryID = ?",
[id]
);
const [result] = await db.execute( const [result] = await db.execute(
`DELETE FROM Category WHERE CategoryID = ?`, `DELETE FROM Category WHERE CategoryID = ?`,
[id], [id]
); );
res.json({ message: "Delete category successfully!" }); res.json({ message: "Delete category successfully!" });
} catch (error) { } catch (error) {
res.json({ error: "Cannot remove category from database!" }); res.json({
error: error.message || "Cannot remove category from database!",
});
} }
}; };

View File

@@ -72,6 +72,7 @@ VALUES
INSERT INTO INSERT INTO
Category (Name) Category (Name)
VALUES VALUES
('Other'),
('Textbooks'), ('Textbooks'),
('Electronics'), ('Electronics'),
('Furniture'), ('Furniture'),
@@ -100,9 +101,8 @@ VALUES
('Winter Clothing'), ('Winter Clothing'),
('Photography Equipment'), ('Photography Equipment'),
('Event Tickets'), ('Event Tickets'),
('Software Licenses'), ('Software Licenses');
('Transportation (Car Pool)'),
('Other');
-- Insert Products -- Insert Products
INSERT INTO INSERT INTO
@@ -454,4 +454,5 @@ VALUES
1, 1,
'This is a great fake product! Totally recommend it.', 'This is a great fake product! Totally recommend it.',
5, 5,
); '2024-10-02 16:00:00'
)

View File

@@ -28,19 +28,6 @@ CREATE TABLE Category (
Name VARCHAR(255) NOT NULL Name VARCHAR(255) NOT NULL
); );
CREATE TABLE Product (
ProductID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Price DECIMAL(10, 2) NOT NULL,
StockQuantity INT,
UserID INT,
Description TEXT,
CategoryID INT NOT NULL,
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES User (UserID),
FOREIGN KEY (CategoryID) REFERENCES Category (CategoryID)
);
-- Product Entity -- Product Entity
CREATE TABLE Product ( CREATE TABLE Product (
ProductID INT AUTO_INCREMENT PRIMARY KEY, ProductID INT AUTO_INCREMENT PRIMARY KEY,
@@ -51,7 +38,7 @@ CREATE TABLE Product (
Description TEXT, Description TEXT,
CategoryID INT, CategoryID INT,
Date DATETIME DEFAULT CURRENT_TIMESTAMP, Date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE SET NULL, FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE CASCADE,
FOREIGN KEY (CategoryID) REFERENCES Category (CategoryID) FOREIGN KEY (CategoryID) REFERENCES Category (CategoryID)
); );
@@ -73,7 +60,7 @@ CREATE TABLE Review (
AND Rating <= 5 AND Rating <= 5
), ),
Date DATETIME DEFAULT CURRENT_TIMESTAMP, Date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE SET NULL, FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE CASCADE,
FOREIGN KEY (ProductID) REFERENCES Product (ProductID) ON DELETE CASCADE FOREIGN KEY (ProductID) REFERENCES Product (ProductID) ON DELETE CASCADE
); );
@@ -85,7 +72,7 @@ CREATE TABLE Transaction (
Date DATETIME DEFAULT CURRENT_TIMESTAMP, Date DATETIME DEFAULT CURRENT_TIMESTAMP,
PaymentStatus VARCHAR(50), PaymentStatus VARCHAR(50),
FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE CASCADE, FOREIGN KEY (UserID) REFERENCES User (UserID) ON DELETE CASCADE,
FOREIGN KEY (ProductID) REFERENCES Product (ProductID) ON DELETE SET NULL FOREIGN KEY (ProductID) REFERENCES Product (ProductID) ON DELETE CASCADE
); );
-- Recommendation Entity (Many-to-One with User, Many-to-One with Product) -- Recommendation Entity (Many-to-One with User, Many-to-One with Product)