Skip to content

Commit

Permalink
[FO-1011] profileSns/profileImage 집교곱 현상 제거하기 위해 profileSns, profileI…
Browse files Browse the repository at this point in the history
…mage OneToMany 관계 제거 (FONE1light#437)
  • Loading branch information
DoodlesOnMyFood authored May 8, 2024
1 parent cf03373 commit 04433fe
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import org.springframework.web.reactive.function.server.ServerRequest

@Component
class GlobalErrorAttributes : DefaultErrorAttributes() {

override fun getErrorAttributes(
request: ServerRequest?,
options: ErrorAttributeOptions?,
): Map<String, Any?> {
val map: MutableMap<String, Any?> = mutableMapOf()
val throwable: Throwable = getError(request)
return if (throwable is GlobalException) {
val ex: GlobalException = getError(request) as GlobalException
map.apply {
this["result"] = "FAIL"
this["data"] = null
this["message"] = ex.reason
this["errorCode"] = ex.status.reasonPhrase
this["message"] = throwable.reason
this["errorCode"] = throwable.status.reasonPhrase
}
} else {
map.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class GlobalErrorWebExceptionHandler(
super.setMessageReaders(serverCodecConfigurer.readers)
}

override fun getRoutingFunction(
errorAttributes: ErrorAttributes,
): RouterFunction<ServerResponse> {
override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> {
return RouterFunctions.route(RequestPredicates.all()) { request: ServerRequest ->
renderErrorResponse(request)
}
Expand Down
28 changes: 7 additions & 21 deletions server/src/main/kotlin/com/fone/profile/domain/entity/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.fone.common.entity.Type
import com.fone.profile.presentation.dto.RegisterProfileRequest
import com.fone.profile.presentation.dto.common.ProfileSnsUrl
import java.time.LocalDate
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
Expand All @@ -17,8 +16,8 @@ import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.OneToMany
import javax.persistence.Table
import javax.persistence.Transient

@Entity
@Table(name = "profiles")
Expand All @@ -27,50 +26,37 @@ data class Profile(
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,

// page1
@Enumerated(EnumType.STRING) var contactMethod: ContactMethod,
@Column(length = 300) var contact: String,

// page2
@Column(length = 10) var name: String,
@Column var hookingComment: String,
@OneToMany(
mappedBy = "profile",
cascade = [CascadeType.PERSIST, CascadeType.MERGE],
orphanRemoval = true
) var profileImages: MutableList<ProfileImage> = mutableListOf(),
@Transient
var profileImages: MutableList<ProfileImage> = mutableListOf(),
@Column var representativeImageUrl: String,

// page3
@Column var birthday: LocalDate,
@Enumerated(EnumType.STRING) var gender: Gender?,
@Column var height: Int?,
@Column var weight: Int?,
@Column var email: String,
@Column(length = 50) var specialty: String,
@OneToMany(
cascade = [CascadeType.PERSIST, CascadeType.MERGE],
orphanRemoval = true
)
@Transient
@JoinColumn(name = "profile_id")
var snsUrls: Set<ProfileSns>,

// page4
@Column(length = 500) var details: String,

// page5
@Enumerated(EnumType.STRING) var career: Career,
@Column(length = 500) var careerDetail: String,

// etc
@Enumerated(EnumType.STRING) var type: Type,
@Column var userId: Long,
@Column var viewCount: Long = 0,
@Column var wantCount: Long = 0,
@Column var isDeleted: Boolean = false,
) : BaseEntity() {

fun view() {
this.viewCount += 1
}
Expand Down Expand Up @@ -100,7 +86,7 @@ data class Profile(
weight = request.thirdPage.weight
email = request.thirdPage.email
specialty = request.thirdPage.specialty
snsUrls = request.thirdPage.snsUrls.map(ProfileSnsUrl::toEntity).toSet()
snsUrls = request.thirdPage.snsUrls.map(ProfileSnsUrl::toEntity).setProfile(this).toSet()

// page4
details = request.fourthPage.details
Expand Down Expand Up @@ -130,9 +116,9 @@ data class Profile(
isDeleted = true
}

/* 연관관계 메서드 */
// 연관관계 메서드
fun addProfileImage(profileImage: ProfileImage) {
this.profileImages.add(profileImage)
profileImage.addProfile(this)
profileImage.profile = this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ data class ProfileImage(
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null

@Column(name = "profile_id", insertable = false, updatable = false)
var profileId: Long? = null

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "profile_id")
var profile: Profile? = null
set(value) {
field = value
profileId = value?.id
}

override fun toString(): String {
return "ProfileImage(id=$id)"
}

fun addProfile(profile: Profile) {
this.profile = profile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import javax.persistence.Enumerated
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.Table

@Entity
Expand All @@ -21,4 +23,22 @@ class ProfileSns(
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null

@Column(name = "profile_id", insertable = false, updatable = false)
var profileId: Long? = null

@ManyToOne
@JoinColumn(name = "profile_id")
var profile: Profile? = null
set(value) {
profileId = value?.id
field = value
}
}

fun <T : Collection<ProfileSns>> T.setProfile(profile: Profile): T {
forEach {
it.profile = profile
}
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fone.profile.domain.repository

import com.fone.profile.domain.entity.ProfileImage

interface ProfileImageRepository {
suspend fun saveAll(images: List<ProfileImage>): List<ProfileImage>

suspend fun findAll(profileId: Long): List<ProfileImage>

suspend fun deleteByProfileId(profileId: Long): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ interface ProfileRepository {
): Page<Profile>

suspend fun findById(profileId: Long): Profile?
suspend fun findByTypeAndId(type: Type?, profileId: Long?): Profile?

suspend fun findAllByUserId(pageable: Pageable, userId: Long): Page<Profile>
suspend fun findByTypeAndId(
type: Type?,
profileId: Long,
): Profile?

suspend fun findAllByUserId(
pageable: Pageable,
userId: Long,
): Page<Profile>

suspend fun save(profile: Profile): Profile

suspend fun findWantAllByUserId(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fone.profile.domain.repository

import com.fone.profile.domain.entity.ProfileSns

interface ProfileSnsRepository {
suspend fun saveAll(urls: Set<ProfileSns>): Set<ProfileSns>

suspend fun findAll(profileId: Long): Set<ProfileSns>

suspend fun deleteByProfileId(profileId: Long): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.fone.common.exception.NotFoundUserException
import com.fone.common.repository.UserCommonRepository
import com.fone.profile.domain.repository.ProfileCategoryRepository
import com.fone.profile.domain.repository.ProfileDomainRepository
import com.fone.profile.domain.repository.ProfileImageRepository
import com.fone.profile.domain.repository.ProfileRepository
import com.fone.profile.domain.repository.ProfileSnsRepository
import org.springframework.stereotype.Service
import javax.transaction.Transactional

Expand All @@ -16,10 +18,14 @@ class DeleteProfileService(
private val profileDomainRepository: ProfileDomainRepository,
private val profileCategoryRepository: ProfileCategoryRepository,
private val userRepository: UserCommonRepository,
private val profileSnsRepository: ProfileSnsRepository,
private val profileImageRepository: ProfileImageRepository,
) {

@Transactional
suspend fun deleteProfile(email: String, profileId: Long) {
suspend fun deleteProfile(
email: String,
profileId: Long,
) {
val userId = userRepository.findByEmail(email) ?: throw NotFoundUserException()
val profile = profileRepository.findByTypeAndId(null, profileId) ?: throw NotFoundProfileException()

Expand All @@ -28,6 +34,8 @@ class DeleteProfileService(
}
profile.delete()
profileRepository.save(profile)
profileSnsRepository.deleteByProfileId(profileId)
profileImageRepository.deleteByProfileId(profileId)
profileDomainRepository.deleteByProfileId(profileId)
profileCategoryRepository.deleteByProfileId(profileId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.fone.profile.domain.service
import com.fone.common.exception.InvalidProfileUserIdException
import com.fone.common.exception.NotFoundProfileException
import com.fone.common.exception.NotFoundUserException
import com.fone.profile.domain.entity.Profile
import com.fone.profile.domain.entity.ProfileCategory
import com.fone.profile.domain.entity.ProfileDomain
import com.fone.profile.domain.entity.ProfileImage
import com.fone.profile.domain.entity.ProfileSns
import com.fone.profile.domain.repository.ProfileCategoryRepository
import com.fone.profile.domain.repository.ProfileDomainRepository
import com.fone.profile.domain.repository.ProfileImageRepository
import com.fone.profile.domain.repository.ProfileRepository
import com.fone.profile.domain.repository.ProfileSnsRepository
import com.fone.profile.domain.repository.ProfileWantRepository
import com.fone.profile.presentation.dto.RegisterProfileRequest
import com.fone.profile.presentation.dto.RegisterProfileResponse
Expand All @@ -22,9 +27,10 @@ class PutProfileService(
private val profileWantRepository: ProfileWantRepository,
private val profileDomainRepository: ProfileDomainRepository,
private val profileCategoryRepository: ProfileCategoryRepository,
private val profileImageRepository: ProfileImageRepository,
private val profileSnsRepository: ProfileSnsRepository,
private val userRepository: UserRepository,
) {

@Transactional
suspend fun putProfile(
request: RegisterProfileRequest,
Expand All @@ -33,30 +39,34 @@ class PutProfileService(
): RegisterProfileResponse {
val user = userRepository.findByEmail(email) ?: throw NotFoundUserException()
val profile = profileRepository.findByTypeAndId(null, profileId) ?: throw NotFoundProfileException()
val imagesAndSns = profile.profileImages to profile.snsUrls
if (user.id != profile.userId) {
throw InvalidProfileUserIdException()
}
profile.put(request)
profileRepository.save(profile)
putImagesAndUrls(profile, imagesAndSns)

val userProfileWants = profileWantRepository.findByUserId(user.id!!)

profileDomainRepository.deleteByProfileId(profile.id!!)
val profileDomains = request.thirdPage.domains?.map {
ProfileDomain(
profile.id!!,
it
)
}
val profileDomains =
request.thirdPage.domains?.map {
ProfileDomain(
profile.id!!,
it
)
}
profileDomainRepository.saveAll(profileDomains)

profileCategoryRepository.deleteByProfileId(profile.id!!)
val profileCategories = request.sixthPage.categories.map {
ProfileCategory(
profile.id!!,
it
)
}
val profileCategories =
request.sixthPage.categories.map {
ProfileCategory(
profile.id!!,
it
)
}
profileCategoryRepository.saveAll(profileCategories)

val profileUser = userRepository.findById(user.id!!)
Expand All @@ -71,4 +81,22 @@ class PutProfileService(
profileUser?.job ?: Job.ACTOR
)
}

private suspend fun putImagesAndUrls(
profile: Profile,
prevImagesAndSns: Pair<List<ProfileImage>, Set<ProfileSns>>,
) {
val prevImages = prevImagesAndSns.first.map { it.url }.toSet()
val currImages = profile.profileImages.map { it.url }.toSet()
if (prevImages != currImages) {
profileImageRepository.deleteByProfileId(profile.id!!)
profileImageRepository.saveAll(profile.profileImages)
}
val prevSns = prevImagesAndSns.second.map { it.url to it.sns }.toSet()
val currSns = profile.snsUrls.map { it.url to it.sns }.toSet()
if (prevSns != currSns) {
profileSnsRepository.deleteByProfileId(profile.id!!)
profileSnsRepository.saveAll(profile.snsUrls)
}
}
}
Loading

0 comments on commit 04433fe

Please sign in to comment.