Skip to content

Commit 3a79c15

Browse files
CRUD - brand
1 parent c179c3b commit 3a79c15

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

controller/brandCtrl.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const Brand = require("../models/brandModel.js");
2+
const asyncHandler = require("express-async-handler");
3+
const validateMongoDbId = require("../utils/validateMongoDB");
4+
5+
const createBrand = asyncHandler(async (req, res) => {
6+
try {
7+
const newBrand = await Brand.create(req.body);
8+
res.json(newBrand);
9+
} catch (error) {
10+
throw new Error(error);
11+
}
12+
});
13+
const updateBrand = asyncHandler(async (req, res) => {
14+
const { id } = req.params;
15+
validateMongoDbId(id);
16+
try {
17+
const updatedBrand = await Brand.findByIdAndUpdate(id, req.body, {
18+
new: true,
19+
});
20+
res.json(updatedBrand);
21+
} catch (error) {
22+
throw new Error(error);
23+
}
24+
});
25+
const deleteBrand = asyncHandler(async (req, res) => {
26+
const { id } = req.params;
27+
validateMongoDbId(id);
28+
try {
29+
const deletedBrand = await Brand.findByIdAndDelete(id);
30+
res.json(deletedBrand);
31+
} catch (error) {
32+
throw new Error(error);
33+
}
34+
});
35+
const getBrand = asyncHandler(async (req, res) => {
36+
const { id } = req.params;
37+
validateMongoDbId(id);
38+
try {
39+
const getaBrand = await Brand.findById(id);
40+
res.json(getaBrand);
41+
} catch (error) {
42+
throw new Error(error);
43+
}
44+
});
45+
const getallBrand = asyncHandler(async (req, res) => {
46+
try {
47+
const getallBrand = await Brand.find();
48+
res.json(getallBrand);
49+
} catch (error) {
50+
throw new Error(error);
51+
}
52+
});
53+
module.exports = {
54+
createBrand,
55+
updateBrand,
56+
deleteBrand,
57+
getBrand,
58+
getallBrand,
59+
};

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const authRouter = require("./routes/authRoute.js");
1010
const productRouter = require("./routes/productRoute");
1111
const blogRouter = require("./routes/blogRoute");
1212
const categoryRouter = require("./routes/prodcategoryRoute");
13+
const brandRouter = require("./routes/brandRoute");
1314
const { notFound, errorHandler } = require('./middlewares/errorHandler');
1415
const cookieParser = require("cookie-parser");
1516
dbConnect();
@@ -24,6 +25,7 @@ app.use('/api/user', authRouter);
2425
app.use('/api/product', productRouter);
2526
app.use('/api/blog', blogRouter);
2627
app.use('/api/category', categoryRouter);
28+
app.use('/api/brand', brandRouter);
2729

2830
app.use(notFound);
2931
app.use(errorHandler);

models/brandModel.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const mongoose = require("mongoose");
2+
3+
var brandSchema = new mongoose.Schema(
4+
{
5+
title: {
6+
type: String,
7+
required: true,
8+
unique: true,
9+
index: true,
10+
},
11+
},
12+
{
13+
timestamps: true,
14+
}
15+
);
16+
17+
module.exports = mongoose.model("Brand", brandSchema);

routes/brandRoute.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require("express");
2+
const {
3+
createBrand,
4+
updateBrand,
5+
deleteBrand,
6+
getBrand,
7+
getallBrand,
8+
} = require("../controller/brandCtrl");
9+
const { authMiddleware, isAdmin } = require("../middlewares/authMiddleware");
10+
const router = express.Router();
11+
12+
router.post("/", authMiddleware, isAdmin, createBrand);
13+
router.put("/:id", authMiddleware, isAdmin, updateBrand);
14+
router.delete("/:id", authMiddleware, isAdmin, deleteBrand);
15+
router.get("/:id", getBrand);
16+
router.get("/", getallBrand);
17+
18+
module.exports = router;

0 commit comments

Comments
 (0)