From 1a5b5a9dd9e3189723235a387a699bcceabac673 Mon Sep 17 00:00:00 2001 From: PgmJun Date: Sat, 9 Mar 2024 17:50:39 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8:=20feat=20=EB=82=98=EB=A7=8C=20?= =?UTF-8?q?=EB=B3=B4=EA=B8=B0=20=EB=8B=B5=EB=B3=80=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=B4=20=EB=8B=B5=EB=B3=80=20=EC=86=8C=EC=9C=A0=EA=B6=8C=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?#28?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/answer/AnswerController.kt | 5 +++-- .../plu/api/service/answer/AnswerService.kt | 8 +++++-- .../plu/api/service/answer/AnswerValidator.kt | 21 +++++++++++++++++++ .../th/plu/common/exception/code/ErrorCode.kt | 1 + 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerValidator.kt diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/answer/AnswerController.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/answer/AnswerController.kt index 4274b06..98ac8d4 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/controller/answer/AnswerController.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/controller/answer/AnswerController.kt @@ -1,6 +1,7 @@ package com.th.plu.api.controller.answer import com.th.plu.api.config.interceptor.Auth +import com.th.plu.api.config.resolver.MemberId import com.th.plu.api.controller.answer.dto.response.AnswerInfoResponse import com.th.plu.api.service.answer.AnswerService import com.th.plu.common.dto.response.ApiResponse @@ -20,7 +21,7 @@ class AnswerController( @Auth @Operation(summary = "답변 조회") @GetMapping("/v1/answer/{answerId}") - fun findAnswerById(@PathVariable answerId: Long): ApiResponse { - return ApiResponse.success(answerService.findAnswerInfoById(answerId)) + fun findAnswerById(@PathVariable answerId: Long, @MemberId memberId: Long): ApiResponse { + return ApiResponse.success(answerService.findAnswerInfoById(answerId, memberId)) } } \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerService.kt b/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerService.kt index 2da6ef6..3a6f737 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerService.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerService.kt @@ -10,10 +10,14 @@ import org.springframework.transaction.annotation.Transactional class AnswerService( private val questionExplorer: QuestionExplorer, private val answerExplorer: AnswerExplorer, + private val answerValidator: AnswerValidator ) { @Transactional(readOnly = true) - fun findAnswerInfoById(id: Long): AnswerInfoResponse { - val answer = answerExplorer.findAnswerById(id) + fun findAnswerInfoById(answerId: Long, memberId: Long): AnswerInfoResponse { + val answer = answerExplorer.findAnswerById(answerId) + if (!answer.isPublic) { + answerValidator.validateIsMemberOwnerOfAnswer(answerId, memberId) + } val question = questionExplorer.findQuestionById(answer.getQuestionId()) return AnswerInfoResponse.of(question, answer) diff --git a/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerValidator.kt b/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerValidator.kt new file mode 100644 index 0000000..f9df3b5 --- /dev/null +++ b/plu-api/src/main/kotlin/com/th/plu/api/service/answer/AnswerValidator.kt @@ -0,0 +1,21 @@ +package com.th.plu.api.service.answer + +import com.th.plu.common.exception.code.ErrorCode +import com.th.plu.common.exception.model.ValidationException +import com.th.plu.domain.domain.answer.explorer.AnswerExplorer +import com.th.plu.domain.domain.answer.repository.AnswerRepository +import org.springframework.stereotype.Component + +@Component +class AnswerValidator( + private val answerExplorer: AnswerExplorer, + private val answerRepository: AnswerRepository +) { + fun validateIsMemberOwnerOfAnswer(answerId: Long, memberId: Long) { + val answer = answerExplorer.findAnswerById(answerId) + if (answer.member.id != memberId) { + throw ValidationException(ErrorCode.INVALID_ANSWER_OWNER, + "멤버 (ID: ${memberId})는 답변 (ID: ${answerId})의 답변자가 아니기 때문에 답변 정보에 접근할 수 없습니다.") + } + } +} \ No newline at end of file diff --git a/plu-common/src/main/kotlin/com/th/plu/common/exception/code/ErrorCode.kt b/plu-common/src/main/kotlin/com/th/plu/common/exception/code/ErrorCode.kt index 14df21f..cb1ac81 100644 --- a/plu-common/src/main/kotlin/com/th/plu/common/exception/code/ErrorCode.kt +++ b/plu-common/src/main/kotlin/com/th/plu/common/exception/code/ErrorCode.kt @@ -10,6 +10,7 @@ enum class ErrorCode(val code: String, val message: String) { BIND_EXCEPTION("V005", "요청 값을 바인딩하는 과정에서 오류가 발생하였습니다."), METHOD_ARGUMENT_NOT_VALID_EXCEPTION("V006", "요청 값이 검증되지 않은 값 입니다."), INVALID_FORMAT_EXCEPTION("V007", "요청 값이 유효하지 않은 데이터입니다."), + INVALID_ANSWER_OWNER("V008", "질문의 소유자가 아닙니다."), // Unauthorized Exception UNAUTHORIZED_EXCEPTION("U001", "토큰이 만료되었습니다. 다시 로그인 해주세요."),