Skip to content

Commit

Permalink
✨: feat 회원탈퇴, 닉네임 수정 API 추가 #24
Browse files Browse the repository at this point in the history
  • Loading branch information
sookyungg committed Mar 3, 2024
1 parent 7b4ff8a commit d90f083
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
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.response.CheckNicknameResponse
import com.th.plu.api.controller.member.dto.response.MyPageResponse
import com.th.plu.api.service.member.MemberService
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.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@Tag(name = "Member")
@RestController
class MemberController(
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)
return ApiResponse.success(response)
}

@Auth
@Operation(summary = "[인증] 닉네임 수정")
@PutMapping("/api/v1/member/{memberId}/nickname")
fun updateNickname(
@PathVariable memberId: Long,
@RequestBody request: UpdateNicknameRequest
): ApiResponse<Any> {
memberService.updateNickname(memberId, request.newNickname)
return ApiResponse.success()
}

@Auth
@Operation(summary = "[인증] 회원 탈퇴")
@DeleteMapping("/api/v1/member")
fun deleteMember(@MemberId memberId: Long): ApiResponse<Any> {
memberService.deleteMember(memberId)
return ApiResponse.success()
}

}
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 UpdateNicknameRequest(
val newNickname: String
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.th.plu.api.service.member

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.domain.domain.member.Member
import com.th.plu.domain.domain.member.Onboarding
import com.th.plu.domain.domain.member.Setting
import com.th.plu.domain.domain.member.explorer.MemberExplorer
import com.th.plu.domain.domain.member.repository.MemberRepository
import com.th.plu.domain.domain.member.repository.OnboardingRepository
import com.th.plu.domain.domain.member.repository.SettingRepository
Expand All @@ -13,10 +16,11 @@ 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 memberValidator: MemberValidator,
private val memberRepository: MemberRepository,
private val onboardingRepository: OnboardingRepository,
private val settingRepository: SettingRepository,
private val memberExplorer: MemberExplorer
) {

@Transactional
Expand All @@ -25,25 +29,45 @@ class MemberService(
memberValidator.validateDuplicatedNickname(request.nickname)

val member = memberRepository.save(
Member.newInstance(
socialId = request.socialId,
socialType = request.socialType,
fcmToken = request.fcmToken,
setting = settingRepository.save(Setting.newInstance())
)
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
)
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)
}

@Transactional
fun updateNickname(memberId: Long, newNickname: String) {
val member = memberExplorer.findMemberById(memberId)

memberValidator.validateDuplicatedNickname(newNickname)

//onboarding 객체가 null이라면 예외 발생
member.onboarding?.let {
it.nickname = newNickname
onboardingRepository.save(it)
} ?: throw NotFoundException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION, "Onboarding 정보가 없는 유저 $memberId 입니다")
}

@Transactional
fun deleteMember(memberId: Long) {
val member = memberExplorer.findMemberById(memberId)
memberRepository.delete(member)
}

}

0 comments on commit d90f083

Please sign in to comment.