-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.wafflestudio.snu4t.handler | ||
|
||
import com.wafflestudio.snu4t.evaluation.service.EvService | ||
import com.wafflestudio.snu4t.middleware.SnuttRestApiDefaultMiddleware | ||
import kotlinx.coroutines.reactor.awaitSingleOrNull | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
import org.springframework.web.reactive.function.server.bodyToMono | ||
|
||
@Component | ||
class EvServiceHandler( | ||
private val evService: EvService, | ||
snuttRestApiDefaultMiddleware: SnuttRestApiDefaultMiddleware, | ||
) : ServiceHandler(snuttRestApiDefaultMiddleware) { | ||
suspend fun handleGet(req: ServerRequest) = | ||
handle(req) { | ||
val body = req.bodyToMono<String>().awaitSingleOrNull() ?: "" | ||
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.GET) | ||
} | ||
|
||
suspend fun handlePost(req: ServerRequest) = | ||
handle(req) { | ||
val body = req.bodyToMono<String>().awaitSingleOrNull() ?: "" | ||
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.POST) | ||
} | ||
|
||
suspend fun handleDelete(req: ServerRequest) = | ||
handle(req) { | ||
val body = req.bodyToMono<String>().awaitSingleOrNull() ?: "" | ||
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.DELETE) | ||
} | ||
|
||
suspend fun handlePatch(req: ServerRequest) = | ||
handle(req) { | ||
val body = req.bodyToMono<String>().awaitSingleOrNull() ?: "" | ||
evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), body, HttpMethod.PATCH) | ||
} | ||
|
||
suspend fun getMyLatestLectures(req: ServerRequest) = | ||
handle(req) { | ||
evService.getMyLatestLectures(req.userId, req.queryParams()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
core/src/main/kotlin/common/exception/EvServiceProxyException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.wafflestudio.snu4t.common.exception | ||
|
||
import org.springframework.http.HttpStatusCode | ||
|
||
class EvServiceProxyException( | ||
val statusCode: HttpStatusCode, | ||
val errorBody: Map<String, Any?>, | ||
) : RuntimeException() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.wafflestudio.snu4t.evaluation.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.wafflestudio.snu4t.common.enum.Semester | ||
import com.wafflestudio.snu4t.timetables.data.TimetableLecture | ||
|
||
data class EvLectureInfoDto( | ||
val year: Int, | ||
val semester: Int, | ||
val instructor: String?, | ||
@JsonProperty("course_number") | ||
val courseNumber: String?, | ||
) | ||
|
||
fun EvLectureInfoDto( | ||
timetableLecture: TimetableLecture, | ||
year: Int, | ||
semester: Semester, | ||
): EvLectureInfoDto = | ||
EvLectureInfoDto( | ||
year = year, | ||
semester = semester.value, | ||
instructor = timetableLecture.instructor, | ||
courseNumber = timetableLecture.courseNumber, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.wafflestudio.snu4t.evaluation.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.wafflestudio.snu4t.users.data.User | ||
|
||
data class EvUserDto( | ||
val id: String?, | ||
val email: String?, | ||
@JsonProperty("local_id") | ||
val localId: String?, | ||
) | ||
|
||
fun EvUserDto(user: User) = | ||
EvUserDto( | ||
id = user.id, | ||
email = user.email, | ||
localId = user.credential.localId, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.wafflestudio.snu4t.evaluation.service | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.wafflestudio.snu4t.common.exception.EvServiceProxyException | ||
import com.wafflestudio.snu4t.common.util.buildMultiValueMap | ||
import com.wafflestudio.snu4t.config.SnuttEvWebClient | ||
import com.wafflestudio.snu4t.coursebook.service.CoursebookService | ||
import com.wafflestudio.snu4t.evaluation.dto.EvLectureInfoDto | ||
import com.wafflestudio.snu4t.evaluation.dto.EvUserDto | ||
import com.wafflestudio.snu4t.timetables.service.TimetableService | ||
import com.wafflestudio.snu4t.users.service.UserService | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.HttpStatusCode | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Service | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.reactive.function.BodyInserters | ||
import org.springframework.web.reactive.function.client.bodyToMono | ||
import reactor.core.publisher.Mono | ||
|
||
@Service | ||
class EvService( | ||
private val snuttEvWebClient: SnuttEvWebClient, | ||
private val timetableService: TimetableService, | ||
private val coursebookService: CoursebookService, | ||
private val userService: UserService, | ||
private val objectMapper: ObjectMapper, | ||
) { | ||
suspend fun handleRouting( | ||
userId: String, | ||
requestPath: String, | ||
requestQueryParams: MultiValueMap<String, String> = buildMultiValueMap(mapOf()), | ||
originalBody: String, | ||
method: HttpMethod, | ||
): Map<String, Any?> { | ||
val result: MutableMap<String, Any?> = | ||
snuttEvWebClient.method(method) | ||
.uri { builder -> builder.path(requestPath).queryParams(requestQueryParams).build() } | ||
.header("Snutt-User-Id", userId) | ||
.header(HttpHeaders.CONTENT_ENCODING, "UTF-8") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.body(BodyInserters.fromValue(originalBody)) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::isError) { response -> | ||
response.bodyToMono<Map<String, Any?>>() | ||
.flatMap { errorBody -> | ||
Mono.error(EvServiceProxyException(response.statusCode(), errorBody)) | ||
} | ||
} | ||
.bodyToMono<MutableMap<String, Any?>>() | ||
.awaitSingle() | ||
return updateUserInfo(result) | ||
} | ||
|
||
suspend fun getMyLatestLectures( | ||
userId: String, | ||
requestQueryParams: MultiValueMap<String, String> = buildMultiValueMap(mapOf()), | ||
): Map<String, Any?> { | ||
val recentLectures: List<EvLectureInfoDto> = | ||
coursebookService.getLastTwoCourseBooksBeforeCurrent().flatMap { coursebook -> | ||
timetableService.getTimetablesBySemester(userId, coursebook.year, coursebook.semester) | ||
.toList() | ||
.flatMap { timetable -> | ||
timetable.lectures.map { lecture -> | ||
EvLectureInfoDto( | ||
lecture, | ||
coursebook.year, | ||
coursebook.semester, | ||
) | ||
} | ||
} | ||
} | ||
|
||
val lectureInfoParam = objectMapper.writeValueAsString(recentLectures) | ||
return snuttEvWebClient.get() | ||
.uri { builder -> | ||
builder | ||
.path("/v1/users/me/lectures/latest") | ||
.queryParam("snutt_lecture_info", "{lectureInfoParam}") | ||
.queryParams(requestQueryParams) | ||
.build(lectureInfoParam) | ||
} | ||
.header("Snutt-User-Id", userId) | ||
.header(HttpHeaders.CONTENT_ENCODING, "UTF-8") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::isError) { response -> | ||
response.bodyToMono<Map<String, Any?>>() | ||
.flatMap { errorBody -> | ||
Mono.error(EvServiceProxyException(response.statusCode(), errorBody)) | ||
} | ||
} | ||
.bodyToMono<MutableMap<String, Any?>>() | ||
.awaitSingle() | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
private suspend fun updateUserInfo(data: MutableMap<String, Any?>): MutableMap<String, Any?> { | ||
val updatedMap: MutableMap<String, Any?> = mutableMapOf() | ||
for ((k, v) in data.entries) { | ||
if (k == "user_id") { | ||
val userDto = runCatching { EvUserDto(userService.getUser(v as String)) }.getOrNull() | ||
updatedMap["user"] = userDto | ||
} else { | ||
when (v) { | ||
is List<*> -> updatedMap[k] = v.map { updateUserInfo(it as MutableMap<String, Any?>) } | ||
is MutableMap<*, *> -> updatedMap[k] = updateUserInfo(v as MutableMap<String, Any?>) | ||
else -> updatedMap[k] = v | ||
} | ||
} | ||
} | ||
return updatedMap | ||
} | ||
} |