Skip to content

Commit

Permalink
MILESTONE: Committing categories routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedMaherElSaeidi committed Jul 26, 2024
1 parent fbfeb9d commit 784e06b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ if (app.get("env") === "development") app.use(morgan("tiny"));
// Importing routes (APIs)
const default_routes = require("./routes/default");
const products_routes = require("./routes/products");
const categories_routes = require("./routes/categories");

app.use("/", default_routes);
app.use("/api/products", products_routes);
app.use("/api/categories", categories_routes);

// Server listening
const PORT = process.env.PORT || 3000;
Expand Down
85 changes: 85 additions & 0 deletions routes/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const { ProductCategory } = require("../models/index");

// Table schema (for validating post, and put request)
const schema = Joi.object({
name: Joi.string().min(3).max(30).required(),
});
const query = {
attributes: {
exclude: ["createdAt", "updatedAt"],
},
};

// CRUD operations
router.get("/", async (req, res) => {
const categories = await ProductCategory.findAll(query);

res.status(201).json({ data: categories });
});

router.get("/:id", async (req, res) => {
const category = await ProductCategory.findOne({
where: { id: req.params.id },
...query,
});

category
? res.status(201).json({ data: category })
: res.status(404).json({ message: "Category with given ID wasn't found" });
});

router.post("/", async (req, res) => {
let category = req.body;

// Validate data
const { error } = schema.validate(category);
if (error) return res.status(400).json({ message: error.details[0].message });

// Saving product
category = await ProductCategory.create(category);
res.status(201).json({ data: category });
});

router.put("/:id", async (req, res) => {
let category = await ProductCategory.findOne({
where: { id: req.params.id },
});

if (category) {
let category = req.body;

// Validate data
const { error } = schema.validate(category);
if (error)
return res.status(400).json({ message: error.details[0].message });

// Updating product
await ProductCategory.update(category, {
where: {
id: req.params.id,
},
});
return res.status(201).json({ message: "Updated successfully." });
}

res.status(404).json({ message: "Category with given ID wasn't found" });
});

router.delete("/:id", async (req, res) => {
const category = await ProductCategory.findOne({
where: { id: req.params.id },
});

if (category) {
// Removing category
await ProductCategory.destroy({ where: { id: req.params.id } });
return res.status(200).json({ message: "Deleted successfully." });
}

res.status(404).json({ message: "Category with given ID wasn't found" });
});

module.exports = router;
3 changes: 2 additions & 1 deletion routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ router.get("/:id", async (req, res) => {
where: { id: req.params.id },
...query,
});

product
? res.status(201).json({ data: product })
: res.status(404).json({ message: "Product with given ID wasn't found" });
Expand All @@ -51,7 +52,7 @@ router.post("/", async (req, res) => {
product.image = product.image ? product.image : "default-product-image.jpg";

// Saving product
product = await Product.create({ data: product });
product = await Product.create(product);
res.status(201).json({ data: product });
});

Expand Down

0 comments on commit 784e06b

Please sign in to comment.