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

[E-Commerce App with REST API] (11) Blog ์ƒ์„ฑํ•˜๊ธฐ (POST) & Blog ์ˆ˜์ •ํ•˜๊ธฐ (PUT) ๋ณธ๋ฌธ

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

[E-Commerce App with REST API] (11) Blog ์ƒ์„ฑํ•˜๊ธฐ (POST) & Blog ์ˆ˜์ •ํ•˜๊ธฐ (PUT)

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

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

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

 

๐ŸŒท Blog ์ƒ์„ฑํ•˜๊ธฐ

์ƒ์„ฑํ•˜๊ธฐ ์ „์—, ๋กœ๊ทธ์ธ๋ถ€ํ„ฐ ์‹ค์‹œ (role์ด "admin" ์ด์–ด์•ผํ•จ)

๋กœ๊ทธ์ธ ํ›„ ๋ฐœ๊ธ‰๋ฐ›์€ ํ† ํฐ์„ blog create ์— ๋„ฃ๊ธฐ

 

๋”๋ณด๊ธฐ

๐Ÿ‘€ ๋ชฐ๋ผ๋„ ๋˜๊ตฌ ์•Œ์•„๋„ ๋˜๊ณ ~

1) postman์— Environment (์™ผ์ชฝ ๋‹จ) ๋“ค์–ด๊ฐ€๊ธฐ

2) New environment ๋ˆŒ๋Ÿฌ์„œ ์ฃผ์†Œ ์ƒ์„ฑํ•˜๊ธฐ

3) ์œ— ์ƒ๋‹จ์— No Envrironment ์—์„œ New environment ๋กœ ๋ฐ”๊พธ๊ธฐ

4) ์•ž์œผ๋กœ ๊ท€์ฐฎ๊ฒŒ ๊ธด API ์‚ฌ์šฉ์•ˆํ•˜๊ณ  ํ•จ์ถ•๋œ  {{Variable}} ์ƒ์„ฑํ•˜๋ฉด ๋จ

 

๐ŸŒท Blog ์ˆ˜์ •ํ•˜๊ธฐ

update ํ•œ๊ฑฐ์ฃต~

 

๐ŸŒท์ฝ”๋“œ

โœ… models/Blogs

const mongoose = require("mongoose");

const BlogSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
    },
    description: {
        type: String,
        required: true,
    },
    category: {
        type: String,
        required: true,
    },
    numViews: {
        type: Number,
        default: 0,
    },
    isLiked: {
        type: Boolean,
        required: false,
    },
    isDisliked: {
        type: Boolean,
        required: false,
    },
    likes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
    }],
    dislikes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
    }],
    image: {
        type: String,
        default:
            "https://tistory1.daumcdn.net/tistory/5027112/attach/f5df7133efd24c339e40102b168855b0"
    },
    author: {
        type: String,
        default: "Admin",
    }
}, {
    toJSON: {
        virtuals: true,
    },
    toObject: {
        virtuals: true,
    },
    timestamps: true,
    collection: 'blog'
});

const Blog = mongoose.model("Blog", BlogSchema);
module.exports = Blog;

 

โœ… routes/blogRoute.js

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

router.post("/", authMiddleware, isAdmin, createBlog);
router.put("/:id", updateBlog);

module.exports = router;

 

โœ… controllers/blogCtrl.js

const Blog = require("../models/Blogs");
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);
  }
});


module.exports = {
  createBlog,
  updateBlog,

}

 

โœ… index.js

blog ๋ผ์šฐํ„ฐ ์ถ”๊ฐ€~

const blogRouter = require("./routes/blogRoute");
728x90
๋ฐ˜์‘ํ˜•
Comments