Skip to content

Commit

Permalink
✨ 유저 프로필 이미지 엔티티 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
waterfogSW committed Nov 24, 2024
1 parent 6f0eaf0 commit 4b49708
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.threedays.domain.user.entity

import com.threedays.domain.user.exception.UserException
import com.threedays.support.common.base.domain.DomainEntity
import com.threedays.support.common.base.domain.UUIDTypeId
import io.github.oshai.kotlinlogging.KotlinLogging
import java.net.URL
import java.util.*

class UserProfileImage(
override val id: Id,
val extension: Extension,
val url: URL,
) : DomainEntity<UserProfileImage, UserProfileImage.Id>() {

data class Id(override val value: UUID) : UUIDTypeId(value)

enum class Extension(val value: String) {
PNG("png"),
}

companion object {

private val logger = KotlinLogging.logger {}

fun create(
extension: Extension,
getProfileImageUrlAction: (Id, Extension) -> URL,
): UserProfileImage {
val imageId = Id(UUID.randomUUID())
val imageUrl: URL = try {
getProfileImageUrlAction(imageId, extension)
} catch (e: Exception) {
logger.error(e) { "Failed to upload profile image" }
throw UserException.ProfileImageUploadFailedException()
}

return UserProfileImage(
id = imageId,
extension = extension,
url = imageUrl,
)
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.threedays.domain.user.exception

import com.threedays.support.common.base.exception.CustomException

sealed class UserException(
codeNumber: Int,
override val message: String = DEFAULT_MESSAGE,
) : CustomException("USER", codeNumber, message) {

data class ProfileImageUploadFailedException(
override val message: String = "프로필 이미지 업로드에 실패했습니다.",
) : UserException(2010, message)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.threedays.domain.user.entity

import com.threedays.domain.user.exception.UserException
import io.kotest.core.spec.DisplayName
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import java.net.URI
import java.net.URL

@DisplayName("[도메인][유저] 프로필 이미지")
class UserProfileImageTest : DescribeSpec({

describe("프로필 이미지 생성") {
UserProfileImage.Extension.entries.forEach { extension ->
it("확장자가 ${extension.value}인 프로필 이미지를 생성한다") {
// arrange
val getProfileImageUrlAction: (UserProfileImage.Id, UserProfileImage.Extension) -> URL =
{ _, _ -> URI("https://example.com").toURL() }

// act
val userProfileImage: UserProfileImage =
UserProfileImage.create(extension, getProfileImageUrlAction)

// assert
userProfileImage.extension shouldBe extension
}
}

context("URL 생성작업에서 예외가 발생하는 경우") {
it("UserException.ProfileImageUploadFailedException 예외를 던진다") {
// arrange
val getProfileImageUrlAction: (UserProfileImage.Id, UserProfileImage.Extension) -> URL =
{ _, _ -> throw Exception() }

// act & assert
runCatching {
UserProfileImage.create(
UserProfileImage.Extension.PNG,
getProfileImageUrlAction
)
}.onFailure { it shouldBe UserException.ProfileImageUploadFailedException() }
}
}
}

})
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import java.time.Year

@DisplayName("[도메인][유저] - 유저")
@DisplayName("[도메인][유저] 유저")
class UserTest : DescribeSpec({

val fixtureMonkey: FixtureMonkey = FixtureMonkey.builder()
Expand Down

0 comments on commit 4b49708

Please sign in to comment.