๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[E-Commerce App with REST API] (17) ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET) & ๋ชจ๋“  ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET) ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฐฑ์—”๋“œ(Back-End)/Node js

[E-Commerce App with REST API] (17) ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET) & ๋ชจ๋“  ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET)

์ง•์ง•์•ŒํŒŒ์นด 2023. 3. 31. 11:54
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” Developers Corner ์˜ ์œ ํŠœ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example!

 

๐ŸŒท ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET)

 

๐ŸŒท ๋ชจ๋“  ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ (GET)

 

๐ŸŒท ์ฝ”๋“œ

โœ… controllers/brandCtrl.js

const Brand = require("../models/Brand");
const asyncHandler = require("express-async-handler");
const { validateMongodbID } = require("../utils/validateMongodbID");

// ๋ธŒ๋žœ๋“œ ์ƒ์„ฑํ•˜๊ธฐ
const createBrand = asyncHandler(async (req, res) => {
  try {
    const newBrand = await Brand.create(req.body);
    res.json(newBrand);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ธŒ๋žœ๋“œ ์ˆ˜์ •ํ•˜๊ธฐ
const updateBrand = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const updateBrand = await Brand.findByIdAndUpdate(id, req.body, {
      new: true,
    });
    res.json(updateBrand);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ธŒ๋žœ๋“œ ์‚ญ์ œํ•˜๊ธฐ
const deleteBrand = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const deleteBrand = await Brand.findByIdAndDelete(id);
    res.json(deleteBrand);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ
const getaBrand = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const getaBrand = await Brand.findById(id);
    res.json(getaBrand);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ชจ๋“  ๋ธŒ๋žœ๋“œ ์กฐํšŒํ•˜๊ธฐ
const getallBrand = asyncHandler(async (req, res) => {
  try {
    const getallBrand = await Brand.find();
    res.json(getallBrand);
  }
  catch (error) {
    throw new Error(error);
  }
});

module.exports = {
  createBrand,
  updateBrand,
  deleteBrand,
  getaBrand,
  getallBrand,

}

 

โœ… models/Brand.js

const mongoose = require("mongoose");

const BrandSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    unique: true,
    index: true
  },
}, {
  timestamps: true,
  collection: 'brand'
});

const Brand = mongoose.model("Brand", BrandSchema);
module.exports = Brand;

 

โœ… routes/brandRoute.js

const express = require("express");
const { createBrand, updateBrand, deleteBrand, getaBrand, getallBrand } = require("../controllers/brandCtrl");
const { authMiddleware, isAdmin } = require("../middlewares/authMiddleware");
const router = express.Router();

router.post("/", authMiddleware, isAdmin, createBrand);
router.put("/:id", authMiddleware, isAdmin, updateBrand);
router.delete("/:id", authMiddleware, isAdmin, deleteBrand);

router.get("/:id", getaBrand);
router.get("/", getallBrand);

module.exports = router;

 

โœ… index.js

const express = require("express");
const bodyParser = require("body-parser");
const dbConnect = require("./config/dbConnect");
const {notFound, errorHandler} = require("./middlewares/errorHandler");
const app = express();
require("dotenv").config();
const PORT = process.env.PORT || 8000;

const authRouter = require("./routes/authRoute");
const productRouter = require("./routes/productRoute");
const blogRouter = require("./routes/blogRoute");
const prodCategoryRouter = require("./routes/prodCategoryRoute");
const blogCategoryRouter = require("./routes/blogCategoryRoute");
const brandRouter = require("./routes/brandRoute");
const cookieParser = require("cookie-parser");

// mongoDB
dbConnect();
// Bodyparser
// express์„œ๋ฒ„๋กœ POST์š”์ฒญ์„ ํ•  ๋•Œ inputํƒœ๊ทธ์˜ value๋ฅผ ์ „๋‹ฌ
// URL-encoded ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋กœ์˜ ๋ณ€ํ™˜ ํ•„์š”
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use("/api/user", authRouter);
app.use("/api/product", productRouter);
app.use("/api/blog", blogRouter);
app.use("/api/prodCategory", prodCategoryRouter);
app.use("/api/blogCategory", blogCategoryRouter);
app.use("/api/brand", brandRouter);

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

app.use("/", (req, res) => {
  res.send("hihi");
})

app.listen(PORT, () => {
    console.log(`๐Ÿš€ Server started on port http://localhost:${PORT}`);
});

 

728x90
๋ฐ˜์‘ํ˜•
Comments