Skip to content

Commit

Permalink
Delete category endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
BahaaAY committed Nov 22, 2023
1 parent 9539108 commit 0657791
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ app.use(bodyParser.json());

app.use("/pos", posRoutes);

Category.hasMany(Item);
Item.belongsTo(Category, { constraints: true, onDelete: "CASCADE" });
Category.hasMany(Item,{onDelete: 'cascade', hooks: true});
Item.belongsTo(Category, );

database.sync().then(() => {
console.log("Database synced");
Expand Down
14 changes: 14 additions & 0 deletions controllers/pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ exports.postCategory = async (req, res, next) => {
}
};

exports.deleteCategory = async (req, res, next) => {
const catId = req.params.catId;
try {
const category = await Category.findByPk(catId);
if (!category) {
return res.status(404).json({ message: "Category not found" });
}
await category.destroy();
res.status(200).json({ message: "Category deleted" });
} catch (err) {
console.log(err);
}
};

exports.getItems = async (req, res, next) => {
try {
const items = await Item.findAll({
Expand Down
1 change: 1 addition & 0 deletions routes/pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const posController = require("../controllers/pos");
router.get("/categories", posController.getCategories);

router.post("/category", posController.postCategory);
router.delete("/category/:catId", posController.deleteCategory);

router.get("/items", posController.getItems);
router.get("/items/:catId", posController.getItemsByCategory);
Expand Down

0 comments on commit 0657791

Please sign in to comment.