From 32c72bd438548e0f3b60d6656203b49165ed6bd8 Mon Sep 17 00:00:00 2001 From: sookyung kang Date: Thu, 7 Mar 2024 02:08:01 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F:=20fix=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81=20#24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/member/MemberController.kt | 19 ++++--- .../dto/request/UpdateNicknameRequest.kt | 5 -- .../dto/request/UpdateNicknameRequestDto.kt | 5 ++ .../dto/response/CheckNickNameResponseDto.kt | 2 +- .../member/dto/response/MyPageResponse.kt | 6 --- .../member/dto/response/MyPageResponseDto.kt | 6 +++ .../plu/api/service/member/MemberService.kt | 54 +++++++------------ .../plu/api/service/member/MemberValidator.kt | 16 ++++++ .../th/plu/common/exception/code/ErrorCode.kt | 6 ++- 9 files changed, 60 insertions(+), 59 deletions(-) delete mode 100644 plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequest.kt create mode 100644 plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequestDto.kt delete mode 100644 plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponse.kt create mode 100644 plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponseDto.kt diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/MemberController.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/MemberController.kt index 22fcad9..807765c 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/MemberController.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/MemberController.kt @@ -3,25 +3,24 @@ package com.th.plu.api.controller.member import com.th.plu.api.config.interceptor.Auth import com.th.plu.api.config.resolver.MemberId import com.th.plu.api.controller.member.dto.request.CheckNicknameRequestDto -import com.th.plu.api.controller.member.dto.request.UpdateNicknameRequest +import com.th.plu.api.controller.member.dto.request.UpdateNicknameRequestDto import com.th.plu.api.controller.member.dto.response.CheckNicknameResponse -import com.th.plu.api.controller.member.dto.response.MyPageResponse +import com.th.plu.api.controller.member.dto.response.MyPageResponseDto import com.th.plu.api.service.member.MemberService +import com.th.plu.common.dto.response.ApiResponse import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag -import com.th.plu.common.dto.response.ApiResponse import org.springframework.web.bind.annotation.* @Tag(name = "Member") @RestController class MemberController( - private val memberService: MemberService, + private val memberService: MemberService, ) { @Operation(summary = "닉네임 중복 체크") @PostMapping("/api/v1/member/nickname/dupl") - fun checkNicknameDupl(@RequestBody request: CheckNicknameRequestDto): ApiResponse { - val isAvailable = memberService.isNicknameAvailable(request.nickname) - val response = CheckNicknameResponse(isAvailable) + fun checkNicknameDuplication(@RequestBody request: CheckNicknameRequestDto): ApiResponse { + val response = memberService.checkNicknameDuplication(request) return ApiResponse.success(response) } @@ -29,8 +28,8 @@ class MemberController( @Operation(summary = "[인증] 닉네임 수정") @PutMapping("/api/v1/member/{memberId}/nickname") fun updateNickname( - @PathVariable memberId: Long, - @RequestBody request: UpdateNicknameRequest + @PathVariable memberId: Long, + @RequestBody request: UpdateNicknameRequestDto ): ApiResponse { memberService.updateNickname(memberId, request.newNickname) return ApiResponse.success() @@ -47,7 +46,7 @@ class MemberController( @Auth @Operation(summary = "[인증] 마이페이지 조회") @GetMapping("/api/v1/mypage") - fun getMyPageInfo(@MemberId memberId: Long): ApiResponse { + fun getMyPageInfo(@MemberId memberId: Long): ApiResponse { val myPageInfo = memberService.getMyPageInfo(memberId) return ApiResponse.success(data = myPageInfo) } diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequest.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequest.kt deleted file mode 100644 index c6c9d46..0000000 --- a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequest.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.th.plu.api.controller.member.dto.request - -data class UpdateNicknameRequest( - val newNickname: String -) \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequestDto.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequestDto.kt new file mode 100644 index 0000000..4b088b1 --- /dev/null +++ b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/request/UpdateNicknameRequestDto.kt @@ -0,0 +1,5 @@ +package com.th.plu.api.controller.member.dto.request + +data class UpdateNicknameRequestDto( + val newNickname: String +) \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/CheckNickNameResponseDto.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/CheckNickNameResponseDto.kt index e250b4b..b5136de 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/CheckNickNameResponseDto.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/CheckNickNameResponseDto.kt @@ -1,5 +1,5 @@ package com.th.plu.api.controller.member.dto.response data class CheckNicknameResponse( - val isAvailable: Boolean + val isAvailable: Boolean ) \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponse.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponse.kt deleted file mode 100644 index 5dd6a3f..0000000 --- a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponse.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.th.plu.api.controller.member.dto.response - -data class MyPageResponse( - val nickname: String, - val notificationStatus: Boolean -) \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponseDto.kt b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponseDto.kt new file mode 100644 index 0000000..40d9cb5 --- /dev/null +++ b/plu-api/src/main/kotlin/com/th/plu/api/controller/member/dto/response/MyPageResponseDto.kt @@ -0,0 +1,6 @@ +package com.th.plu.api.controller.member.dto.response + +data class MyPageResponseDto( + val nickname: String, + val notificationStatus: Boolean +) \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberService.kt b/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberService.kt index 1f69c55..a478508 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberService.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberService.kt @@ -1,9 +1,9 @@ package com.th.plu.api.service.member +import com.th.plu.api.controller.member.dto.request.CheckNicknameRequestDto import com.th.plu.api.controller.member.dto.request.CreateUserRequestDto -import com.th.plu.api.controller.member.dto.response.MyPageResponse -import com.th.plu.common.exception.code.ErrorCode -import com.th.plu.common.exception.model.NotFoundException +import com.th.plu.api.controller.member.dto.response.CheckNicknameResponse +import com.th.plu.api.controller.member.dto.response.MyPageResponseDto import com.th.plu.domain.domain.member.Member import com.th.plu.domain.domain.member.Onboarding import com.th.plu.domain.domain.member.Setting @@ -15,40 +15,23 @@ import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service -class MemberService( - private val memberValidator: MemberValidator, - private val memberRepository: MemberRepository, - private val onboardingRepository: OnboardingRepository, - private val settingRepository: SettingRepository, - private val memberExplorer: MemberExplorer -) { +class MemberService(private val memberValidator: MemberValidator, private val memberRepository: MemberRepository, private val onboardingRepository: OnboardingRepository, private val settingRepository: SettingRepository, private val memberExplorer: MemberExplorer) { @Transactional fun registerUser(request: CreateUserRequestDto): Long { memberValidator.validateNotExistsMember(request.socialId, request.socialType) memberValidator.validateDuplicatedNickname(request.nickname) - val member = memberRepository.save( - Member.newInstance( - socialId = request.socialId, - socialType = request.socialType, - fcmToken = request.fcmToken, - setting = settingRepository.save(Setting.newInstance()) - ) - ) - val onboarding = onboardingRepository.save( - Onboarding.newInstance( - member = member, - nickname = request.nickname - ) - ) + val member = memberRepository.save(Member.newInstance(socialId = request.socialId, socialType = request.socialType, fcmToken = request.fcmToken, setting = settingRepository.save(Setting.newInstance()))) + val onboarding = onboardingRepository.save(Onboarding.newInstance(member = member, nickname = request.nickname)) member.initOnboarding(onboarding) return member.id!! } @Transactional(readOnly = true) - fun isNicknameAvailable(nickname: String): Boolean { - return !memberRepository.existsByNickname(nickname) + fun checkNicknameDuplication(request: CheckNicknameRequestDto): CheckNicknameResponse { + val isAvailable = !memberRepository.existsByNickname(request.nickname) + return CheckNicknameResponse(isAvailable) } @Transactional @@ -56,12 +39,10 @@ class MemberService( val member = memberExplorer.findMemberById(memberId) memberValidator.validateDuplicatedNickname(newNickname) + memberValidator.validateOnboardingExists(member) + + member.onboarding!!.nickname = newNickname - //onboarding 객체가 null이라면 예외 발생 - member.onboarding?.let { - it.nickname = newNickname - onboardingRepository.save(it) - } ?: throw NotFoundException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION, "Onboarding 정보가 없는 유저 $memberId 입니다") } @Transactional @@ -71,12 +52,13 @@ class MemberService( } @Transactional(readOnly = true) - fun getMyPageInfo(memberId: Long): MyPageResponse { + fun getMyPageInfo(memberId: Long): MyPageResponseDto { val member = memberExplorer.findMemberById(memberId) - return MyPageResponse( - nickname = member.onboarding?.nickname ?: "", - notificationStatus = member.setting.notificationStatus - ) + + memberValidator.validateNullorBlankNickname(member.onboarding?.nickname) + + return MyPageResponseDto(nickname = member.onboarding?.nickname + ?: "", notificationStatus = member.setting.notificationStatus) } } \ No newline at end of file diff --git a/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberValidator.kt b/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberValidator.kt index ba47380..2cf63b8 100644 --- a/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberValidator.kt +++ b/plu-api/src/main/kotlin/com/th/plu/api/service/member/MemberValidator.kt @@ -2,6 +2,9 @@ package com.th.plu.api.service.member import com.th.plu.common.exception.code.ErrorCode import com.th.plu.common.exception.model.ConflictException +import com.th.plu.common.exception.model.IllegalArgumentException +import com.th.plu.common.exception.model.NotFoundException +import com.th.plu.domain.domain.member.Member import com.th.plu.domain.domain.member.MemberSocialType import com.th.plu.domain.domain.member.repository.MemberRepository import org.springframework.stereotype.Component @@ -21,4 +24,17 @@ class MemberValidator( throw ConflictException(ErrorCode.CONFLICT_NICKNAME_EXCEPTION, "이미 사용 중인 닉네임입니다.") } } + + fun validateNullorBlankNickname(nickname: String?) { + if (nickname.isNullOrBlank()) { + throw IllegalArgumentException(ErrorCode.Illegal_ARGUMENT_NICKNAME_EXCEPTION, "닉네임은 비어 있을 수 없습니다.") + } + } + + fun validateOnboardingExists(member: Member) { + if (member.onboarding == null) { + throw NotFoundException(ErrorCode.NOT_FOUND_ONBOARDING_EXCEPTION, "Onboarding 정보가 없는 유저 입니다") + } + } + } \ 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 1993628..6280007 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 @@ -26,16 +26,20 @@ enum class ErrorCode(val code: String, val message: String) { NOT_FOUND_ARTICLE_EXCEPTION("N005", "삭제되었거나 존재하지 않는 아티클입니다."), NOT_FOUND_ARTICLE_IN_WEEK_AND_DAY_EXCEPTION("N006", "해당 주차 일차에 해당하는 아티클이 존재하지 않습니다."), NOT_FOUND_ENDPOINT_EXCEPTION("N007", "존재하지 않는 엔드포인트입니다."), + NOT_FOUND_ONBOARDING_EXCEPTION("N007", "존재하지 않는 엔드포인트입니다."), // Conflict Exception CONFLICT_EXCEPTION("C001", "이미 존재합니다."), CONFLICT_MEMBER_EXCEPTION("C002", "이미 해당 계정으로 회원가입하셨습니다.\n로그인 해주세요."), CONFLICT_BOOKMARK_EXCEPTION("C003", "요청과 동일한 북마크 상태 입니다."), - CONFLICT_NICKNAME_EXCEPTION("C004","이미 사용 중인 닉네임 입니다."), + CONFLICT_NICKNAME_EXCEPTION("C004", "이미 사용 중인 닉네임 입니다."), // Internal Server Exception INTERNAL_SERVER_EXCEPTION("I001", "서버 내부에서 에러가 발생하였습니다."), // Bad Gateway Exception BAD_GATEWAY_EXCEPTION("B001", "외부 연동 중 에러가 발생하였습니다."), + + // Illegal Argument Exception + Illegal_ARGUMENT_NICKNAME_EXCEPTION("IA001", "잘못된 닉네임 형식입니다."), }