Skip to content

Commit

Permalink
refactor : rds 모듈 entity 매핑 방식 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
wwan13 committed Jul 30, 2024
1 parent e79bdc0 commit 0fd63f6
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.grida.persistence.diary

import org.grida.domain.diary.Diary
import org.grida.domain.diary.DiaryScope
import org.grida.domain.user.User
import org.grida.persistence.base.BaseEntity
import org.grida.persistence.user.UserEntity
import java.time.LocalDate
Expand Down Expand Up @@ -39,37 +37,11 @@ class DiaryEntity(
var user: UserEntity
) : BaseEntity() {

fun toDiary(): Diary {
return Diary(
id = id,
timestamp = this.toTimeStamp(),
targetDate = targetDate,
content = content,
scope = scope,
userId = user.id
)
}

fun updateContent(content: String) {
this.content = content
}

fun updateScope(scope: DiaryScope) {
this.scope = scope
}

companion object {
fun from(
diary: Diary,
user: User
): DiaryEntity {
return DiaryEntity(
id = diary.id,
targetDate = diary.targetDate,
content = diary.content,
scope = diary.scope,
user = UserEntity.from(user)
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.grida.persistence.diary

import org.grida.domain.diary.Diary
import org.grida.domain.user.User
import org.grida.persistence.user.toEntity

fun Diary.toEntity(user: User): DiaryEntity {
return DiaryEntity(
id = this.id,
targetDate = this.targetDate,
content = this.content,
scope = this.scope,
user = user.toEntity()
)
}

fun DiaryEntity.toDomain(): Diary {
return Diary(
id = this.id,
timestamp = this.toTimeStamp(),
targetDate = this.targetDate,
content = this.content,
scope = this.scope,
userId = this.user.id
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class DiaryEntityRepository(
diary: Diary,
user: User
): Long {
val diaryEntity = diaryJpaEntityRepository.save(DiaryEntity.from(diary, user))
val diaryEntity = diary.toEntity(user)
diaryJpaEntityRepository.save(diaryEntity)
return diaryEntity.id
}

override fun findById(id: Long): Diary {
val diaryEntity = diaryJpaEntityRepository.findByIdOrException(id)
return diaryEntity.toDiary()
return diaryEntity.toDomain()
}

override fun existsByUserIdAndTargetDate(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.grida.persistence.diaryimage

import org.grida.domain.diary.Diary
import org.grida.domain.diaryimage.DiaryImage
import org.grida.domain.image.Image
import org.grida.persistence.diary.DiaryEntity
import org.grida.persistence.user.UserEntity
import org.grida.domain.user.User
import org.grida.persistence.diary.toEntity
import org.grida.persistence.user.toEntity

fun DiaryImage.toEntity(
userEntity: UserEntity,
diaryEntity: DiaryEntity
user: User,
diary: Diary
): DiaryImageEntity {
return DiaryImageEntity(
id = this.id,
imageUrl = this.image.url,
status = this.image.status,
user = userEntity,
diary = diaryEntity
user = user.toEntity(),
diary = diary.toEntity(user)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.grida.persistence.profileimage

import org.grida.domain.image.Image
import org.grida.domain.image.ImageStatus
import org.grida.domain.profileimage.Appearance
import org.grida.domain.profileimage.Gender
import org.grida.domain.profileimage.ProfileImage
import org.grida.domain.user.User
import org.grida.persistence.base.BaseEntity
import org.grida.persistence.user.UserEntity
import javax.persistence.Column
Expand Down Expand Up @@ -50,44 +46,7 @@ class ProfileImageEntity(
var user: UserEntity
) : BaseEntity() {

fun toProfileImage(): ProfileImage {
return ProfileImage(
id = id,
userId = user.id,
image = Image(
url = imageUrl,
status = status,
timestamp = toTimeStamp()
),
appearance = Appearance(
gender = gender,
age = age,
hairStyle = hairStyle,
glasses = glasses,
bodyShape = bodyShape
)
)
}

fun updateStatue(status: ImageStatus) {
this.status = status
}

companion object {
fun from(
profileImage: ProfileImage,
user: User
): ProfileImageEntity {
return ProfileImageEntity(
imageUrl = profileImage.image.url,
status = profileImage.image.status,
gender = profileImage.appearance.gender,
age = profileImage.appearance.age,
hairStyle = profileImage.appearance.hairStyle,
glasses = profileImage.appearance.glasses,
bodyShape = profileImage.appearance.bodyShape,
user = UserEntity.from(user)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.grida.persistence.profileimage

import org.grida.domain.image.Image
import org.grida.domain.profileimage.Appearance
import org.grida.domain.profileimage.ProfileImage
import org.grida.domain.user.User
import org.grida.persistence.user.toEntity

fun ProfileImage.toEntity(user: User): ProfileImageEntity {
return ProfileImageEntity(
imageUrl = this.image.url,
status = this.image.status,
gender = this.appearance.gender,
age = this.appearance.age,
hairStyle = this.appearance.hairStyle,
glasses = this.appearance.glasses,
bodyShape = this.appearance.bodyShape,
user = user.toEntity()
)
}

fun ProfileImageEntity.toDomain(): ProfileImage {
return ProfileImage(
id = this.id,
userId = this.user.id,
image = Image(
url = this.imageUrl,
status = this.status,
timestamp = this.toTimeStamp()
),
appearance = Appearance(
gender = this.gender,
age = this.age,
hairStyle = this.hairStyle,
glasses = this.glasses,
bodyShape = this.bodyShape
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ class ProfileImageEntityRepository(
profileImage: ProfileImage,
user: User
): Long {
val profileImageEntity =
profileImageJpaEntityRepository.save(ProfileImageEntity.from(profileImage, user))
val profileImageEntity = profileImage.toEntity(user)
profileImageJpaEntityRepository.save(profileImageEntity)
return profileImageEntity.id
}

override fun findById(id: Long): ProfileImage {
val profileImageEntity = profileImageJpaEntityRepository.findByIdOrException(id)
return profileImageEntity.toProfileImage()
return profileImageEntity.toDomain()
}

override fun findByUserIdAndStatus(userId: Long, status: ImageStatus): ProfileImage {
val profileImageEntity =
profileImageJpaEntityRepository.findByUserIdAndStatusOrException(userId, status)
return profileImageEntity.toProfileImage()
return profileImageEntity.toDomain()
}

override fun findAllByUserId(userId: Long): List<ProfileImage> {
val profileImageEntities = profileImageJpaEntityRepository.findAllByUserId(userId)
return profileImageEntities.map { it.toProfileImage() }
return profileImageEntities.map { it.toDomain() }
}

override fun existsByUserIdAndStatus(userId: Long, status: ImageStatus): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,4 @@ class UserEntity(
var role: Role,

var nickname: String
) : BaseEntity() {

fun toUser(): User {
return User(
id = id,
username = username,
password = password,
role = role,
nickname = nickname,
timestamp = toTimeStamp()
)
}

companion object {
fun from(user: User): UserEntity {
return UserEntity(
id = user.id,
username = user.username,
password = user.password,
role = user.role,
nickname = user.nickname
)
}
}
}
) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.grida.persistence.user

import org.grida.domain.user.User

fun User.toEntity(): UserEntity {
return UserEntity(
id = this.id,
username = this.username,
password = this.password,
role = this.role,
nickname = this.nickname
)
}

fun UserEntity.toDomain(): User {
return User(
id = this.id,
username = this.username,
password = this.password,
role = this.role,
nickname = this.nickname,
timestamp = this.toTimeStamp()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ class UserEntityRepository(

@Transactional
override fun save(user: User): Long {
val userEntity = userJpaEntityRepository.save(UserEntity.from(user))
val userEntity = user.toEntity()
userJpaEntityRepository.save(userEntity)
return userEntity.id
}

override fun findById(id: Long): User {
val userEntity = userJpaEntityRepository.findByIdOrException(id)
return userEntity.toUser()
return userEntity.toDomain()
}

override fun findByUsername(username: String): User {
val userEntity = userJpaEntityRepository.findByUsernameOrException(username)
return userEntity.toUser()
return userEntity.toDomain()
}

override fun existsByUsername(username: String): Boolean {
Expand Down

0 comments on commit 0fd63f6

Please sign in to comment.