삭제를 위한 핸들러 함수도 작성해줍시다.

    api/comment/controllers/comment.ts

    const { sanitizeEntity } = require('strapi-utils');
    
    module.exports = {
      async create(ctx) {
        (...)
      },
      async find(ctx) {
        (...)
      },
      async update(ctx) {
        (...)
      },
      async delete(ctx) {
        const { articleId, id } = ctx.params; // URL 파라미터 추출
        // 댓글 조회
        const comment = await strapi.services.comment.findOne({
          id,
          article: articleId,
        });
        // 데이터가 존재하지 않을 때
        if (!comment) {
          return ctx.throw(404);
        }
    
        // 사용자 확인
        if (ctx.state.user.id !== comment.user.id) {
          return ctx.unauthorized(`You can't remove this entry`);
        }
    
        // 데이터 삭제
        await strapi.services.comment.delete({ id });
    
        ctx.status = 204;
      },
    };

    이것으로 댓글 API도 완성됐습니다!

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.