Skip to content

Commit

Permalink
강의 데이터 반환 전에 빌딩 정보 매핑 [이후 롤백]
Browse files Browse the repository at this point in the history
  • Loading branch information
Hank-Choi committed Apr 5, 2024
1 parent 54f972d commit b547b59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/src/main/kotlin/handler/LectureSearchHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.wafflestudio.snu4t.handler
import com.fasterxml.jackson.annotation.JsonProperty
import com.wafflestudio.snu4t.common.enum.DayOfWeek
import com.wafflestudio.snu4t.common.enum.Semester
import com.wafflestudio.snu4t.lecturebuildings.data.PlaceInfo
import com.wafflestudio.snu4t.lecturebuildings.service.LectureBuildingService
import com.wafflestudio.snu4t.lectures.dto.LectureDto
import com.wafflestudio.snu4t.lectures.dto.SearchDto
import com.wafflestudio.snu4t.lectures.dto.SearchTimeDto
import com.wafflestudio.snu4t.lectures.service.LectureService
import com.wafflestudio.snu4t.middleware.SnuttRestApiNoAuthMiddleware
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.toList
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
Expand All @@ -17,13 +22,28 @@ import org.springframework.web.reactive.function.server.awaitBody
@Component
class LectureSearchHandler(
private val lectureService: LectureService,
private val lectureBuildingService: LectureBuildingService,
snuttRestApiNoAuthMiddleware: SnuttRestApiNoAuthMiddleware,
) : ServiceHandler(
handlerMiddleware = snuttRestApiNoAuthMiddleware
) {
suspend fun searchLectures(req: ServerRequest): ServerResponse = handle(req) {
val query: SearchQueryLegacy = req.awaitBody()
lectureService.search(query.toSearchDto()).toList().map { LectureDto(it) }
.map { it.addLectureBuildings() }
}

private suspend fun LectureDto.addLectureBuildings(): LectureDto = coroutineScope {
copy(
classPlaceAndTimes = classPlaceAndTimes.map { classPlaceAndTime ->
async {
classPlaceAndTime.apply {
lectureBuildings =
place?.let { lectureBuildingService.getLectureBuildings(PlaceInfo.getValuesOf(it)) }
}
}
}.awaitAll()
)
}
}

Expand Down
27 changes: 27 additions & 0 deletions api/src/main/kotlin/handler/TimetableHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package com.wafflestudio.snu4t.handler

import com.wafflestudio.snu4t.common.enum.Semester
import com.wafflestudio.snu4t.common.exception.InvalidPathParameterException
import com.wafflestudio.snu4t.lecturebuildings.data.PlaceInfo
import com.wafflestudio.snu4t.lecturebuildings.service.LectureBuildingService
import com.wafflestudio.snu4t.middleware.SnuttRestApiDefaultMiddleware
import com.wafflestudio.snu4t.timetables.dto.TimetableLegacyDto
import com.wafflestudio.snu4t.timetables.dto.request.TimetableAddRequestDto
import com.wafflestudio.snu4t.timetables.dto.request.TimetableModifyRequestDto
import com.wafflestudio.snu4t.timetables.dto.request.TimetableModifyThemeRequestDto
import com.wafflestudio.snu4t.timetables.service.TimetableService
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.toList
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
Expand All @@ -18,6 +23,7 @@ import timetables.dto.TimetableBriefDto
@Component
class TimetableHandler(
private val timetableService: TimetableService,
private val lectureBuildingService: LectureBuildingService,
snuttRestApiDefaultMiddleware: SnuttRestApiDefaultMiddleware,
) : ServiceHandler(
handlerMiddleware = snuttRestApiDefaultMiddleware
Expand All @@ -32,6 +38,7 @@ class TimetableHandler(
val userId = req.userId

timetableService.getMostRecentlyUpdatedTimetable(userId).let(::TimetableLegacyDto)
.addLectureBuidlings()
}

suspend fun getTimetablesBySemester(req: ServerRequest): ServerResponse = handle(req) {
Expand All @@ -41,6 +48,7 @@ class TimetableHandler(
Semester.getOfValue(req.pathVariable("semester").toInt()) ?: throw InvalidPathParameterException("semester")

timetableService.getTimetablesBySemester(userId, year, semester).toList().map(::TimetableLegacyDto)
.map { it.addLectureBuidlings() }
}

suspend fun addTimetable(req: ServerRequest): ServerResponse = handle(req) {
Expand All @@ -61,6 +69,7 @@ class TimetableHandler(
val timetableId = req.pathVariable("timetableId")

timetableService.getTimetable(userId, timetableId).let(::TimetableLegacyDto)
.addLectureBuidlings()
}

suspend fun modifyTimetable(req: ServerRequest): ServerResponse = handle(req) {
Expand Down Expand Up @@ -100,6 +109,7 @@ class TimetableHandler(
val body = req.awaitBody<TimetableModifyThemeRequestDto>()

timetableService.modifyTimetableTheme(userId, timetableId, body.theme, body.themeId).let(::TimetableLegacyDto)
.addLectureBuidlings()
}

suspend fun setPrimary(req: ServerRequest): ServerResponse = handle(req) {
Expand All @@ -111,4 +121,21 @@ class TimetableHandler(
val timetableId = req.pathVariable("timetableId")
timetableService.unSetPrimary(req.userId, timetableId)
}

private suspend fun TimetableLegacyDto.addLectureBuidlings(): TimetableLegacyDto = coroutineScope {
copy(
lectures = lectures.map { lecture ->
lecture.apply {
classPlaceAndTimes = classPlaceAndTimes.map { placeAndTime ->
async {
placeAndTime.apply {
lectureBuildings =
place?.let { lectureBuildingService.getLectureBuildings(PlaceInfo.getValuesOf(it)) }
}
}
}.awaitAll()
}
}
)
}
}
2 changes: 2 additions & 0 deletions core/src/main/kotlin/lectures/dto/ClassPlaceAndTimeDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wafflestudio.snu4t.lectures.dto

import com.fasterxml.jackson.annotation.JsonProperty
import com.wafflestudio.snu4t.common.enum.DayOfWeek
import com.wafflestudio.snu4t.lecturebuildings.data.LectureBuilding
import com.wafflestudio.snu4t.lectures.data.ClassPlaceAndTime
import com.wafflestudio.snu4t.lectures.utils.endPeriod
import com.wafflestudio.snu4t.lectures.utils.minuteToString
Expand Down Expand Up @@ -34,6 +35,7 @@ data class ClassPlaceAndTimeLegacyDto(
val periodLength: Double,
@JsonProperty("start")
val startPeriod: Double,
var lectureBuildings: List<LectureBuilding>? = null,
)

fun ClassPlaceAndTimeLegacyDto(classPlaceAndTime: ClassPlaceAndTime): ClassPlaceAndTimeLegacyDto = ClassPlaceAndTimeLegacyDto(
Expand Down

0 comments on commit b547b59

Please sign in to comment.