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

[E-Commerce App with REST API] (19) ์ฟ ํฐ ์กฐํšŒํ•˜๊ธฐ (GET) & ๋ชจ๋“  ์ฟ ํฐ ์กฐํšŒํ•˜๊ธฐ (GET) & ์ฟ ํฐ ์ƒ์„ฑํ•˜๊ธฐ (POST) & ์ฟ ํฐ ์ˆ˜์ •ํ•˜๊ธฐ (PUT) & ์ฟ ํฐ ์‚ญ์ œํ•˜๊ธฐ (DELETE) ๋ณธ๋ฌธ

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

[E-Commerce App with REST API] (19) ์ฟ ํฐ ์กฐํšŒํ•˜๊ธฐ (GET) & ๋ชจ๋“  ์ฟ ํฐ ์กฐํšŒํ•˜๊ธฐ (GET) & ์ฟ ํฐ ์ƒ์„ฑํ•˜๊ธฐ (POST) & ์ฟ ํฐ ์ˆ˜์ •ํ•˜๊ธฐ (PUT) & ์ฟ ํฐ ์‚ญ์ œํ•˜๊ธฐ (DELETE)

์ง•์ง•์•ŒํŒŒ์นด 2023. 4. 2. 01:23
728x90
๋ฐ˜์‘ํ˜•

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

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

 

๐ŸŒท ์ฟ ํฐ ์กฐํšŒํ•˜๊ธฐ (GET)

 

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

 

๐ŸŒท ์ฟ ํฐ ์ƒ์„ฑํ•˜๊ธฐ (POST) 

 

๐ŸŒท ์ฟ ํฐ ์ˆ˜์ •ํ•˜๊ธฐ (PUT)

 

๐ŸŒท ์ฟ ํฐ ์‚ญ์ œํ•˜๊ธฐ (DELETE)

 

๐ŸŒท ์ฝ”๋“œ

โœ… controllers/couponCtrl.js

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

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

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

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

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

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

module.exports = {
  createCoupon,
  updateCoupon,
  deleteCoupon,
  getaCoupon,
  getallCoupon,

}

 

โœ… routes/couponRoute.js

const express = require("express");
const { createCoupon, updateCoupon, deleteCoupon, getaCoupon, getallCoupon } = require("../controllers/couponCtrl");
const { authMiddleware, isAdmin } = require("../middlewares/authMiddleware");
const router = express.Router();

router.post("/", authMiddleware, isAdmin, createCoupon);
router.put("/:id", authMiddleware, isAdmin, updateCoupon);
router.delete("/:id", authMiddleware, isAdmin, deleteCoupon);

router.get("/:id", getaCoupon);
router.get("/", getallCoupon);

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 couponRouter = require("./routes/couponRoute");

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("/api/coupon", couponRouter);

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