forked from prgrms-be-devcourse/NBB1_2_3_Team10
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Preta3418/origin/feat/profile
Origin/feat/profile
- Loading branch information
Showing
12 changed files
with
261 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/org/tenten/bittakotlin/profile/constant/Job.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.tenten.bittakotlin.profile.constant | ||
|
||
enum class Job { | ||
ACTOR, DIRECTOR | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/org/tenten/bittakotlin/profile/controller/ProfileController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.tenten.bittakotlin.profile.controller | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
import org.tenten.bittakotlin.profile.dto.ProfileDTO | ||
import org.tenten.bittakotlin.profile.service.ProfileServiceImpl | ||
|
||
|
||
|
||
@RestController | ||
@RequestMapping("/api/v1/profile") | ||
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}") | ||
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 | ||
} | ||
|
||
companion object { | ||
private val logger: Logger = LoggerFactory.getLogger(ProfileController::class.java) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/org/tenten/bittakotlin/profile/dto/ProfileDTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.tenten.bittakotlin.profile.dto | ||
|
||
import jakarta.validation.constraints.NotBlank | ||
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 profileUrl: String? = null, | ||
val description: String? = null, | ||
val job: String? = null, | ||
val socialMedia: String? = null | ||
) |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/org/tenten/bittakotlin/profile/entity/Profile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.tenten.bittakotlin.profile.entity | ||
|
||
|
||
import jakarta.persistence.* | ||
import org.tenten.bittakotlin.member.entity.Member | ||
import org.tenten.bittakotlin.profile.constant.Job | ||
|
||
//data class 로 변경 | ||
@Entity | ||
class Profile( | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long? = null, | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id", nullable = false) | ||
val member: Member, | ||
|
||
@Column(length = 20, nullable = false) | ||
var nickname: String, | ||
|
||
@Column(name = "profile_url") | ||
var profileUrl: String? = null, | ||
|
||
@Column(columnDefinition = "TEXT") | ||
var description: String? = null, | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(length = 10) | ||
var job: Job? = null, | ||
|
||
@Column(columnDefinition = "JSON") | ||
var socialMedia: String? = null | ||
) |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/org/tenten/bittakotlin/profile/repository/ProfileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.tenten.bittakotlin.profile.repository | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.tenten.bittakotlin.profile.entity.Profile | ||
|
||
|
||
interface ProfileRepository : JpaRepository<Profile, Long> { | ||
fun findByMemberId(memberId: Long): Profile? | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/tenten/bittakotlin/profile/service/ProfileService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.tenten.bittakotlin.profile.service | ||
|
||
import org.tenten.bittakotlin.member.entity.Member | ||
import org.tenten.bittakotlin.profile.dto.ProfileDTO | ||
|
||
interface ProfileService { | ||
fun createProfile(memberId: Long, nickname: String): ProfileDTO | ||
fun getProfile(memberId: Long): ProfileDTO | ||
fun updateProfile(memberId: Long, profileDTO: ProfileDTO): ProfileDTO | ||
fun createDefaultProfile(member: Member): ProfileDTO | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/kotlin/org/tenten/bittakotlin/profile/service/ProfileServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.tenten.bittakotlin.profile.service | ||
|
||
import jakarta.persistence.EntityNotFoundException | ||
import org.slf4j.LoggerFactory | ||
import org.slf4j.Logger | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import org.tenten.bittakotlin.member.entity.Member | ||
import org.tenten.bittakotlin.member.repository.MemberRepository | ||
import org.tenten.bittakotlin.member.service.MemberService | ||
import org.tenten.bittakotlin.profile.dto.ProfileDTO | ||
import org.tenten.bittakotlin.profile.entity.Profile | ||
import org.tenten.bittakotlin.profile.repository.ProfileRepository | ||
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 profile = Profile( | ||
member = member, | ||
nickname = member.nickname, | ||
profileUrl = null, | ||
description = "This is a default profile.", | ||
job = null, | ||
socialMedia = null | ||
) | ||
val savedProfile = profileRepository.save(profile) | ||
return toDto(savedProfile) | ||
} | ||
|
||
|
||
//자체적으로 profile 을 생성할경우. "테스트 용도" | ||
@Transactional | ||
override fun createProfile(memberId: Long, nickname: String): ProfileDTO { | ||
|
||
val member = memberRepository.findById(memberId) | ||
.orElseThrow { EntityNotFoundException("Member not found for memberId=$memberId") } | ||
|
||
val profile = Profile( | ||
member = member, | ||
nickname = nickname, | ||
profileUrl = null, | ||
description = "This is a custom profile.", | ||
job = null, | ||
socialMedia = null | ||
) | ||
|
||
val savedProfile = profileRepository.save(profile) | ||
logger.info("Profile created successfully for memberId=$memberId") | ||
|
||
return toDto(savedProfile) | ||
} | ||
|
||
|
||
@Transactional(readOnly = true) | ||
override fun getProfile(memberId: Long): ProfileDTO { | ||
logger.info("Fetching profile for memberId=$memberId") | ||
|
||
val profile = profileRepository.findByMemberId(memberId) | ||
?: throw EntityNotFoundException("Profile not found for memberId=$memberId") | ||
|
||
logger.info("Profile fetched successfully for memberId=$memberId") | ||
return toDto(profile) | ||
} | ||
|
||
@Transactional | ||
override fun updateProfile(memberId: Long, profileDTO: ProfileDTO): ProfileDTO { | ||
logger.info("Updating profile for memberId=$memberId") | ||
|
||
val profile = profileRepository.findByMemberId(memberId) | ||
?: throw EntityNotFoundException("Profile not found for memberId=$memberId") | ||
|
||
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"), | ||
nickname = profile.nickname, | ||
profileUrl = profile.profileUrl, | ||
description = profile.description, | ||
job = profile.job?.name, | ||
socialMedia = profile.socialMedia | ||
) | ||
} | ||
|
||
companion object { | ||
private val logger: Logger = LoggerFactory.getLogger(ProfileServiceImpl::class.java) | ||
} | ||
} |