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

[E-Commerce App with REST API] (13) ์ข‹์•„ํ•˜๊ฑฐ๋‚˜ ์‹ซ์–ดํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ ํ•˜๊ธฐ (PUT) ๋ณธ๋ฌธ

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

[E-Commerce App with REST API] (13) ์ข‹์•„ํ•˜๊ฑฐ๋‚˜ ์‹ซ์–ดํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ ํ•˜๊ธฐ (PUT)

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

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

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

 

๐ŸŒท ์ข‹์•„ํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ ํ•˜๊ธฐ (PUT)

์ข‹์•„ํ•˜๋Š” ๋ธ”๋กœ๊ทธ๊ฐ€ ์žˆ๋‹ค๋ฉด ํฌ์ŠคํŠธํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ PUSH!!! ์ทจ์†Œํ•œ๋‹ค๋ฉด PULL!!!

$push ์ œํ•œ์ž
: Field๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด ๋์— ์ถ”๊ฐ€
: pushํ•˜๋Š” ์ˆœ๊ฐ„ ๋ฐฐ์—ด๋กœ ๋ฐ”๋€œ

$pull ์ œํ•œ์ž

: ์กฐ๊ฑด์„ ์ ์œผ๋ฉด ๊ทธ ๋ถ€๋ถ„๋งŒ ์‚ญ์ œ

: ํฌ์ŠคํŠธ์— ๋Œ“๊ธ€์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋“ฑ๋กํ•˜๊ณ  ๊ทธ ์ค‘ ํ•˜๋‚˜ ์‚ญ์ œํ•˜๊ธฐ

 

๐ŸŒท ์‹ซ์–ดํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ ํ•˜๊ธฐ (PUT)

์‹ซ์–ดํ•˜๋Š” ๋ธ”๋กœ๊ทธ๊ฐ€ ์žˆ๋‹ค๋ฉด ํฌ์ŠคํŠธํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ PUSH!!! ์ทจ์†Œํ•œ๋‹ค๋ฉด PULL!!!

 

๐ŸŒท ์ฝ”๋“œ

โœ… controllers/blogCtrl.js

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

// ๋ธ”๋กœ๊ทธ ์ƒ์„ฑํ•˜๊ธฐ
const createBlog = asyncHandler(async (req, res) => {
  try {
    const newBlog = await Blog.create(req.body);
    res.json(newBlog);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ธ”๋กœ๊ทธ ์ˆ˜์ •ํ•˜๊ธฐ
const updateBlog = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const updateBlog = await Blog.findByIdAndUpdate(id, req.body);
    res.json(updateBlog);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ๋ธ”๋กœ๊ทธ ์กฐํšŒํ•˜๊ธฐ
const getBlog = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const getBlog = await Blog.findById(id)
    .populate("likes")
    .populate("dislikes");
    const updateViews = await Blog.findByIdAndUpdate(
      id,
      {
        $inc: { numViews: 1 },    // ์กฐํšŒํ•  ๋•Œ๋งˆ๋‹ค numViews ์˜ฌ๋ผ๊ฐ~
      },
      { new: true }
    );
    res.json(getBlog);
  }
  catch (error) {
    throw new Error(error);
  }
});

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

// ๋ธ”๋กœ๊ทธ ์‚ญ์ œํ•˜๊ธฐ
const deleteBlog = asyncHandler(async (req, res) => {
  const { id } = req.params;
  validateMongodbID(id);
  try {
    const deleteBlog = await Blog.findByIdAndDelete(id);
    res.json(deleteBlog);
  }
  catch (error) {
    throw new Error(error);
  }
});

// ์ข‹์•„ํ•˜๋Š” ๋ธ”๋กœ๊ทธ ์ €์žฅ
const likeBlog = asyncHandler(async (req, res) => {
  const { blogId } = req.body;
  validateMongodbID(blogId);

  // find the blog which you want to be liked
  const blog = await Blog.findById(blogId);
  // find the login user
  const loginUserId = req?.user?._id;
  // find if the user has liked the post
  const isLiked = blog?.isLiked;

  // find if the user has disliked the blog
  const alreadyDisLiked = blog?.dislikes?.find(
    (userId) => userId?.toString() === loginUserId?.toString());

  if (alreadyDisLiked) {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $pull: { dislikes: loginUserId },
      isDisliked: false,
    },
      { new: true });
    res.json(blog);
  }

  // ์ข‹์•„ํ•˜๋ฉด (true) -> ์•„๋‹Œ๊ฑธ๋กœ (false)
  if (isLiked) {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $pull: { likes: loginUserId },
      isLiked: false,
    },
      { new: true });
    res.json(blog);
  }
  // ์•„๋‹ˆ์—ˆ๋Š”๋ฐ (false) -> ์ข‹์•„ํ•˜๋Š”๊ฑธ๋กœ (true)
  else {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $push: { likes: loginUserId },
      isLiked: true,
    },
      { new: true });
    res.json(blog);
  }
});

// ์‹ซ์–ดํ•˜๋Š” ๋ธ”๋กœ๊ทธ ์ €์žฅ
const dislikeBlog = asyncHandler(async (req, res) => {
  const { blogId } = req.body;
  validateMongodbID(blogId);

  // find the blog which you want to be liked
  const blog = await Blog.findById(blogId);
  // find the login user
  const loginUserId = req?.user?._id;
  // find if the user has disliked the post
  const isDisLiked = blog?.isDisliked;

  // find if the user has disliked the blog
  const alreadyLiked = blog?.likes?.find(
    (userId) => userId?.toString() === loginUserId?.toString());

  if (alreadyLiked) {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $pull: { likes: loginUserId },
      isLiked: false,
    },
      { new: true });
    res.json(blog);
  }

  // ์‹ซ์–ดํ•˜๋ฉด (true) -> ์•„๋‹ˆ์—ˆ๋˜๊ฑธ๋กœ (false)
  if (isDisLiked) {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $pull: { dislikes: loginUserId },
      isDisliked: false,
    },
      { new: true });
    res.json(blog);
  }
  // ์•„๋‹ˆ์—ˆ๋Š”๋ฉด (false) -> ์‹ซ์–ดํ•˜๋Š”๊ฑธ๋กœ (true)
  else {
    const blog = await Blog.findByIdAndUpdate(blogId, {
      $push: { dislikes: loginUserId },
      isDisliked: true,
    },
      { new: true });
    res.json(blog);
  }
});

module.exports = {
  createBlog,
  updateBlog,
  getBlog,
  getAllBlogs,
  deleteBlog,
  likeBlog,
  dislikeBlog,

}

 

โœ… routes/blogRoute.js

const express = require("express");
const { createBlog, updateBlog, getBlog, getAllBlogs, deleteBlog, likeBlog, dislikeBlog } = require("../controllers/blogCtrl");
const { authMiddleware, isAdmin } = require("../middlewares/authMiddleware");
const router = express.Router();

router.post("/", authMiddleware, isAdmin, createBlog);

router.put("/likes", authMiddleware, likeBlog);
router.put("/dislikes", authMiddleware, dislikeBlog);

router.put("/:id", updateBlog);
router.get("/:id", getBlog);
router.get("/", getAllBlogs);
router.delete("/:id", authMiddleware, isAdmin, deleteBlog);

module.exports = router;
728x90
๋ฐ˜์‘ํ˜•
Comments