Skip to content

Commit

Permalink
Merge pull request #1 from Preta3418/origin/feat/profile
Browse files Browse the repository at this point in the history
Origin/feat/profile
  • Loading branch information
Preta3418 authored Nov 1, 2024
2 parents 2332ea5 + f53f079 commit c279ae8
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches: [ "main", "release" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "release" ]

jobs:
build:
if: github.ref == 'refs/heads/release' # release 브랜치에만 빌드 수행
if: github.repository == 'prgrms-be-devcourse/NBB1_2_3_Team10'
runs-on: ubuntu-latest
permissions:
contents: read
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
docker push ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
deploy:
if: github.ref == 'refs/heads/release' # release 브랜치에만 배포 수행
if: github.repository == 'prgrms-be-devcourse/NBB1_2_3_Team10' && github.ref == 'refs/heads/release'
needs: build
runs-on: ubuntu-latest
permissions:
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Use OpenJDK 17 image
FROM openjdk:17

Expand All @@ -6,3 +7,4 @@ COPY build/libs/*.jar app.jar

# Set the entry point to run the application
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=prod", "/app.jar"]

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.tenten.bittakotlin.member.entity
import jakarta.persistence.*
import lombok.*
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.tenten.bittakotlin.profile.entity.Profile

@Data
@Builder
Expand All @@ -11,10 +12,15 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener
@Entity
@EntityListeners(AuditingEntityListener::class)
data class Member(

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,

@OneToOne(mappedBy = "member", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
var profile: Profile? = null,


@Column(nullable = false, unique = true)
var username: String = "",

Expand All @@ -29,4 +35,6 @@ data class Member(

@Column(nullable = false)
var role: String? = null


)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ interface MemberService {

fun updateMember(request: MemberRequestDTO.UpdateMemberRequest)

//Profile 측 정보 조회 메서드
fun findMemberProfileData(memberID: Long): MemberResponseDTO.Information


// fun delete(id: Long)
//
// fun checkAuthority(id: Long, username: String): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tenten.bittakotlin.member.service


import jakarta.persistence.EntityNotFoundException

import lombok.RequiredArgsConstructor
import lombok.extern.slf4j.Slf4j
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
Expand All @@ -9,13 +12,17 @@ import org.tenten.bittakotlin.member.dto.MemberRequestDTO
import org.tenten.bittakotlin.member.dto.MemberResponseDTO
import org.tenten.bittakotlin.member.entity.Member
import org.tenten.bittakotlin.member.repository.MemberRepository
import org.tenten.bittakotlin.profile.service.ProfileService

@Service
@RequiredArgsConstructor
@Slf4j
class MemberServiceImpl (
private val memberRepository: MemberRepository,
private val bCryptPasswordEncoder: BCryptPasswordEncoder,
private val profileService: ProfileService
private val bCryptPasswordEncoder: BCryptPasswordEncoder

): MemberService {

override fun join(joinDTO: MemberRequestDTO.Join) {
Expand All @@ -38,6 +45,13 @@ class MemberServiceImpl (
role = "ROLE_USER"
)

//memberRepository.save(data)

//Member 생성시 Profile 도 같이 생성되게 코드 생성
val savedMember = memberRepository.save(data)

profileService.createDefaultProfile(savedMember)

memberRepository.save(data)
}

Expand Down Expand Up @@ -74,4 +88,5 @@ class MemberServiceImpl (

memberRepository.save(member) // 수정 후 저장
}

}
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
}
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 src/main/kotlin/org/tenten/bittakotlin/profile/dto/ProfileDTO.kt
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 src/main/kotlin/org/tenten/bittakotlin/profile/entity/Profile.kt
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
)
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?
}
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
}
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)
}
}

0 comments on commit c279ae8

Please sign in to comment.