diff --git a/src/main/java/qp/official/qp/apiPayload/code/status/ErrorStatus.java b/src/main/java/qp/official/qp/apiPayload/code/status/ErrorStatus.java index d9d7243d..a5c31a09 100644 --- a/src/main/java/qp/official/qp/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/qp/official/qp/apiPayload/code/status/ErrorStatus.java @@ -32,6 +32,7 @@ public enum ErrorStatus implements BaseErrorCode { QUESTION_ALARM_ALREADY_EXISTS(HttpStatus.CONFLICT, "QUESTION_2004", "해당 질문에 대한 알람 설정이 이미 존재 하는 유저 입니다."), QUESTION_ALARM_NOT_FOUND_BY_USER(HttpStatus.NOT_FOUND, "QUESTION_2005", "해당 유저가 설정한 알람은 존재 하지 않습니다."), QUESTION_NOT_EXIST_BY_USER(HttpStatus.NOT_FOUND, "QUESTION_2006", "해당 유저가 작성한 질문은 존재 하지 않습니다."), + QUESTION_NOT_MATCH_BY_USER(HttpStatus.BAD_REQUEST, "QUESTION_2007", "질문을 작성한 유저 본인만 삭제할 수 있습니다."), // 답변 관련 에러 3000 ANSWER_NOT_FOUND(HttpStatus.NOT_FOUND, "ANSWER_3001", "찾고있는 답변이 없습니다."), diff --git a/src/main/java/qp/official/qp/domain/Question.java b/src/main/java/qp/official/qp/domain/Question.java index 83387175..9d41e414 100644 --- a/src/main/java/qp/official/qp/domain/Question.java +++ b/src/main/java/qp/official/qp/domain/Question.java @@ -7,6 +7,7 @@ import qp.official.qp.domain.enums.ChildStatus; import qp.official.qp.domain.enums.Role; import qp.official.qp.domain.mapping.QuestionHashTag; +import qp.official.qp.domain.mapping.UserQuestionAlarm; import qp.official.qp.web.dto.QuestionRequestDTO; import javax.persistence.*; @@ -52,6 +53,10 @@ public class Question extends BaseEntity { @Builder.Default private List answers = new ArrayList<>(); + @OneToMany(mappedBy = "question", cascade = CascadeType.ALL) + @Builder.Default + private List alarms = new ArrayList<>(); + public Question addHit() { this.hit++; return this; diff --git a/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandService.java b/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandService.java index b6dfce14..25c61ee1 100644 --- a/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandService.java +++ b/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandService.java @@ -9,7 +9,7 @@ public interface QuestionCommandService { Question updateQuestion(Long questionId, QuestionRequestDTO.UpdateDTO request); - void deleteQuestion(Long questionId); + void deleteQuestion(Long questionId, Long userId); UserQuestionAlarm saveQuestionAlarm(Long questionId, Long userId); } diff --git a/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandServiceImpl.java b/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandServiceImpl.java index 2ca49a69..6e4af492 100644 --- a/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandServiceImpl.java +++ b/src/main/java/qp/official/qp/service/QuestionService/QuestionCommandServiceImpl.java @@ -66,8 +66,11 @@ public Question updateQuestion(Long questionId, QuestionRequestDTO.UpdateDTO req } @Override - public void deleteQuestion(Long questionId) { + public void deleteQuestion(Long questionId, Long userId) { Question deleteQuestion = questionRepository.findById(questionId).get(); + if (!deleteQuestion.getUser().getUserId().equals(userId)){ + throw new QuestionHandler(ErrorStatus.QUESTION_NOT_MATCH_BY_USER); + } questionRepository.delete(deleteQuestion); } diff --git a/src/main/java/qp/official/qp/web/controller/QuestionController.java b/src/main/java/qp/official/qp/web/controller/QuestionController.java index 5ac82e34..3b8ad283 100644 --- a/src/main/java/qp/official/qp/web/controller/QuestionController.java +++ b/src/main/java/qp/official/qp/web/controller/QuestionController.java @@ -129,7 +129,7 @@ public ApiResponse deleteQuestion( // accessToken으로 유효한 유저인지 인가 tokenService.isValidToken(userId); - questionCommandService.deleteQuestion(questionId); + questionCommandService.deleteQuestion(questionId,userId); return ApiResponse.onSuccess( SuccessStatus.Question_OK, null diff --git a/src/test/java/qp/official/qp/service/QuestionService/QuestionCommandServiceTest.java b/src/test/java/qp/official/qp/service/QuestionService/QuestionCommandServiceTest.java index 03e3a182..70544c8c 100644 --- a/src/test/java/qp/official/qp/service/QuestionService/QuestionCommandServiceTest.java +++ b/src/test/java/qp/official/qp/service/QuestionService/QuestionCommandServiceTest.java @@ -160,13 +160,17 @@ void deleteQuestion() { // when // 질문 정보 Long questionId = 1L; + Long userId = 2L; String title = "testTitle"; String content = "testContent"; + User testUser = User.builder().userId(userId).build(); + //예상 질문 객체 생성 // given Question expectQuestion = Question.builder() .questionId(questionId) + .user(testUser) .title(title) .content(content) .questionHashTagList(new ArrayList<>()) @@ -175,7 +179,7 @@ void deleteQuestion() { when(questionRepository.findById(questionId)).thenReturn(Optional.of(expectQuestion)); //질문 삭제 - questionCommandService.deleteQuestion(questionId); + questionCommandService.deleteQuestion(questionId, userId); //verify verify(questionRepository).delete(expectQuestion);