Skip to content

Commit

Permalink
BookmarkLectureDto 변환에 별도 함수 사용하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
asp345 committed Jul 28, 2024
1 parent 07ec3c6 commit 4ce52e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
10 changes: 9 additions & 1 deletion api/src/main/kotlin/handler/BookmarkHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wafflestudio.snu4t.handler

import com.wafflestudio.snu4t.bookmark.dto.BookmarkLectureModifyRequest
import com.wafflestudio.snu4t.bookmark.dto.BookmarkResponse
import com.wafflestudio.snu4t.bookmark.service.BookmarkService
import com.wafflestudio.snu4t.common.dto.ExistenceResponse
import com.wafflestudio.snu4t.common.enum.Semester
Expand All @@ -19,7 +20,14 @@ class BookmarkHandler(
val userId: String = req.userId
val year: Int = req.parseRequiredQueryParam("year")
val semester: Semester = req.parseRequiredQueryParam("semester") { Semester.getOfValue(it.toInt()) }
bookmarkService.getBookmark(userId, year, semester)
val bookmark = bookmarkService.getBookmark(userId, year, semester)
val bookmarkLectureDtos = bookmarkService.convertBookmarkLecturesToBookmarkLectureDtos(bookmark.lectures)

BookmarkResponse(
year = bookmark.year,
semester = bookmark.semester.value,
lectures = bookmarkLectureDtos
)
}

suspend fun existsBookmarkLecture(req: ServerRequest) = handle(req) {
Expand Down
8 changes: 0 additions & 8 deletions core/src/main/kotlin/bookmark/dto/BookmarkResponse.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package com.wafflestudio.snu4t.bookmark.dto

import com.wafflestudio.snu4t.bookmark.data.Bookmark
import com.wafflestudio.snu4t.evaluation.dto.SnuttEvLectureSummaryDto
import com.wafflestudio.snu4t.lectures.dto.BookmarkLectureDto

class BookmarkResponse(
val year: Int,
val semester: Int,
val lectures: List<BookmarkLectureDto>,
)

fun BookmarkResponse(bookmark: Bookmark, snuttIdToEvLectureMap: Map<String, SnuttEvLectureSummaryDto> = mapOf()): BookmarkResponse = BookmarkResponse(
year = bookmark.year,
semester = bookmark.semester.value,
lectures = bookmark.lectures.map { BookmarkLectureDto(it, snuttIdToEvLectureMap[it.id]) },
)
23 changes: 14 additions & 9 deletions core/src/main/kotlin/bookmark/service/BookmarkService.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.wafflestudio.snu4t.bookmark.service

import com.wafflestudio.snu4t.bookmark.data.Bookmark
import com.wafflestudio.snu4t.bookmark.dto.BookmarkResponse
import com.wafflestudio.snu4t.bookmark.repository.BookmarkRepository
import com.wafflestudio.snu4t.common.enum.Semester
import com.wafflestudio.snu4t.common.exception.LectureNotFoundException
import com.wafflestudio.snu4t.evaluation.repository.SnuttEvRepository
import com.wafflestudio.snu4t.lectures.data.BookmarkLecture
import com.wafflestudio.snu4t.lectures.dto.BookmarkLectureDto
import com.wafflestudio.snu4t.lectures.service.LectureService
import org.springframework.stereotype.Service

interface BookmarkService {
suspend fun getBookmark(userId: String, year: Int, semester: Semester): BookmarkResponse
suspend fun getBookmark(userId: String, year: Int, semester: Semester): Bookmark
suspend fun existsBookmarkLecture(userId: String, lectureId: String): Boolean
suspend fun addLecture(userId: String, lectureId: String): Bookmark
suspend fun deleteLecture(userId: String, lectureId: String): Bookmark
suspend fun convertBookmarkLecturesToBookmarkLectureDtos(bookmarkLectures: List<BookmarkLecture>): List<BookmarkLectureDto>
}

@Service
Expand All @@ -27,13 +28,8 @@ class BookmarkServiceImpl(
userId: String,
year: Int,
semester: Semester
): BookmarkResponse {
val bookmark = bookmarkRepository.findFirstByUserIdAndYearAndSemester(userId, year, semester)
?: Bookmark(userId = userId, year = year, semester = semester)
val snuttIdToEvLectureMap =
snuttEvRepository.getSummariesByIds(bookmark.lectures.map { it.id!! }).associateBy { it.snuttId }
return BookmarkResponse(bookmark, snuttIdToEvLectureMap)
}
): Bookmark = bookmarkRepository.findFirstByUserIdAndYearAndSemester(userId, year, semester)
?: Bookmark(userId = userId, year = year, semester = semester)

override suspend fun existsBookmarkLecture(userId: String, lectureId: String): Boolean {
val lecture = lectureService.getByIdOrNull(lectureId) ?: throw LectureNotFoundException
Expand All @@ -60,4 +56,13 @@ class BookmarkServiceImpl(
lectureId
)
}

override suspend fun convertBookmarkLecturesToBookmarkLectureDtos(bookmarkLectures: List<BookmarkLecture>): List<BookmarkLectureDto> {
val snuttIdtoLectureMap =
snuttEvRepository.getSummariesByIds(bookmarkLectures.map { it.id!! }).associateBy { it.snuttId }
return bookmarkLectures.map { bookmarkLecture ->
val snuttEvLecture = snuttIdtoLectureMap[bookmarkLecture.id]
BookmarkLectureDto(bookmarkLecture, snuttEvLecture)
}
}
}

0 comments on commit 4ce52e4

Please sign in to comment.