From 181ec875b2a4bc5ff30f2a638f22a95a5b0cd430 Mon Sep 17 00:00:00 2001 From: min429 Date: Sun, 13 Oct 2024 16:36:10 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=B0=B1=EB=AC=B8=EB=B0=B1=EB=8B=B5=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20API=EB=A5=BC=20=EA=B5=AC=ED=98=84=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/qna100/api/QnaController.java | 11 +++++++++++ .../domain/qna100/api/dto/DeleteQnaRequest.java | 8 ++++++++ .../qna100/infrastructure/QnaRepository.java | 6 +++++- .../domain/qna100/service/QnaService.java | 17 +++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/dnd/accompany/domain/qna100/api/dto/DeleteQnaRequest.java diff --git a/src/main/java/com/dnd/accompany/domain/qna100/api/QnaController.java b/src/main/java/com/dnd/accompany/domain/qna100/api/QnaController.java index 0482e12..9b7bda0 100644 --- a/src/main/java/com/dnd/accompany/domain/qna100/api/QnaController.java +++ b/src/main/java/com/dnd/accompany/domain/qna100/api/QnaController.java @@ -2,6 +2,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -9,6 +10,7 @@ import com.dnd.accompany.domain.auth.dto.jwt.JwtAuthentication; import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest; +import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest; import com.dnd.accompany.domain.qna100.service.QnaService; import io.swagger.v3.oas.annotations.Operation; @@ -32,4 +34,13 @@ public ResponseEntity createAndUpdate( qnaService.saveAndUpdate(user.getId(), request); return ResponseEntity.ok().build(); } + + @Operation(summary = "백문백답 삭제") + @DeleteMapping + public ResponseEntity delete( + @RequestBody @Valid DeleteQnaRequest request, + @AuthenticationPrincipal JwtAuthentication user) { + qnaService.delete(user.getId(), request); + return ResponseEntity.ok().build(); + } } diff --git a/src/main/java/com/dnd/accompany/domain/qna100/api/dto/DeleteQnaRequest.java b/src/main/java/com/dnd/accompany/domain/qna100/api/dto/DeleteQnaRequest.java new file mode 100644 index 0000000..43a93c6 --- /dev/null +++ b/src/main/java/com/dnd/accompany/domain/qna100/api/dto/DeleteQnaRequest.java @@ -0,0 +1,8 @@ +package com.dnd.accompany.domain.qna100.api.dto; + +import java.util.List; + +public record DeleteQnaRequest( + List ids +) { +} diff --git a/src/main/java/com/dnd/accompany/domain/qna100/infrastructure/QnaRepository.java b/src/main/java/com/dnd/accompany/domain/qna100/infrastructure/QnaRepository.java index d137efc..09bcc1c 100644 --- a/src/main/java/com/dnd/accompany/domain/qna100/infrastructure/QnaRepository.java +++ b/src/main/java/com/dnd/accompany/domain/qna100/infrastructure/QnaRepository.java @@ -1,5 +1,8 @@ package com.dnd.accompany.domain.qna100.infrastructure; +import java.util.List; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -8,5 +11,6 @@ @Repository public interface QnaRepository extends JpaRepository, QnaRepositoryCustom { - void deleteAllByUserId(Long userId); + Optional findFirstByUserId(Long userId); + void deleteByIdIn(List ids); } diff --git a/src/main/java/com/dnd/accompany/domain/qna100/service/QnaService.java b/src/main/java/com/dnd/accompany/domain/qna100/service/QnaService.java index 1551a1e..7ef795b 100644 --- a/src/main/java/com/dnd/accompany/domain/qna100/service/QnaService.java +++ b/src/main/java/com/dnd/accompany/domain/qna100/service/QnaService.java @@ -6,12 +6,17 @@ import java.util.Map; import java.util.function.Function; +import org.springframework.data.domain.Example; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest; +import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest; import com.dnd.accompany.domain.qna100.entity.Qna100; +import com.dnd.accompany.domain.qna100.exception.Qna100AccessDeniedException; +import com.dnd.accompany.domain.qna100.exception.Qna100NotFoundException; import com.dnd.accompany.domain.qna100.infrastructure.QnaRepository; +import com.dnd.accompany.global.common.response.ErrorCode; import lombok.RequiredArgsConstructor; @@ -41,4 +46,16 @@ public void saveAndUpdate(Long userId, CreateAndUpdateQnaRequest request) { qnaRepository.saveAll(newQnas); } + + @Transactional + public void delete(Long userId, DeleteQnaRequest request) { + Qna100 qna = qnaRepository.findFirstByUserId(userId) + .orElseThrow(() -> new Qna100NotFoundException(ErrorCode.QNA100_NOT_FOUND)); + + if(!userId.equals(qna.getUserId())){ + throw new Qna100AccessDeniedException(ErrorCode.QNA100_ACCESS_DENIED); + } + + qnaRepository.deleteByIdIn(request.ids()); + } }