Skip to content

Commit a15ab09

Browse files
committed
Refactor BookService query logic to introduce genre_match_score and optimize sorting
- Replaced redundant `var` declaration with `val` for immutability. - Introduced `genre_match_score` to prioritize books by genre similarity. - Refactored query sorting to use the `genre_match_score` field directly.
1 parent 8babd08 commit a15ab09

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/main/kotlin/io/heapy/kotbusta/service/BookService.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class BookService {
5252
.fetch(BOOK_AUTHORS.AUTHOR_ID)
5353

5454
// Build query for similar books
55-
var query = dslContext
55+
val query = dslContext
5656
.selectDistinct(
5757
BOOKS.ID,
5858
BOOKS.TITLE,
@@ -64,7 +64,11 @@ class BookService {
6464
DSL.case_()
6565
.`when`(USER_STARS.BOOK_ID.isNotNull, DSL.inline(true))
6666
.otherwise(DSL.inline(false))
67-
.`as`("is_starred")
67+
.`as`("is_starred"),
68+
DSL.case_()
69+
.`when`(BOOKS.GENRE.eq(book.genre), DSL.inline(1))
70+
.otherwise(DSL.inline(0))
71+
.`as`("genre_match_score")
6872
)
6973
.from(BOOKS)
7074
.leftJoin(SERIES).on(BOOKS.SERIES_ID.eq(SERIES.ID))
@@ -82,10 +86,7 @@ class BookService {
8286
val results = query
8387
.where(condition)
8488
.orderBy(
85-
DSL.case_()
86-
.`when`(BOOKS.GENRE.eq(book.genre), DSL.inline(1))
87-
.otherwise(DSL.inline(0))
88-
.desc(),
89+
DSL.field("genre_match_score", Integer::class.java).desc(),
8990
BOOKS.ID.desc()
9091
)
9192
.limit(limit)

0 commit comments

Comments
 (0)