Skip to content

Commit

Permalink
⚡️: fix 코드리뷰 반영 #24
Browse files Browse the repository at this point in the history
  • Loading branch information
sookyungg committed Mar 6, 2024
1 parent 015460d commit 32c72bd
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@ 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<CheckNicknameResponse> {
val isAvailable = memberService.isNicknameAvailable(request.nickname)
val response = CheckNicknameResponse(isAvailable)
fun checkNicknameDuplication(@RequestBody request: CheckNicknameRequestDto): ApiResponse<CheckNicknameResponse> {
val response = memberService.checkNicknameDuplication(request)
return ApiResponse.success(response)
}

@Auth
@Operation(summary = "[인증] 닉네임 수정")
@PutMapping("/api/v1/member/{memberId}/nickname")
fun updateNickname(
@PathVariable memberId: Long,
@RequestBody request: UpdateNicknameRequest
@PathVariable memberId: Long,
@RequestBody request: UpdateNicknameRequestDto
): ApiResponse<Any> {
memberService.updateNickname(memberId, request.newNickname)
return ApiResponse.success()
Expand All @@ -47,7 +46,7 @@ class MemberController(
@Auth
@Operation(summary = "[인증] 마이페이지 조회")
@GetMapping("/api/v1/mypage")
fun getMyPageInfo(@MemberId memberId: Long): ApiResponse<MyPageResponse> {
fun getMyPageInfo(@MemberId memberId: Long): ApiResponse<MyPageResponseDto> {
val myPageInfo = memberService.getMyPageInfo(memberId)
return ApiResponse.success(data = myPageInfo)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.th.plu.api.controller.member.dto.request

data class UpdateNicknameRequestDto(
val newNickname: String
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.th.plu.api.controller.member.dto.response

data class CheckNicknameResponse(
val isAvailable: Boolean
val isAvailable: Boolean
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.th.plu.api.controller.member.dto.response

data class MyPageResponseDto(
val nickname: String,
val notificationStatus: Boolean
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,53 +15,34 @@ 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
fun updateNickname(memberId: Long, newNickname: String) {
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
Expand All @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 정보가 없는 유저 입니다")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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", "잘못된 닉네임 형식입니다."),
}

0 comments on commit 32c72bd

Please sign in to comment.