-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ :: (entry-238) application fullback method์ถ๊ฐ #93
base: develop
Are you sure you want to change the base?
The head ref may contain hidden characters: "feature/entry-238-application-fullback-method\uCD94\uAC00"
Changes from 6 commits
77d9b36
e64b81d
c62765c
7628292
943e2da
03a0cb4
02b94d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package hs.kr.equus.application.domain.application.spi | ||
|
||
import hs.kr.equus.application.domain.user.model.User | ||
import hs.kr.equus.application.domain.user.model.UserCache | ||
import java.util.UUID | ||
|
||
interface ApplicationQueryUserPort { | ||
fun queryUserByUserId(userId: UUID): User | ||
|
||
fun queryUserByUserIdInCache(userId: UUID): UserCache? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package hs.kr.equus.application.domain.user.model | ||
|
||
import java.util.* | ||
|
||
data class UserCache ( | ||
val id: UUID, | ||
val phoneNumber: String, | ||
val name: String, | ||
val isParent: Boolean, | ||
val receiptCode: Long?, | ||
val role: String, | ||
val ttl: Long | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package hs.kr.equus.application.domain.user.domain | ||
|
||
import hs.kr.equus.application.domain.user.domain.repository.UserCacheRepository | ||
import hs.kr.equus.application.domain.user.model.User | ||
import hs.kr.equus.application.domain.user.model.UserCache | ||
import hs.kr.equus.application.domain.user.spi.UserPort | ||
import hs.kr.equus.application.global.feign.client.UserClient | ||
import org.springframework.stereotype.Component | ||
|
@@ -9,6 +11,7 @@ import java.util.UUID | |
@Component | ||
class UserPersistenceAdapter( | ||
private val userClient: UserClient, | ||
private val userCacheRepository: UserCacheRepository | ||
) : UserPort { | ||
override fun queryUserByUserId(userId: UUID): User { | ||
return userClient.getUserInfoByUserId(userId).run { | ||
|
@@ -20,4 +23,19 @@ class UserPersistenceAdapter( | |
) | ||
} | ||
} | ||
|
||
override fun queryUserByUserIdInCache(userId: UUID): UserCache? { | ||
return userCacheRepository.findById(userId) | ||
.map { | ||
UserCache( | ||
id = it.id, | ||
phoneNumber = it.phoneNumber, | ||
name = it.name, | ||
isParent = it.isParent, | ||
receiptCode = it.receiptCode, | ||
role = it.role.name, | ||
ttl = it.ttl | ||
) | ||
}.orElse(null) | ||
} | ||
Comment on lines
+27
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion ์๋ก์ด
์บ์ ๋ฏธ์ค ์ ๋ก๊น ์ ์ถ๊ฐํ๋ ์์: override fun queryUserByUserIdInCache(userId: UUID): UserCache? {
return userCacheRepository.findById(userId)
.map {
UserCache(
// ... (ํ์ฌ ๋งคํ ๋ก์ง)
)
}.orElse(null)
.also { if (it == null) logger.debug("Cache miss for user ID: $userId") }
} |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package hs.kr.equus.application.domain.user.domain.entity | ||
|
||
import hs.kr.equus.application.global.security.jwt.UserRole | ||
import org.springframework.data.annotation.Id | ||
import org.springframework.data.redis.core.RedisHash | ||
import org.springframework.data.redis.core.TimeToLive | ||
import java.util.* | ||
|
||
@RedisHash(value = "status_cache") | ||
class UserCacheRedisEntity ( | ||
@Id | ||
val id: UUID, | ||
val phoneNumber: String, | ||
val name: String, | ||
val isParent: Boolean, | ||
val receiptCode: Long?, | ||
val role: UserRole, | ||
@TimeToLive | ||
val ttl: Long | ||
) | ||
Comment on lines
+11
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion ์ผ๋ถ ์์ฑ์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ์ด๋ ธํ ์ด์ ์ถ๊ฐ๋ฅผ ๊ณ ๋ คํด๋ณด์ธ์. ๋ช๋ช ์์ฑ์ ๋ํด ์ ํจ์ฑ ๊ฒ์ฌ ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํ๋ฉด ๋ฐ์ดํฐ์ ๋ฌด๊ฒฐ์ฑ์ ๋ณด์ฅํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด: import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern
class UserCacheRedisEntity(
@Id
val id: UUID,
@NotBlank
@Pattern(regexp = "^\\d{11}$", message = "์ ํ๋ฒํธ๋ 11์๋ฆฌ ์ซ์์ฌ์ผ ํฉ๋๋ค")
val phoneNumber: String,
@NotBlank
val name: String,
val isParent: Boolean,
val receiptCode: Long?,
val role: UserRole,
@TimeToLive
val ttl: Long
) ์ด๋ ๊ฒ ํ๋ฉด |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package hs.kr.equus.application.domain.user.domain.repository | ||
|
||
import hs.kr.equus.application.domain.user.domain.entity.UserCacheRedisEntity | ||
import org.springframework.data.repository.CrudRepository | ||
import java.util.UUID | ||
|
||
interface UserCacheRepository : CrudRepository<UserCacheRedisEntity, UUID> { | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,15 +1,48 @@ | ||||||
package hs.kr.equus.application.global.feign.client | ||||||
|
||||||
import hs.kr.equus.application.domain.status.spi.StatusPort | ||||||
import hs.kr.equus.application.domain.user.model.UserCache | ||||||
import hs.kr.equus.application.domain.user.spi.UserPort | ||||||
import hs.kr.equus.application.global.feign.client.dto.response.StatusInfoElement | ||||||
import hs.kr.equus.application.global.feign.client.dto.response.UserInfoElement | ||||||
import hs.kr.equus.application.global.security.jwt.UserRole | ||||||
import org.springframework.cloud.openfeign.FeignClient | ||||||
import org.springframework.context.annotation.Lazy | ||||||
import org.springframework.stereotype.Component | ||||||
import org.springframework.web.bind.annotation.GetMapping | ||||||
import org.springframework.web.bind.annotation.PathVariable | ||||||
import java.util.UUID | ||||||
|
||||||
@FeignClient(name = "UserClient", url = "\${url.user}") | ||||||
@FeignClient(name = "UserClient", url = "\${url.user}", fallback = UserFallBack::class) | ||||||
interface UserClient { | ||||||
@GetMapping("/user/{userId}") | ||||||
fun getUserInfoByUserId( | ||||||
@PathVariable("userId") userId: UUID, | ||||||
): UserInfoElement | ||||||
|
||||||
} | ||||||
|
||||||
@Component | ||||||
class UserFallBack( | ||||||
@Lazy private val userPort: UserPort | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ ๏ธ Refactor suggestion @lazy ์ด๋ ธํ ์ด์ ์ฌ์ฉ์ ํ์์ฑ ๊ฒํ
|
||||||
) : UserClient { | ||||||
|
||||||
override fun getUserInfoByUserId(userId: UUID): UserInfoElement { | ||||||
val user = userPort.queryUserByUserIdInCache(userId) | ||||||
return user?.let { | ||||||
UserInfoElement( | ||||||
id = it.id, | ||||||
isParent = it.isParent, | ||||||
phoneNumber = it.phoneNumber, | ||||||
name = it.name, | ||||||
role = UserRole.valueOf(it.role) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UserRole ๋ณํ ์ ์์ธ ์ฒ๋ฆฌ ํ์
๋ค์๊ณผ ๊ฐ์ด ์์ ํ์ฌ ์์ธ ๋ฐ์์ ๋ฐฉ์งํ ์ ์์ต๋๋ค: - role = UserRole.valueOf(it.role)
+ role = UserRole.values().find { role -> role.name == it.role } ?: UserRole.USER ๐ Committable suggestion
Suggested change
|
||||||
) | ||||||
} ?: UserInfoElement( | ||||||
id = userId, | ||||||
phoneNumber = "01000000000", | ||||||
name = "ํ๊ธธ๋", | ||||||
isParent = false, | ||||||
role = UserRole.USER | ||||||
) | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UserCache
๊ฐ์ฒด ์์ฑ ์ ํ๋ ๋งคํ ์ ํ์ฑ ๊ฒํUserCache
๊ฐ์ฒด๋ฅผ ์์ฑํ ๋role = it.role.name
์ผ๋ก ์ค์ ํ๊ณ ์์ต๋๋ค. ์ด๋it.role
์ด Enum์ด๊ฑฐ๋ ๊ฐ์ฒด๋ผ๋ฉด.name
์ผ๋ก ๋ฌธ์์ด์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ๋ง์ง๋ง,UserCache
์role
ํ๋ ํ์ ๊ณผ ์ผ์นํ๋์ง ํ์ธ์ด ํ์ํฉ๋๋ค. ํ์ ๋ถ์ผ์น๋ก ์ธํ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ํ๋์ ํ์ ์ ํ์ธํ๊ณ ์ ์ ํ๊ฒ ๋งคํํด์ฃผ์ธ์.๋ง์ฝ
role
ํ๋์ ํ์ ์ด Enum์ด๋ผ๋ฉด,it.role
๊ทธ๋๋ก ํ ๋นํ๊ฑฐ๋ ํ์ํ ํ์ ์ผ๋ก ๋ณํํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด:๋๋