|
1 | 1 | package com.wafflestudio.snu4t.handler |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.databind.ObjectMapper |
4 | | -import com.wafflestudio.snu4t.config.SnuttEvWebClient |
5 | | -import com.wafflestudio.snu4t.coursebook.service.CoursebookService |
6 | | -import com.wafflestudio.snu4t.evaluation.dto.EvLectureInfoDto |
| 3 | +import com.wafflestudio.snu4t.evaluation.service.EvService |
7 | 4 | import com.wafflestudio.snu4t.middleware.SnuttRestApiDefaultMiddleware |
8 | | -import com.wafflestudio.snu4t.timetables.service.TimetableService |
9 | | -import kotlinx.coroutines.flow.toList |
10 | | -import org.springframework.http.HttpHeaders |
11 | 5 | import org.springframework.http.HttpMethod |
12 | | -import org.springframework.http.MediaType |
13 | 6 | import org.springframework.stereotype.Component |
14 | | -import org.springframework.web.reactive.function.BodyInserters |
15 | | -import org.springframework.web.reactive.function.client.awaitBody |
16 | 7 | import org.springframework.web.reactive.function.server.ServerRequest |
17 | 8 | import org.springframework.web.reactive.function.server.awaitBody |
18 | | -import org.springframework.web.util.UriComponentsBuilder |
19 | | -import java.net.URLEncoder |
20 | 9 |
|
21 | 10 | @Component |
22 | 11 | class EvServiceHandler( |
23 | | - private val coursebookService: CoursebookService, |
24 | | - private val timetableService: TimetableService, |
25 | | - private val snuttEvWebClient: SnuttEvWebClient, |
| 12 | + private val evService: EvService, |
26 | 13 | snuttRestApiDefaultMiddleware: SnuttRestApiDefaultMiddleware, |
27 | 14 | ) : ServiceHandler(snuttRestApiDefaultMiddleware) { |
28 | | - suspend fun handleGet(req: ServerRequest) = handleRouting(req, HttpMethod.GET) |
29 | | - |
30 | | - suspend fun handlePost(req: ServerRequest) = handleRouting(req, HttpMethod.POST) |
31 | | - |
32 | | - suspend fun handleDelete(req: ServerRequest) = handleRouting(req, HttpMethod.DELETE) |
| 15 | + suspend fun handleGet(req: ServerRequest) = |
| 16 | + handle(req) { |
| 17 | + evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), req.awaitBody(), HttpMethod.GET) |
| 18 | + } |
33 | 19 |
|
34 | | - suspend fun handlePatch(req: ServerRequest) = handleRouting(req, HttpMethod.PATCH) |
| 20 | + suspend fun handlePost(req: ServerRequest) = |
| 21 | + handle(req) { |
| 22 | + evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), req.awaitBody(), HttpMethod.POST) |
| 23 | + } |
35 | 24 |
|
36 | | - private suspend fun handleRouting( |
37 | | - req: ServerRequest, |
38 | | - method: HttpMethod, |
39 | | - ) = handle(req) { |
40 | | - val userId = req.userId |
41 | | - val requestPath = req.pathVariable("requestPath") |
42 | | - val originalBody = req.awaitBody<String>() |
| 25 | + suspend fun handleDelete(req: ServerRequest) = |
| 26 | + handle(req) { |
| 27 | + evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), req.awaitBody(), HttpMethod.DELETE) |
| 28 | + } |
43 | 29 |
|
44 | | - snuttEvWebClient.method(method) |
45 | | - .uri { builder -> builder.path("/v1").path(requestPath).build() } |
46 | | - .header("Snutt-User-Id", userId) |
47 | | - .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) |
48 | | - .body(BodyInserters.fromValue(originalBody)) |
49 | | - .retrieve() |
50 | | - .awaitBody() |
51 | | - } |
| 30 | + suspend fun handlePatch(req: ServerRequest) = |
| 31 | + handle(req) { |
| 32 | + evService.handleRouting(req.userId, req.pathVariable("requestPath"), req.queryParams(), req.awaitBody(), HttpMethod.PATCH) |
| 33 | + } |
52 | 34 |
|
53 | 35 | suspend fun getMyLatestLectures(req: ServerRequest) = |
54 | 36 | handle(req) { |
55 | | - val userId = req.userId |
56 | | - val requestQueryParams = req.queryParams() |
57 | | - val recentLectures = |
58 | | - coursebookService.getLastTwoCourseBooks().flatMap { coursebook -> |
59 | | - timetableService.getTimetablesBySemester(userId, coursebook.year, coursebook.semester) |
60 | | - .toList() |
61 | | - .flatMap { |
62 | | - timetable -> |
63 | | - timetable.lectures.map { lecture -> EvLectureInfoDto(lecture, coursebook.year, coursebook.semester) } |
64 | | - } |
65 | | - } |
66 | | - val recentLecturesJson = ObjectMapper().writeValueAsString(recentLectures).filterNot { it.isWhitespace() } |
67 | | - val encodedJson = URLEncoder.encode(recentLecturesJson, "EUC-KR") |
68 | | - |
69 | | - snuttEvWebClient.get() |
70 | | - .uri { builder -> |
71 | | - UriComponentsBuilder.fromUri(builder.build()) |
72 | | - .path("/v1/users/me/lectures/latest") |
73 | | - .queryParam("snutt_lecture_info", encodedJson) |
74 | | - .queryParams(requestQueryParams) |
75 | | - .build(true).toUri() |
76 | | - } |
77 | | - .header("Snutt-User-Id", userId) |
78 | | - .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) |
79 | | - .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) |
80 | | - .retrieve() |
81 | | - .awaitBody() |
| 37 | + evService.getMyLatestLectures(req.userId, req.queryParams()) |
82 | 38 | } |
83 | 39 | } |
0 commit comments