11package com.wafflestudio.snu4t.sugangsnu.job.sync.service
22
33import com.wafflestudio.snu4t.bookmark.repository.BookmarkRepository
4- import com.wafflestudio.snu4t.common.cache.Cache
54import com.wafflestudio.snu4t.coursebook.data.Coursebook
65import com.wafflestudio.snu4t.coursebook.repository.CoursebookRepository
76import com.wafflestudio.snu4t.lecturebuildings.data.Campus
@@ -10,6 +9,7 @@ import com.wafflestudio.snu4t.lecturebuildings.service.LectureBuildingService
109import com.wafflestudio.snu4t.lectures.data.Lecture
1110import com.wafflestudio.snu4t.lectures.service.LectureService
1211import com.wafflestudio.snu4t.lectures.utils.ClassTimeUtils
12+ import com.wafflestudio.snu4t.pre2025category.service.CategoryPre2025FetchService
1313import com.wafflestudio.snu4t.sugangsnu.common.SugangSnuRepository
1414import com.wafflestudio.snu4t.sugangsnu.common.data.SugangSnuCoursebookCondition
1515import com.wafflestudio.snu4t.sugangsnu.common.service.SugangSnuFetchService
@@ -31,6 +31,7 @@ import kotlinx.coroutines.flow.filter
3131import kotlinx.coroutines.flow.map
3232import kotlinx.coroutines.flow.merge
3333import kotlinx.coroutines.flow.toList
34+ import org.slf4j.LoggerFactory
3435import org.springframework.stereotype.Service
3536import java.time.Instant
3637import kotlin.reflect.full.memberProperties
@@ -46,32 +47,48 @@ interface SugangSnuSyncService {
4647@Service
4748class SugangSnuSyncServiceImpl (
4849 private val sugangSnuFetchService : SugangSnuFetchService ,
50+ private val categoryPre2025FetchService : CategoryPre2025FetchService ,
4951 private val lectureService : LectureService ,
5052 private val timeTableRepository : TimetableRepository ,
5153 private val sugangSnuRepository : SugangSnuRepository ,
5254 private val coursebookRepository : CoursebookRepository ,
5355 private val bookmarkRepository : BookmarkRepository ,
5456 private val tagListRepository : TagListRepository ,
5557 private val lectureBuildingService : LectureBuildingService ,
56- private val cache : Cache ,
5758) : SugangSnuSyncService {
59+ private val log = LoggerFactory .getLogger(javaClass)
60+
5861 override suspend fun updateCoursebook (coursebook : Coursebook ): List <UserLectureSyncResult > {
59- val newLectures = sugangSnuFetchService.getSugangSnuLectures(coursebook.year, coursebook.semester)
62+ val courseNumberCategoryPre2025Map = categoryPre2025FetchService.getCategoriesPre2025()
63+ val newLectures =
64+ sugangSnuFetchService.getSugangSnuLectures(coursebook.year, coursebook.semester)
65+ .map { lecture ->
66+ lecture.apply {
67+ categoryPre2025 = courseNumberCategoryPre2025Map[lecture.courseNumber]
68+ }
69+ }
6070 val oldLectures =
6171 lectureService.getLecturesByYearAndSemesterAsFlow(coursebook.year, coursebook.semester).toList()
6272 val compareResult = compareLectures(newLectures, oldLectures)
6373
6474 syncLectures(compareResult)
65- updateLectureBuildings(compareResult)
6675 val syncUserLecturesResults = syncSavedUserLectures(compareResult)
6776 syncTagList(coursebook, newLectures)
6877 coursebookRepository.save(coursebook.apply { updatedAt = Instant .now() })
78+ runCatching { updateLectureBuildings(compareResult) }.onFailure { log.error(" Failed to update lecture buildings" , it) }
6979
7080 return syncUserLecturesResults
7181 }
7282
7383 override suspend fun addCoursebook (coursebook : Coursebook ) {
74- val newLectures = sugangSnuFetchService.getSugangSnuLectures(coursebook.year, coursebook.semester)
84+ val courseNumberCategoryPre2025Map = categoryPre2025FetchService.getCategoriesPre2025()
85+ val newLectures =
86+ sugangSnuFetchService.getSugangSnuLectures(coursebook.year, coursebook.semester)
87+ .map { lecture ->
88+ lecture.apply {
89+ categoryPre2025 = courseNumberCategoryPre2025Map[lecture.courseNumber]
90+ }
91+ }
7592 lectureService.upsertLectures(newLectures)
7693 syncTagList(coursebook, newLectures)
7794
@@ -125,6 +142,7 @@ class SugangSnuSyncServiceImpl(
125142 credit = acc.credit + lecture.credit,
126143 instructor = acc.instructor + lecture.instructor,
127144 category = acc.category + lecture.category,
145+ categoryPre2025 = acc.categoryPre2025 + lecture.categoryPre2025,
128146 )
129147 }.let { parsedTag ->
130148 TagCollection (
@@ -135,6 +153,7 @@ class SugangSnuSyncServiceImpl(
135153 credit = parsedTag.credit.sorted().map { " ${it} 학점" },
136154 instructor = parsedTag.instructor.filterNotNull().filter { it.isNotBlank() }.sorted(),
137155 category = parsedTag.category.filterNotNull().filter { it.isNotBlank() }.sorted(),
156+ categoryPre2025 = parsedTag.categoryPre2025.filterNotNull().filter { it.isNotBlank() }.sorted(),
138157 )
139158 }
140159 val tagList =
@@ -182,8 +201,8 @@ class SugangSnuSyncServiceImpl(
182201 updatedLecture.oldData.semester,
183202 updatedLecture.oldData.id!! ,
184203 ).map { bookmark ->
185- bookmark. apply {
186- lectures.find { it.id == updatedLecture.oldData.id }?.apply {
204+ val updatedBookmarkLecture =
205+ bookmark. lectures.find { it.id == updatedLecture.oldData.id }?.apply {
187206 academicYear = updatedLecture.newData.academicYear
188207 category = updatedLecture.newData.category
189208 classPlaceAndTimes = updatedLecture.newData.classPlaceAndTimes
@@ -197,10 +216,9 @@ class SugangSnuSyncServiceImpl(
197216 lectureNumber = updatedLecture.newData.lectureNumber
198217 courseNumber = updatedLecture.newData.courseNumber
199218 courseTitle = updatedLecture.newData.courseTitle
200- }
201- }
202- }.let {
203- bookmarkRepository.saveAll(it)
219+ categoryPre2025 = updatedLecture.newData.categoryPre2025
220+ }!!
221+ bookmarkRepository.updateLecture(bookmark.id!! , updatedBookmarkLecture)
204222 }.map { bookmark ->
205223 BookmarkLectureUpdateResult (
206224 bookmark.year,
@@ -235,8 +253,8 @@ class SugangSnuSyncServiceImpl(
235253 updatedLecture : UpdatedLecture ,
236254 ): Flow <TimetableLectureUpdateResult > =
237255 timetables.map { timetable ->
238- timetable. apply {
239- lectures.find { it.lectureId == updatedLecture.oldData.id }?.apply {
256+ val updatedTimetableLecture =
257+ timetable. lectures.find { it.lectureId == updatedLecture.oldData.id }?.apply {
240258 academicYear = updatedLecture.newData.academicYear
241259 category = updatedLecture.newData.category
242260 classPlaceAndTimes = updatedLecture.newData.classPlaceAndTimes
@@ -250,11 +268,9 @@ class SugangSnuSyncServiceImpl(
250268 remark = updatedLecture.newData.remark
251269 courseNumber = updatedLecture.newData.courseNumber
252270 courseTitle = updatedLecture.newData.courseTitle
271+ categoryPre2025 = updatedLecture.newData.categoryPre2025
253272 }
254- updatedAt = Instant .now()
255- }
256- }.let {
257- timeTableRepository.saveAll(it)
273+ timeTableRepository.updateTimetableLecture(timetable.id!! , updatedTimetableLecture!! )
258274 }.map { timetable ->
259275 TimetableLectureUpdateResult (
260276 year = timetable.year,
@@ -347,4 +363,5 @@ data class ParsedTags(
347363 val instructor : Set <String ?> = setOf(),
348364 val category : Set <String ?> = setOf(),
349365 val etc : Set <String ?> = setOf(),
366+ val categoryPre2025 : Set <String ?> = setOf(),
350367)
0 commit comments