TIL(5/31)

정성현·2022년 5월 31일
0

항해99

목록 보기
33/33

미니 블로그 API 수정

회원가입 API(method = POST)

  • request = URL:/users
    body = nickname : string, email : string, password : string, checkPassword : string
  • response = payload

로그인 API(method = POST)GET요청으로하면 URL에 노출이 되므로 POST로 하는게 보안상 좋다.

  • request = URL:/auth
    body = email : string, password : string
  • response = 조회한 뒤 상황에 맞게 메세지 보냄

댓글 목록 조회 API(method = GET) => 로그인을 안해도 조회 가능

  • request = URL:/articles/:user_Id/comment
  • response = payload comment : string

전체 게시글 목록 조회 API(method = GET) => 로그인을 안해도 조회 가능

  • 제목, 작성자명, 작성 날짜를 조회하기
  • 작성날짜를 기준으로 내림차순 정렬하기
  • request = URL:/
  • response = title : string, nickname : string, date : string

게시글 조회 API (method = GET) => 로그인을 안해도 조회가능

  • 제목, 작성자명, 작성 내용을 조회하기
  • request = URL:/articles/:user_Id
    param : user_Id : string
  • response = title : string, name : string, comment : string

게시글 작성 API (method = POST) => 로그인을 해야 작성가능

  • 제목, 작성 내용을 입력하기
  • request = URL:/articles
    body = title : string , comment : string, date : Number ,user_Id : string
  • response = payload

게시글 수정 API (method = PUT) => 로그인을 해야 작성가능

  • request = URL:/articles/:user_Id/modify,
    param : user_Id, body : comment:string
  • response = body: comment : string,

게시글 삭제 API (method = DELETE) => 로그인을 해야 작성가능

  • request = URL:/articles/:user_Id/delete,
    param : user_Id
  • response = 삭제

댓글 작성 API (method = POST)

로그인을 해야 작성 가능, 댓글내용이 비워져 있으면 입력해주세요 메세지 보내기

  • request = URL:/articles/:user_Id/comment
  • param = : user_Id body : comment : string
  • response = : payload, comment : string , date : date

댓글 수정 API (method = PATCH)

로그인을 해야 수정가능, 로그인 토큰 해당하는 사용자가 작성한 댓글만 수정가능하도록 하기

  • request = URL:/articles/:user_Id/comment/modify
    param = user_Id
    body = comment : string
  • response = comment : string

댓글 삭제 API (method = DELETE)

해당 작성자만 삭제가능

  • request = URL:/articles/:user_Id/comment/delete
    param : user_Id
  • response = 삭제
//authMiddleware 사용자 인증 미들웨어

const express = require("express");
const router = express.Router();
const Comment = require("../schemas/comment");
const authMiddlewares = require("../auth_middleware/auth_middleware");


router.get("/articles/:user_Id/comment",async (req,res)=>{
    const {user_Id} = req.params;
    const comment = await Comment.find({articles_Nickname : user_Id},{comment : 1, date : 1}).sort({date: -1})
    res.send({comment});
});

router.post("/articles/:user_Id/comment", authMiddlewares ,async (req,res) =>{

    const {user_Id} = req.params;
    const {comment} = req.body;
    const today = new Date();
    const {user} = res.locals;
    const comment_id = await Comment.findOne().sort("-comment_id");
    let id = 1;
    if(!comment){
        res.status(400).send({
            errorMessage : "댓글 내용이 비워져 있습니다.",
        });
        return;
    }
    if(comment_id){
        id = comment_id.comment_id + 1;
    }
    await Comment.create({comment,date : today, articles_Nickname : user_Id,
        my_Nickname : user.nickname, comment_id : id
    })
    res.status(201).send({success : "true"});

});

router.patch("/articles/:user_Id/comment/modify", authMiddlewares, async (req,res)=>{
    const {user_Id} = req.params;
    const {comment} = req.body;
    const {user} = res.locals;
    console.log(user.nickname);
    const my_Nickname = await Comment.findOne({my_Nickname : user.nickname});
    console.log(my_Nickname.my_Nickname);
    if(!comment){
        res.status(400).send({errorMessage : "댓글란에 댓글을 입력하세요."});
        return;
    }
    else if(user.nickname != my_Nickname.my_Nickname){
        res.status(400).send({errorMessage : "본인이 쓴 댓글만 수정가능합니다."});
        return;
    }
    await Comment.updateOne({my_Nickname : my_Nickname.my_Nickname}, {$set : {comment : comment}})
    res.send({success : true});

});

router.delete("/articles/:user_Id/comment/delete", authMiddlewares, async (req,res)=>{
    const {user_Id} = req.params;
    const {user} = res.locals;
    const my_Nickname = await Comment.findOne({my_Nickname : user.nickname});
    if(user.nickname !== my_Nickname.my_Nickname){
        res.status(400).send({errorMessage : "본인이 쓴 댓글만 삭제가능합니다."});
        return;
    }
    await Comment.deleteOne({my_Nickname : my_Nickname.my_Nickname});
    res.json({success:true});

});



module.exports = router;
profile
I want to be programmer

0개의 댓글