Skip to content

Commit

Permalink
Merge pull request #25 from Preta3418/main
Browse files Browse the repository at this point in the history
Feat: nickname 생성 구현
  • Loading branch information
Preta3418 authored Nov 4, 2024
2 parents 03e5fb0 + 3697a58 commit 6c32199
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,20 @@ class ProfileController(
private val profileService: ProfileServiceImpl
) {

@PostMapping
fun create(@RequestBody profileDTO: ProfileDTO): ResponseEntity<ProfileDTO> {
logger.info("Received request to create profile for memberId=${profileDTO.memberId}")
val response = ResponseEntity.ok(profileService.createProfile(profileDTO))
logger.info("Profile created successfully for memberId=${profileDTO.memberId}")
@GetMapping("/{profileId}")
fun get(@PathVariable profileId: Long): ResponseEntity<ProfileDTO> {
logger.info("Received request to get profile for profileId=$profileId")
val response = ResponseEntity.ok(profileService.getProfile(profileId))
logger.info("Profile fetched successfully for memberId=$profileId")
return response
}

@GetMapping("/{memberId}")
fun get(@PathVariable memberId: Long): ResponseEntity<ProfileDTO> {
logger.info("Received request to get profile for memberId=$memberId")
val response = ResponseEntity.ok(profileService.getProfile(memberId))
logger.info("Profile fetched successfully for memberId=$memberId")
return response
}

@PutMapping("/{memberId}")
fun update(@PathVariable memberId: Long, @RequestBody profileDTO: ProfileDTO): ResponseEntity<ProfileDTO> {
logger.info("Received request to update profile for memberId=$memberId")
val response = ResponseEntity.ok(profileService.updateProfile(memberId, profileDTO))
logger.info("Profile updated successfully for memberId=$memberId")
return response
@PutMapping("/{profileId}")
fun update(@PathVariable profileId: Long, @RequestBody profileDTO: ProfileDTO): ResponseEntity<ProfileDTO> {
logger.info("Received request to update profile for profileId=$profileId")
val updatedProfile = profileService.updateProfile(profileId, profileDTO)
logger.info("Profile updated successfully for profileId=$profileId")
return ResponseEntity.ok(updatedProfile)
}

companion object {
Expand Down
13 changes: 4 additions & 9 deletions src/main/kotlin/org/tenten/bittakotlin/profile/dto/ProfileDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size

data class ProfileDTO(
@field:NotNull(message = "회원 ID는 누락될 수 없습니다.")
val memberId: Long,

@field:NotBlank(message = "닉네임은 비워둘 수 없습니다.")
@field:Size(max = 20, message = "닉네임은 최대 20자까지 입력할 수 있습니다.")
val nickname: String,

val profileId: Long? = null,
val nickname: String? = null,
val profileUrl: String? = null,
val description: String? = null,
val job: String? = null,
val socialMedia: String? = null
val socialMedia: String? = null,
val job: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import org.tenten.bittakotlin.profile.dto.ProfileDTO
import org.tenten.bittakotlin.profile.entity.Profile

interface ProfileService {
fun createProfile(ProfileDTO: ProfileDTO): ProfileDTO
fun getProfile(memberId: Long): ProfileDTO
fun updateProfile(memberId: Long, profileDTO: ProfileDTO): ProfileDTO
fun getProfile(profileId: Long): ProfileDTO
fun updateProfile(profileId: Long, profileDTO: ProfileDTO): ProfileDTO
fun createDefaultProfile(member: Member): ProfileDTO

fun getByNickname(nickname: String): Profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import org.tenten.bittakotlin.profile.constant.Job
@Service
class ProfileServiceImpl(
private val profileRepository: ProfileRepository,
private val memberRepository: MemberRepository
) : ProfileService {

//Member 생성시 Profile 도 같이 생성
@Transactional
override fun createDefaultProfile(member: Member): ProfileDTO {

val nickname = generateUniqueNickname()
val profile = Profile(
member = member,
nickname = member.nickname,
nickname = nickname,
profileUrl = null,
description = "This is a default profile.",
job = null,
Expand All @@ -36,67 +36,55 @@ class ProfileServiceImpl(
return toDto(savedProfile)
}


//자체적으로 profile 을 생성할경우. "테스트 용도"
@Transactional
override fun createProfile(profileDTO: ProfileDTO): ProfileDTO {
val member = memberRepository.findById(profileDTO.memberId).orElseThrow {
EntityNotFoundException("Member not found for memberId=${profileDTO.memberId}")
}

val profile = Profile(
member = member,
nickname = profileDTO.nickname,
profileUrl = profileDTO.profileUrl,
description = profileDTO.description,
job = profileDTO.job?.let { Job.valueOf(it) },
socialMedia = profileDTO.socialMedia
)

val savedProfile = profileRepository.save(profile)
logger.info("Profile created successfully for memberId=${profileDTO.memberId}")

return toDto(savedProfile)
private fun generateUniqueNickname(): String {
var nickname: String
var counter = 1
do {
nickname = "default#$counter"
counter++
} while (profileRepository.findByNickname(nickname).isPresent) // 중복 검사
return nickname
}


@Transactional(readOnly = true)
override fun getProfile(memberId: Long): ProfileDTO {
logger.info("Fetching profile for memberId=$memberId")
override fun getProfile(profileId: Long): ProfileDTO {
logger.info("Fetching profile for profileId=$profileId")

val profile = profileRepository.findByMemberId(memberId)
?: throw EntityNotFoundException("Profile not found for memberId=$memberId")
val profile = profileRepository.findById(profileId)
.orElseThrow { EntityNotFoundException("Profile not found for profileId=$profileId") }

logger.info("Profile fetched successfully for memberId=$memberId")
logger.info("Profile fetched successfully for profileId=$profileId")
return toDto(profile)
}

@Transactional
override fun updateProfile(memberId: Long, profileDTO: ProfileDTO): ProfileDTO {
logger.info("Updating profile for memberId=$memberId")
override fun updateProfile(profileId: Long, profileDTO: ProfileDTO): ProfileDTO {
val profile = profileRepository.findById(profileId)
.orElseThrow { EntityNotFoundException("Profile not found with id=$profileId") }

profileDTO.nickname?.let { newNickname ->
if (profile.nickname != newNickname && profileRepository.findByNickname(newNickname).isPresent) {
throw IllegalArgumentException("Nickname '$newNickname' is already in use")
}
profile.nickname = newNickname
}

val profile = profileRepository.findByMemberId(memberId)
?: throw EntityNotFoundException("Profile not found for memberId=$memberId")
profile.description = profileDTO.description ?: profile.description
profile.socialMedia = profileDTO.socialMedia ?: profile.socialMedia
profile.profileUrl = profileDTO.profileUrl ?: profile.profileUrl
profile.job = profileDTO.job?.let { Job.valueOf(it) } ?: profile.job

profile.nickname = profileDTO.nickname
profile.profileUrl = profileDTO.profileUrl
profile.description = profileDTO.description
profile.job = profileDTO.job?.let { Job.valueOf(it) }
profile.socialMedia = profileDTO.socialMedia

val updatedProfile = profileRepository.save(profile)
logger.info("Profile updated successfully for memberId=$memberId")

return toDto(updatedProfile)
}

private fun toDto(profile: Profile): ProfileDTO {
return ProfileDTO(
memberId = profile.member.id ?: throw IllegalStateException("Member ID is missing"),
profileId = profile.id,
nickname = profile.nickname,
profileUrl = profile.profileUrl,
description = profile.description,
job = profile.job?.name,
socialMedia = profile.socialMedia
)
}
Expand Down

0 comments on commit 6c32199

Please sign in to comment.