[2024.06.19 TIL] 내일배움캠프 45일차 (팀프로젝트, 펫시터 API 구현 및 수정)

My_Code·2024년 6월 19일
0

TIL

목록 보기
58/113
post-thumbnail

본 내용은 내일배움캠프에서 활동한 내용을 기록한 글입니다.


💻 TIL(Today I Learned)

📌 Today I Done

✏️ 자격증 등록 기능 구현(Router)

  • 자격증 등록 기능은 게시물 등록 기능과 거의 유사함

  • 자격증은 하나의 이미지와 자격증에 대한 정보들을 데이터베이스에 저장함

  • 이미지가 하나이기에 자격증 이미지용 데이터베이스가 없어도 됨

  • 그냥 컬럼에 S3에서 받은 URL를 넣어서 사용함

  • 아래는 라우터에서 선언한 코드

// 펫시터 자격증 추가 API
petsitterRouter.post(
  '/certificate',
  petsitterAccessToken,
  uploadImage.single('image'),
  petsitterController.createCertificate
);

✏️ 자격증 등록 기능 구현(Controller)

  • Controller에서는 실제로 사용자의 입력을 받는 구간으로 여기서는 자격증의 이름, 발급처, 발행일만 req.body로 받음

  • 펫시터의 ID는 로그인한 펫시터의 정보가 있는 `req.petsitter'에서 가져옴

  • 이미지 데이터는 multer가 담아준 req.file에서 가져옴
    (single일 때는 req.file, array일 때는 'req.files')

  • 펫시터 Access Token 미들웨어에서 필요한 인증 절차를 거쳤기 때문에 자격증 생성에서는 따로 에러처리가 필요없음

  • 다만, 추후 req.body를 검사하기 위한 Joi 유효성 검사가 필요함

  // 펫시터 자격증 추가 API
  createCertificate = async (req, res, next) => {
    try {
      console.log();
      const { certificateName, certificateIssuer, certificateDate } = req.body;
      const image = req.file;
      const { petsitterId } = req.petsitter;

      const certificate = await this.petsitterService.createCertificate(
        +petsitterId,
        certificateName,
        certificateIssuer,
        certificateDate,
        image,
        req.petsitter
      );

      return res.status(200).json({
        status: 200,
        message: '자격증 등록에 성공했습니다.',
        data: { certificate },
      });
    } catch (err) {
      next(err);
    }
  };

✏️ 자격증 등록 기능 구현(Service)

  • Service에서는 단순하게 Controller에서 받은 데이터를 Repository에 넣어주는 역할을 함

  • 추후 필요한 로직이 생기면 더 추가할 예정

  // 펫시터 자격증 추가 API
  createCertificate = async (
    petsitterId,
    certificateName,
    certificateIssuer,
    certificateDate,
    image,
    petsitter
  ) => {
    const certificate = await this.petsitterRepository.createCertificate(
      petsitterId,
      certificateName,
      certificateIssuer,
      certificateDate,
      image,
      petsitter
    );

    return certificate;
  };
}

✏️ 자격증 등록 기능 구현(Repository)

  • Repository는 데이터베이스에 직접 접근해서 데이터를 관리하는 계층임

  • 여기서는 매개변수로 필요한 데이터를 받아와서 Prisma의 create메서드로 데이터를 저장소에 넣어줌

  • 작성하다보니 petsitter라는 매개변수가 필요없다는 사실을 알게 됨

  • 내일 따로 수정해야겠음

  // 펫시터 자격증 추가 API
  createCertificate = async (
    petsitterId,
    certificateName,
    certificateIssuer,
    certificateDate,
    image,
    petsitter
  ) => {
    const certificate = await this.prisma.certificate.create({
      data: {
        petsitterId,
        imageUrl: image.location,
        certificateName,
        certificateIssuer,
        certificateDate,
      },
    });

    return certificate;
  };


📌 Tomorrow's Goal

✏️ 펫시터 팀프로젝트 마무리 작업

  • 펫시터 자격증 CRUD 마무리하기

  • 리뷰 CRUD 마무리하기

  • Insomnia로 모든 API 테스트해서 에러 찾기

  • 발표자 선정 및 PPT 제작

  • 프로젝트 시연 영상 촬영

  • 리드미 작성하기



📌 Today's Goal I Done

✔️ 팀프로젝트 펫시터 API 구현

  • 오늘은 펫시터 API 관련 에러나 유효성 검사를 구현했음

  • 하지만 가장 기억에 남는 작업은 자격증 등록 API를 구현하는 것임

  • 자격증 등록 API는 일종의 게시물과 같은 로직을 사용하며 자격증에 대한 정보와 사진을 데이터베이스에 등록함

  • 이미지를 업로드하는 multer부분은 지난 프로젝트에서 구현했기에 생각보다 쉽게 구현이 되었음

  • 하지만 AWS S3버킷을 생성할 때 사용자 인증키 발급에서 생각보다 오래 걸렸음

  • 튜터님께서 주신 블로그 자료가 너무 오래되어서 지금의 AWS S3와는 맞지 않았기에 따로 구글링을 통해서 인증키를 발급함


profile
조금씩 정리하자!!!

0개의 댓글