Skip to content

Commit

Permalink
검색에 평점순 정렬 추가 (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
asp345 authored Aug 26, 2024
1 parent 410c24a commit db864c0
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion api/src/main/kotlin/handler/LectureSearchHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ data class SearchQueryLegacy(
val page: Int = 0,
val offset: Long = page * 20L,
val limit: Int = 20,
val sortCriteria: String? = null,
) {
fun toSearchDto(): SearchDto {
return SearchDto(
Expand All @@ -63,7 +64,8 @@ data class SearchQueryLegacy(
timesToExclude = timesToExclude,
page = page,
offset = offset,
limit = limit
limit = limit,
sortBy = sortCriteria
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wafflestudio.snu4t.sugangsnu.job.sync.service

import com.wafflestudio.snu4t.bookmark.repository.BookmarkRepository
import com.wafflestudio.snu4t.common.cache.Cache
import com.wafflestudio.snu4t.common.enum.SortCriteria
import com.wafflestudio.snu4t.coursebook.data.Coursebook
import com.wafflestudio.snu4t.coursebook.repository.CoursebookRepository
import com.wafflestudio.snu4t.lecturebuildings.data.Campus
Expand Down Expand Up @@ -129,6 +130,7 @@ class SugangSnuSyncServiceImpl(
credit = parsedTag.credit.sorted().map { "${it}학점" },
instructor = parsedTag.instructor.filterNotNull().filter { it.isNotBlank() }.sorted(),
category = parsedTag.category.filterNotNull().filter { it.isNotBlank() }.sorted(),
sortCriteria = SortCriteria.values().map { it.fullName }.filterNot { it == "기본값" }.sorted()
)
}
val tagList = tagListRepository.findByYearAndSemester(coursebook.year, coursebook.semester)
Expand Down
33 changes: 33 additions & 0 deletions core/src/main/kotlin/common/enum/SortCriteria.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.wafflestudio.snu4t.common.enum

import com.fasterxml.jackson.annotation.JsonValue
import com.wafflestudio.snu4t.common.extension.asc
import com.wafflestudio.snu4t.common.extension.desc
import com.wafflestudio.snu4t.lectures.data.EvInfo
import com.wafflestudio.snu4t.lectures.data.Lecture
import org.springframework.data.domain.Sort
import org.springframework.data.mapping.div

enum class SortCriteria(
val value: Int,
@JsonValue
val fullName: String
) {
ID(1, "기본값"),
RATING_DESC(2, "평점 높은 순"),
RATING_ASC(3, "평점 낮은 순"),
COUNT_DESC(4, "강의평 많은 순"),
COUNT_ASC(5, "강의평 적은 순");

companion object {
private val nameMap = values().associateBy { it.fullName }
fun getOfName(sortCriteriaName: String?): SortCriteria? = nameMap[sortCriteriaName]
fun getSort(sortCriteria: SortCriteria?): Sort = when (sortCriteria) {
RATING_DESC -> (Lecture::evInfo / EvInfo::avgRating).desc()
RATING_ASC -> (Lecture::evInfo / EvInfo::avgRating).asc()
COUNT_DESC -> (Lecture::evInfo / EvInfo::count).desc()
COUNT_ASC -> (Lecture::evInfo / EvInfo::count).asc()
else -> Sort.unsorted()
}
}
}
7 changes: 7 additions & 0 deletions core/src/main/kotlin/lectures/data/EvInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wafflestudio.snu4t.lectures.data

data class EvInfo(
val evId: String? = null,
val avgRating: Double?,
val count: Int?,
)
1 change: 1 addition & 0 deletions core/src/main/kotlin/lectures/data/Lecture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data class Lecture(
var courseTitle: String,
var registrationCount: Int = 0,
var wasFull: Boolean = false,
val evInfo: EvInfo? = null,
) {
infix fun equalsMetadata(other: Lecture): Boolean {
return this === other ||
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/lectures/dto/SearchDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ data class SearchDto(
val page: Int = 0,
val offset: Long = page * 20L,
val limit: Int = 20,
val sortBy: String?,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wafflestudio.snu4t.lectures.repository

import com.wafflestudio.snu4t.common.enum.SortCriteria
import com.wafflestudio.snu4t.common.extension.isEqualTo
import com.wafflestudio.snu4t.lectures.data.ClassPlaceAndTime
import com.wafflestudio.snu4t.lectures.data.Lecture
Expand Down Expand Up @@ -87,7 +88,9 @@ class LectureCustomRepositoryImpl(
}.toTypedArray()
),
)
).skip(searchCondition.offset).limit(searchCondition.limit)
)
.with(SortCriteria.getSort(SortCriteria.getOfName(searchCondition.sortBy)))
.skip(searchCondition.offset).limit(searchCondition.limit)
).asFlow()

private fun makeSearchCriteriaFromQuery(query: String): Criteria =
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/tag/data/TagList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ data class TagCollection(
val credit: List<String>,
val instructor: List<String>,
val category: List<String>,
val sortCriteria: List<String>
)

0 comments on commit db864c0

Please sign in to comment.