Skip to content

Commit

Permalink
feat(user): #52 UserprofileImageUrl 필드를 profileImage로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Wo-ogie committed Nov 10, 2024
1 parent ae8f741 commit 5012201
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 101 deletions.
21 changes: 11 additions & 10 deletions damaba/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ DROP TABLE IF EXISTS promotion_hashtag;

CREATE TABLE `user`
(
id BIGINT NOT NULL AUTO_INCREMENT,
roles VARCHAR(255) NOT NULL,
login_type VARCHAR(255) NOT NULL,
o_auth_login_uid VARCHAR(255) NOT NULL UNIQUE,
nickname VARCHAR(7) NOT NULL UNIQUE,
profile_image_url VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
instagram_id VARCHAR(30),
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
id BIGINT NOT NULL AUTO_INCREMENT,
roles VARCHAR(255) NOT NULL,
login_type VARCHAR(255) NOT NULL,
o_auth_login_uid VARCHAR(255) NOT NULL UNIQUE,
nickname VARCHAR(7) NOT NULL UNIQUE,
profile_image_name VARCHAR(255) NOT NULL,
profile_image_url VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
instagram_id VARCHAR(30),
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX idx__user__o_auth_login_uid ON `user` (o_auth_login_uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class RandomTestUtils {
randomInt(positive = true) % 25 + 1,
)

fun randomUrl(): String = "https://${randomString()}"

fun <T> generateRandomList(maxSize: Int, generator: () -> T): List<T> =
generateSequence { generator() }
.take(randomInt(positive = true, max = maxSize))
Expand Down
6 changes: 4 additions & 2 deletions damaba/src/test/kotlin/com/damaba/damaba/util/TestFixture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import com.damaba.damaba.util.RandomTestUtils.Companion.generateRandomSet
import com.damaba.damaba.util.RandomTestUtils.Companion.randomLocalDate
import com.damaba.damaba.util.RandomTestUtils.Companion.randomLong
import com.damaba.damaba.util.RandomTestUtils.Companion.randomString
import com.damaba.damaba.util.RandomTestUtils.Companion.randomUrl
import com.damaba.user.domain.user.User
import com.damaba.user.domain.user.UserProfileImage
import com.damaba.user.domain.user.constant.Gender
import com.damaba.user.domain.user.constant.LoginType
import com.damaba.user.domain.user.constant.UserRoleType
Expand All @@ -30,7 +32,7 @@ object TestFixture {
oAuthLoginUid: String = randomString(),
loginType: LoginType = LoginType.KAKAO,
nickname: String = randomString(len = 7),
profileImageUrl: String = randomString(),
profileImage: UserProfileImage = UserProfileImage(randomString(), randomUrl()),
gender: Gender = Gender.MALE,
instagramId: String = randomString(len = 30),
): User = User(
Expand All @@ -39,7 +41,7 @@ object TestFixture {
oAuthLoginUid = oAuthLoginUid,
loginType = loginType,
nickname = nickname,
profileImageUrl = profileImageUrl,
profileImage = profileImage,
gender = gender,
instagramId = instagramId,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.damaba.user.adapter.inbound.user.dto

import com.damaba.user.application.port.inbound.user.UpdateMyInfoUseCase
import com.damaba.user.domain.user.UserProfileImage
import com.damaba.user.domain.user.constant.Gender
import io.swagger.v3.oas.annotations.media.Schema

Expand All @@ -14,14 +15,14 @@ data class UpdateMyInfoRequest(
@Schema(description = "인스타 아이디", example = "damaba.unofficial")
val instagramId: String?,

@Schema(description = "프로필 이미지 URL")
val profileImageUrl: String,
@Schema(description = "프로필 이미지")
val profileImage: UserProfileImage,
) {
fun toCommand(requestUserId: Long): UpdateMyInfoUseCase.Command = UpdateMyInfoUseCase.Command(
userId = requestUserId,
nickname = this.nickname,
gender = this.gender,
instagramId = this.instagramId,
profileImageUrl = profileImageUrl,
profileImage = profileImage,
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.damaba.user.adapter.inbound.user.dto

import com.damaba.user.domain.user.User
import com.damaba.user.domain.user.UserProfileImage
import com.damaba.user.domain.user.constant.Gender
import com.damaba.user.domain.user.constant.LoginType
import io.swagger.v3.oas.annotations.media.Schema
Expand All @@ -15,8 +16,8 @@ data class UserResponse(
@Schema(description = "닉네임", example = "홍길동")
val nickname: String,

@Schema(description = "프로필 이미지 url", example = "https://user-profile-image-url")
val profileImageUrl: String,
@Schema(description = "프로필 이미지")
val profileImage: UserProfileImage,

@Schema(description = "성별")
val gender: Gender,
Expand All @@ -29,7 +30,7 @@ data class UserResponse(
id = user.id,
loginType = user.loginType,
nickname = user.nickname,
profileImageUrl = user.profileImageUrl,
profileImage = user.profileImage,
gender = user.gender,
instagramId = user.instagramId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,27 @@ class UserCoreRepository(
}

override fun update(user: User): User {
val originalUser = getUserJpaEntityById(user.id)
val originalUserJpaEntity = getUserJpaEntityById(user.id)

if (originalUser.profileImageUrl != user.profileImageUrl) {
val originalProfileImage = userProfileImageJpaRepository.findByUrl(originalUser.profileImageUrl)
if (originalUserJpaEntity.profileImage.url != user.profileImage.url) {
val originalProfileImage = userProfileImageJpaRepository.findByUrl(originalUserJpaEntity.profileImage.url)
originalProfileImage?.delete()
userProfileImageJpaRepository.save(
UserProfileImageJpaEntity(
userId = user.id,
url = user.profileImageUrl,
name = extractProfileImageFileName(user.profileImageUrl),
name = user.profileImage.name,
url = user.profileImage.url,
),
)
}

originalUser.update(user)
return originalUser.toDomain()
originalUserJpaEntity.update(user)
return originalUserJpaEntity.toDomain()
}

private fun findUserJpaEntityById(id: Long): UserJpaEntity? =
userJpaRepository.findById(id).orElseGet { null }

private fun getUserJpaEntityById(id: Long): UserJpaEntity =
userJpaRepository.findById(id).orElseThrow { UserNotFoundException() }

// https://image.damaba.me/profile-image-1.jpg => profile-image-1
private fun extractProfileImageFileName(profileImageUrl: String) =
profileImageUrl.substringBeforeLast(".")
.substringAfterLast("/")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,10 @@ class UserJpaEntity(
loginType: LoginType,
oAuthLoginUid: String,
nickname: String,
profileImageUrl: String,
profileImage: UserProfileImageJpaEmbeddable,
gender: Gender,
instagramId: String?,
) : BaseJpaTimeEntity() {
companion object {
fun from(user: User): UserJpaEntity = UserJpaEntity(
roles = user.roles,
loginType = user.loginType,
oAuthLoginUid = user.oAuthLoginUid,
nickname = user.nickname,
profileImageUrl = user.profileImageUrl,
gender = user.gender,
instagramId = user.instagramId,
)
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
Expand All @@ -62,7 +50,7 @@ class UserJpaEntity(
private set

@Column(name = "profile_image_url", nullable = false)
var profileImageUrl: String = profileImageUrl
var profileImage: UserProfileImageJpaEmbeddable = profileImage
private set

@Enumerated(EnumType.STRING)
Expand All @@ -80,7 +68,7 @@ class UserJpaEntity(
oAuthLoginUid = this.oAuthLoginUid,
loginType = this.loginType,
nickname = this.nickname,
profileImageUrl = this.profileImageUrl,
profileImage = this.profileImage.toDomain(),
gender = this.gender,
instagramId = this.instagramId,
)
Expand All @@ -89,6 +77,18 @@ class UserJpaEntity(
this.nickname = user.nickname
this.gender = user.gender
this.instagramId = user.instagramId
this.profileImageUrl = user.profileImageUrl
this.profileImage = UserProfileImageJpaEmbeddable.from(user.profileImage)
}

companion object {
fun from(user: User): UserJpaEntity = UserJpaEntity(
roles = user.roles,
loginType = user.loginType,
oAuthLoginUid = user.oAuthLoginUid,
nickname = user.nickname,
profileImage = UserProfileImageJpaEmbeddable.from(user.profileImage),
gender = user.gender,
instagramId = user.instagramId,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.damaba.user.adapter.outbound.user

import com.damaba.user.domain.user.UserProfileImage
import jakarta.persistence.Column
import jakarta.persistence.Embeddable

@Embeddable
data class UserProfileImageJpaEmbeddable(
@Column(name = "profile_image_name", nullable = false)
val name: String,

@Column(name = "profile_image_url", nullable = false)
val url: String,
) {
fun toDomain() = UserProfileImage(name, url)

companion object {
fun from(userProfileImage: UserProfileImage) = UserProfileImageJpaEmbeddable(
name = userProfileImage.name,
url = userProfileImage.url,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.damaba.user.application.port.inbound.user

import com.damaba.user.domain.user.User
import com.damaba.user.domain.user.UserProfileImage
import com.damaba.user.domain.user.UserValidator
import com.damaba.user.domain.user.constant.Gender

Expand All @@ -12,7 +13,7 @@ interface UpdateMyInfoUseCase {
val nickname: String,
val gender: Gender,
val instagramId: String?,
val profileImageUrl: String,
val profileImage: UserProfileImage,
) {
init {
UserValidator.validateUserNickname(nickname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class UserService(
throw NicknameAlreadyExistsException(command.nickname)
}

if (user.profileImageUrl != command.profileImageUrl) {
publishEventPort.publish(DeleteFileEvent(url = user.profileImageUrl))
if (user.profileImage != command.profileImage) {
publishEventPort.publish(DeleteFileEvent(url = user.profileImage.url))
}

user.update(
nickname = command.nickname,
gender = command.gender,
instagramId = command.instagramId,
profileImageUrl = command.profileImageUrl,
profileImage = command.profileImage,
)
return updateUserPort.update(user)
}
Expand Down
15 changes: 9 additions & 6 deletions user/src/main/kotlin/com/damaba/user/domain/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class User(
val loginType: LoginType,
val oAuthLoginUid: String,
nickname: String,
profileImageUrl: String,
profileImage: UserProfileImage,
gender: Gender,
instagramId: String?,
) {
var nickname: String = nickname
private set

var profileImageUrl: String = profileImageUrl
var profileImage: UserProfileImage = profileImage
private set

var gender: Gender = gender
Expand All @@ -34,12 +34,12 @@ class User(
nickname: String,
gender: Gender,
instagramId: String?,
profileImageUrl: String,
profileImage: UserProfileImage,
) {
this.nickname = nickname
this.gender = gender
this.instagramId = instagramId
this.profileImageUrl = profileImageUrl
this.profileImage = profileImage
}

override fun equals(other: Any?): Boolean {
Expand All @@ -52,15 +52,18 @@ class User(

companion object {
val DEFAULT_GENDER = Gender.UNDEFINED
const val DEFAULT_PROFILE_IMAGE_URL = "https://dummyimage.com/244x100.png/cc0000/ffffff"
val DEFAULT_PROFILE_IMAGE = UserProfileImage(
name = "default-user-profile-image.jpg",
url = "https://dummyimage.com/244x100.png/cc0000/ffffff",
)

fun create(loginType: LoginType, oAuthLoginUid: String, nickname: String): User = User(
id = 0,
roles = setOf(UserRoleType.USER),
loginType = loginType,
oAuthLoginUid = oAuthLoginUid,
nickname = nickname,
profileImageUrl = DEFAULT_PROFILE_IMAGE_URL,
profileImage = DEFAULT_PROFILE_IMAGE,
gender = DEFAULT_GENDER,
instagramId = null,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.damaba.user.domain.user

data class UserProfileImage(
val name: String,
val url: String,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.damaba.user

import com.damaba.user.property.AuthProperties
import com.damaba.user.property.DamabaProperties
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cloud.openfeign.EnableFeignClients
Expand All @@ -10,8 +9,5 @@ import org.springframework.test.context.ActiveProfiles
@ActiveProfiles("test")
@SpringBootApplication(scanBasePackages = ["com.damaba"])
@EnableFeignClients
@EnableConfigurationProperties(
DamabaProperties::class,
AuthProperties::class,
)
@EnableConfigurationProperties(AuthProperties::class)
class DamabaUserTestApplication
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.damaba.user.application.port.inbound.user.CheckNicknameExistenceUseCa
import com.damaba.user.application.port.inbound.user.GetMyInfoUseCase
import com.damaba.user.application.port.inbound.user.UpdateMyInfoUseCase
import com.damaba.user.config.ControllerTestConfig
import com.damaba.user.domain.user.UserProfileImage
import com.damaba.user.domain.user.constant.Gender
import com.damaba.user.util.RandomTestUtils.Companion.randomBoolean
import com.damaba.user.util.RandomTestUtils.Companion.randomLong
Expand Down Expand Up @@ -77,7 +78,7 @@ class UserControllerTest @Autowired constructor(
nickname = randomString(len = 7),
gender = Gender.FEMALE,
instagramId = randomString(),
profileImageUrl = randomUrl(),
profileImage = UserProfileImage(randomString(), randomUrl()),
)
val expectedResult = createUser(
id = requestUser.id,
Expand All @@ -98,7 +99,8 @@ class UserControllerTest @Autowired constructor(
.andExpect(jsonPath("$.nickname").value(expectedResult.nickname))
.andExpect(jsonPath("$.gender").value(expectedResult.gender.toString()))
.andExpect(jsonPath("$.instagramId").value(expectedResult.instagramId))
.andExpect(jsonPath("$.profileImageUrl").value(expectedResult.profileImageUrl))
.andExpect(jsonPath("$.profileImage.name").value(expectedResult.profileImage.name))
.andExpect(jsonPath("$.profileImage.url").value(expectedResult.profileImage.url))
verify { updateMyInfoUseCase.updateMyInfo(request.toCommand(requestUser.id)) }
}

Expand Down
Loading

0 comments on commit 5012201

Please sign in to comment.