댓글창 만들기
스키마 폴더
라우터에 필요한 값을 클라이언트로부터 받아와서 라우터에 전송
// schemas/comment.schema.js
import mongoose from "mongoose";
const commentSchema = new mongoose.Schema({
postId: {
type: String,
required: true, // 필수요소
},
user: {
type: String,
required: true, // 필수요소
},
password: {
type: String,
required: true, // 필수요소
},
content: {
type : String,
required : true, // 필수요소
},
createdAt: {
type: Date, // createAt 필드는 Date 타입을 가집니다.
required: false,
},
});
export default mongoose.model("Schemacomment", commentSchema);
댓글생성 API
import express from 'express';
import schemaPost from '../schmas/post.schema.js'
import schemaComment from '../schmas/comments.schema.js'
//express.js 라우터 생성
const router = express.Router();
/* 댓글 생성 API */
router.post('/post/:_postId/comments', async(req,res,next) =>{
try{
const {_postId} =req.params
const { password, user, content } = req.body
if(!content){
return res.status(400).json({ Message : '댓글 내용을 입력해주세요'})
}
const newComment = new schemaComment({
postId : _postId,
user,
password,
content,
createdAt : new Date()
})
await newComment.save();
res.status(201).json({message:'댓글을 생성하였습니다.'})
}catch{
console.error(err);
return res.status(400).json({message : '데이터 형식이 올바르지 않습니다.'})
}
});
댓글 목록 조회 API
/* 댓글 목록 조회 */
router.get('/post/:_postId/comments', async(req, res, next) =>{
try{
const cheakcomment = await schemaComment.find().sort({createdAt : -1}).exec();
const mapCheakcomment = cheakcomment.map(comment =>({
return:{
commentId : comment._id,
user: comment.user,
content : comment.content,
createdAt: comment.createdAt
}
}));
return res.status(200).json({ mapCheakcomment });
}catch{
console.error(err);
return res.status(400).send({message : '데이터 형식이 올바르지 않습니다.'})
}
});
댓글 수정 API
/* 댓글 수정 */
router.put('/posts/:_postId/comments/:_commentId', async(req,res,next)=>{
try{
const {_commentId} = req.params;
const {password , content} = req.body
const editcomment = await schemaComment.findById(_commentId).exec();
if(!content){
return res.status(400).json({ Message : '댓글 내용을 입력해주세요'})
}
if(!_commentId){
return res.status(404).json({ Message : '댓글 조회에 실패하였습니다.'})
}
if(editcomment.password !== password){
return res.status(400).json({ Message : '비밀번호가 맞지 않습니다.'})
}
editcomment.content = content;
await editcomment.save();
return res.status(200).json({massege: '댓글이 수정되었습니다.'})
}catch{
console.error(err);
return res.status(400).send({message : '데이터 형식이 올바르지 않습니다.'})
}
});
댓글 삭제 API
/* 댓글 삭제 */
router.delete('/posts/:_postId/comments/:_commentId', async(req,res,next) => {
try{
const {_commentId} = req.params;
const {password} = req.body;
const deletecomment = await schemaComment.findById(_commentId).exec();
if(!_commentId){
return res.status(404).json({message : '댓글 조회에 실패하였습니다.'})
}
if(deletecomment.password !== password){
return res.status(400).json({ Message : '비밀번호가 맞지 않습니다.'})
}
await deletecomment.deleteOne({password: _commentId});
return res.status(200).json({message : '댓글을 삭제하였습니다.'});
}catch{
console.error(err);
return res.status(400).send({message : '데이터 형식이 올바르지 않습니다.'})
}
});
export default router;
'node.js' 카테고리의 다른 글
| prisma 를 사용하여 게시판 만들기 (0) | 2023.09.01 |
|---|---|
| 게시판 만들기 (완성버전) (0) | 2023.08.29 |
| 게시판 만들기 (0) | 2023.08.28 |
| 비즈니스 로직이란? (0) | 2023.08.27 |
| REST API 개발하기 (0) | 2023.08.27 |