[TIL] Sequelizer 데이터 베이스

Cherry Jin·2023년 11월 16일
0

sparta_내배캠

목록 보기
23/53
post-thumbnail

생성 Post

// 게시글 생성
router.post('/posts', async (req, res) => {
  const { title, content, password } = req.body;
  const post = await Posts.create({ title, content, password });

  res.status(201).json({ data: post });
});

조회 get

// 게시글 전체 조회
router.get('/posts', async (req, res) => {
  const posts = await Posts.findAll({
    attributes: ["postId", "title", "createdAt", "updatedAt"]
  });

  res.status(200).json({ data: posts });
});
// 게시글 상세 조회
router.get('/posts/:postId', async (req, res) => {
  const { postId } = req.params;
  const post = await Posts.findOne({
    attributes: ["postId", "title", "content", "createdAt", "updatedAt"],
    where: { postId }
  });

  res.status(200).json({ data: post });
});

수정 Put

const { Op } = require("sequelize");
// 게시글 수정
router.put('/posts/:postId', async (req, res) => {
  const { postId } = req.params;
  const { title, content, password } = req.body;

  const post = await Posts.findOne({ where: { postId } });
  if (!post) {
    return res.status(404).json({ message: '게시글이 존재하지 않습니다.' });
  } else if (post.password !== password) {
    return res.status(401).json({ message: '비밀번호가 일치하지 않습니다.' });
  }

  await Posts.update(
    { title, content },
    {
      where: {
        [Op.and]: [{ postId }, [{ password }]],
      }
    }
  );

  res.status(200).json({ data: "게시글이 수정되었습니다." });
});

삭제 Delete

const { Op } = require("sequelize");

// 게시글 삭제
router.delete('/posts/:postId', async (req, res) => {
  const { postId } = req.params;
  const { password } = req.body;

  const post = await Posts.findOne({ where: { postId } });
  if (!post) {
    return res.status(404).json({ message: '게시글이 존재하지 않습니다.' });
  } else if (post.password !== password) {
    return res.status(401).json({ message: '비밀번호가 일치하지 않습니다.' });
  }

  await Posts.destroy({ where: { postId } });

  res.status(200).json({ data: "게시글이 삭제되었습니다." });
});
profile
풀스택이 되버린 주니어 개발자

0개의 댓글