노드심화 4장 Quiz

·2022년 12월 28일
0

study

목록 보기
40/81
post-thumbnail

추가 API 구현

상세조회, 수정, 삭제 API 구현

추가 API

routes/posts.routes.js

router.get('/:postId', postsController.getPostById);
router.put('/:postId', postsController.updatePost);
router.delete('/:postId', postsController.deletePost);

controllers/posts.controller.js

getPostById = async (req, res, next) => {
    const { postId } = req.params;
    const post = await this.postService.findPostById(postId);

    res.status(200).json({ data: post });
  };
updatePost = async (req, res, next) => {
    const { postId } = req.params;
    const { password, title, content } = req.body;

    const updatePost = await this.postService.updatePost(
      postId,
      password,
      title,
      content
    );

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

  deletePost = async (req, res, next) => {
    const { postId } = req.params;
    const { password } = req.body;

    const deletePost = await this.postService.deletePost(postId, password);

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

services/posts.services.js

findPostById = async (postId) => {
    const findPost = await this.postRepository.findPostById(postId);

    return {
      postId: findPost.postId,
      nickname: findPost.nickname,
      title: findPost.title,
      content: findPost.content,
      createdAt: findPost.createdAt,
      updatedAt: findPost.updatedAt,
    };
  };
updatePost = async (postId, password, title, content) => {
    const findPost = await this.postRepository.findPostById(postId);
    if (!findPost) throw new Error("Post doesn't exist");

    await this.postRepository.updatePost(postId, password, title, content);

    const updatePost = await this.postRepository.findPostById(postId);

    return {
      postId: updatePost.postId,
      nickname: updatePost.nickname,
      title: updatePost.title,
      content: updatePost.content,
      createdAt: updatePost.createdAt,
      updatedAt: updatePost.updatedAt,
    };
  };

  deletePost = async (postId, password) => {
    const findPost = await this.postRepository.findPostById(postId);
    if (!findPost) throw new Error("Post doesn't exist");

    await this.postRepository.deletePost(postId, password);

    return {
      postId: findPost.postId,
      nickname: findPost.nickname,
      title: findPost.title,
      content: findPost.content,
      createdAt: findPost.createdAt,
      updatedAt: findPost.updatedAt,
    };
  };

repositories/posts.repository.js

findPostById = async (postId) => {
    const post = await Posts.findById(postId);

    return post;
  };
updatePost = async (postId, password, title, content) => {
    const updatePostData = await Posts.update(
      {
        title,
        content,
      },
      { where: { postId, password } }
    );

    return updatePostData;
  };

  deletePost = async (postId, password) => {
    const deletePostData = await Posts.destroy({ where: { postId, password } });

    return deletePostData;
  };

ADD

위의 코드들을 보면 난 findById메소드를 사용했는데, 강의자료에서는 findByPk메소드를 사용하더라
그래서 찾아본 결과

findByPk :
프라이머 키(보통 id)로 특정 레코드를 가져오는 메소드

둘이 똑같은 기능을 하는 메소드인 거 같다.
둘의 차이점에 대해 찾아보려고 했는데 자료가 잘 나오지 않는다... 내일 질문해야겠다!

profile
개발자 꿈나무

0개의 댓글