Skip to content

Commit a0848f9

Browse files
committed
Merge remote-tracking branch 'origin'
2 parents 20d723e + 1004815 commit a0848f9

File tree

6 files changed

+37
-41
lines changed

6 files changed

+37
-41
lines changed

api/src/main/kotlin/handler/EvServiceHandler.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ class EvServiceHandler(
1515
suspend fun handleGet(req: ServerRequest) =
1616
handle(req) {
1717
val body = req.awaitBodyOrNull<String>() ?: ""
18-
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.GET)
18+
evService.handleRouting(req.user, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.GET)
1919
}
2020

2121
suspend fun handlePost(req: ServerRequest) =
2222
handle(req) {
2323
val body = req.awaitBodyOrNull<String>() ?: ""
24-
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.POST)
24+
evService.handleRouting(req.user, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.POST)
2525
}
2626

2727
suspend fun handleDelete(req: ServerRequest) =
2828
handle(req) {
2929
val body = req.awaitBodyOrNull<String>() ?: ""
30-
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.DELETE)
30+
evService.handleRouting(req.user, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.DELETE)
3131
}
3232

3333
suspend fun handlePatch(req: ServerRequest) =
3434
handle(req) {
3535
val body = req.awaitBodyOrNull<String>() ?: ""
36-
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.PATCH)
36+
evService.handleRouting(req.user, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.PATCH)
3737
}
3838

3939
suspend fun getMyLatestLectures(req: ServerRequest) =

api/src/main/kotlin/handler/RequestContext.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ fun ServerRequest.setContext(value: RequestContext) {
2020
this.attributes()[CONTEXT_ATTRIBUTE_KEY] = value
2121
}
2222

23+
val ServerRequest.user: User
24+
get() = this.getContext().user ?: throw WrongUserTokenException
25+
2326
val ServerRequest.userId: String
2427
get() = this.getContext().user?.id ?: throw WrongUserTokenException
2528

core/src/main/kotlin/common/cache/Cache.kt

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,55 +50,40 @@ class RedisCache(
5050
typeRef: TypeReference<T>,
5151
supplier: (suspend () -> T?)?,
5252
): T? {
53-
try {
54-
log.debug("[CACHE GET] {}", builtKey.key)
55-
val redisValue = redisTemplate.opsForValue().getAndAwait(builtKey.key)
56-
redisValue?.let {
57-
return objectMapper.readValue(it, typeRef)
53+
val redisValue = runCatching { redisTemplate.opsForValue().getAndAwait(builtKey.key) }.getOrNull()
54+
return if (redisValue != null) {
55+
objectMapper.readValue(redisValue, typeRef)
56+
} else {
57+
if (supplier == null) return null
58+
supplier().also { value ->
59+
coroutineScope.launch {
60+
set(builtKey, value)
61+
}
5862
}
59-
} catch (e: Exception) {
60-
log.error(e.message, e)
6163
}
62-
63-
if (supplier == null) return null
64-
65-
val value = supplier()
66-
67-
coroutineScope.launch { set(builtKey, value) }
68-
69-
return value
7064
}
7165

7266
override suspend fun <T : Any> set(
7367
builtKey: BuiltCacheKey,
7468
value: T?,
7569
) {
76-
try {
77-
val redisValue = objectMapper.writeValueAsString(value)
78-
79-
log.debug("[CACHE SET] {}", builtKey.key)
80-
redisTemplate.opsForValue().setAndAwait(builtKey.key, redisValue, builtKey.ttl)
81-
} catch (_: Exception) {
82-
}
70+
val redisValue = objectMapper.writeValueAsString(value)
71+
runCatching { redisTemplate.opsForValue().setAndAwait(builtKey.key, redisValue, builtKey.ttl) }
8372
}
8473

8574
override suspend fun delete(builtKey: BuiltCacheKey) {
86-
try {
87-
log.debug("[CACHE DEL] {}", builtKey.key)
88-
redisTemplate.deleteAndAwait(builtKey.key)
89-
} catch (_: Exception) {
90-
}
75+
runCatching { redisTemplate.deleteAndAwait(builtKey.key) }
9176
}
9277

93-
override suspend fun acquireLock(builtKey: BuiltCacheKey): Boolean {
94-
log.debug("[CACHE SETNX] {}", builtKey.key)
95-
return redisTemplate.opsForValue().setIfAbsentAndAwait(builtKey.key, "true", builtKey.ttl)
96-
}
78+
override suspend fun acquireLock(builtKey: BuiltCacheKey): Boolean =
79+
runCatching {
80+
redisTemplate.opsForValue().setIfAbsentAndAwait(builtKey.key, "true", builtKey.ttl)
81+
}.getOrDefault(false)
9782

98-
override suspend fun releaseLock(builtKey: BuiltCacheKey): Boolean {
99-
log.debug("[CACHE DEL] {}", builtKey.key)
100-
return redisTemplate.deleteAndAwait(builtKey.key) > 0
101-
}
83+
override suspend fun releaseLock(builtKey: BuiltCacheKey): Boolean =
84+
runCatching {
85+
redisTemplate.deleteAndAwait(builtKey.key) > 0
86+
}.getOrDefault(false)
10287
}
10388

10489
suspend inline fun <reified T : Any> Cache.get(

core/src/main/kotlin/common/exception/ErrorType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ enum class ErrorType(
2929
LECTURE_TIME_OVERLAP(HttpStatus.FORBIDDEN, 0x300C, "강의 시간이 겹칩니다."),
3030
CANNOT_RESET_CUSTOM_LECTURE(HttpStatus.FORBIDDEN, 0x300D, "cannot reset custom lectures"),
3131
INVALID_EMAIL(HttpStatus.FORBIDDEN, 0x300F, "email이 유효하지 않습니다.", "이메일 형식이 올바르지 않습니다."),
32+
USER_EMAIL_IS_NOT_VERIFIED(HttpStatus.FORBIDDEN, 0x3011, "User email is not verified", "이메일 인증이 필요합니다."),
3233

3334
LECTURE_NOT_FOUND(HttpStatus.NOT_FOUND, 0x4003, "lecture가 없습니다.", "해당 강의는 존재하지 않습니다."),
3435
USER_NOT_FOUND(HttpStatus.NOT_FOUND, 0x4004, "user가 없습니다.", "해당 유저는 존재하지 않습니다."),

core/src/main/kotlin/common/exception/SnuttException.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ object DuplicateLocalIdException : SnuttException(ErrorType.DUPLICATE_LOCAL_ID)
3434

3535
object InvalidEmailException : SnuttException(ErrorType.INVALID_EMAIL)
3636

37+
object UserEmailIsNotVerifiedException : SnuttException(ErrorType.USER_EMAIL_IS_NOT_VERIFIED)
38+
3739
object LectureNotFoundException : SnuttException(ErrorType.LECTURE_NOT_FOUND)
3840

3941
object UserNotFoundException : SnuttException(ErrorType.USER_NOT_FOUND)

core/src/main/kotlin/evaluation/service/EvService.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
44
import com.wafflestudio.snutt.common.dto.ListResponse
55
import com.wafflestudio.snutt.common.exception.EvDataNotFoundException
66
import com.wafflestudio.snutt.common.exception.EvServiceProxyException
7+
import com.wafflestudio.snutt.common.exception.UserEmailIsNotVerifiedException
78
import com.wafflestudio.snutt.common.util.buildMultiValueMap
89
import com.wafflestudio.snutt.config.SnuttEvWebClient
910
import com.wafflestudio.snutt.coursebook.service.CoursebookService
@@ -13,6 +14,7 @@ import com.wafflestudio.snutt.evaluation.dto.EvUserDto
1314
import com.wafflestudio.snutt.evaluation.dto.SnuttEvLectureIdDto
1415
import com.wafflestudio.snutt.evaluation.dto.SnuttEvLectureSummaryDto
1516
import com.wafflestudio.snutt.timetables.repository.TimetableRepository
17+
import com.wafflestudio.snutt.users.data.User
1618
import com.wafflestudio.snutt.users.service.UserService
1719
import kotlinx.coroutines.flow.toList
1820
import org.springframework.http.HttpHeaders
@@ -35,16 +37,19 @@ class EvService(
3537
private val objectMapper: ObjectMapper,
3638
) {
3739
suspend fun handleRouting(
38-
userId: String,
40+
user: User,
3941
requestPath: String,
4042
requestQueryParams: MultiValueMap<String, String> = buildMultiValueMap(mapOf()),
4143
originalBody: String,
4244
method: HttpMethod,
4345
): Map<String, Any?>? {
46+
if (user.isEmailVerified == false) {
47+
throw UserEmailIsNotVerifiedException
48+
}
4449
val result: MutableMap<String, Any?>? =
4550
snuttEvWebClient.method(method)
4651
.uri { builder -> builder.path(requestPath).queryParams(requestQueryParams).build() }
47-
.header("Snutt-User-Id", userId)
52+
.header("Snutt-User-Id", user.id)
4853
.header(HttpHeaders.CONTENT_ENCODING, "UTF-8")
4954
.contentType(MediaType.APPLICATION_JSON)
5055
.bodyValue(originalBody)

0 commit comments

Comments
 (0)