@@ -7,7 +7,6 @@ import io.heapy.kotbusta.jooq.tables.references.*
77import io.heapy.kotbusta.model.*
88import org.jooq.*
99import org.jooq.impl.DSL
10- import java.lang.classfile.Attributes.record
1110import java.time.OffsetDateTime
1211
1312class BookService {
@@ -35,15 +34,15 @@ class BookService {
3534 )
3635 }
3736
38- context(_: TransactionContext , _ : UserSession )
37+ context(_: TransactionContext , userSession : UserSession )
3938 fun getBookById (bookId : Long ): Book ? = dslContext { dslContext ->
4039 getBookDetails(dslContext, bookId)
4140 }
4241
4342 context(_: TransactionContext , userSession: UserSession )
4443 fun getSimilarBooks (bookId : Long , limit : Int = 10): List <BookSummary > = dslContext { dslContext ->
4544 // Get book details to find similar books
46- val book = getBookDetails(bookId) ? : return @dslContext emptyList()
45+ val book = getBookDetails(dslContext, bookId) ? : return @dslContext emptyList()
4746
4847 // Get author IDs for the book
4948 val authorIds = dslContext
@@ -159,7 +158,7 @@ class BookService {
159158 val total = dslContext
160159 .selectCount()
161160 .from(USER_STARS )
162- .where(USER_STARS .USER_ID .eq(userId))
161+ .where(USER_STARS .USER_ID .eq(userSession. userId))
163162 .fetchOne(0 , Long ::class .java) ? : 0L
164163
165164 SearchResult (
@@ -205,7 +204,7 @@ class BookService {
205204 .offset(offset)
206205 .fetch()
207206
208- return buildBookSummaryList(dslContext, results)
207+ return buildBookSummaryList(results)
209208 }
210209
211210 context(_: TransactionContext , userSession: UserSession )
@@ -232,7 +231,7 @@ class BookService {
232231 conditions.add(AUTHORS .FULL_NAME .likeIgnoreCase(" %${query.author} %" ))
233232 }
234233
235- var selectQuery = dslContext
234+ val selectQuery = dslContext
236235 .selectDistinct(
237236 BOOKS .ID ,
238237 BOOKS .TITLE ,
@@ -253,22 +252,22 @@ class BookService {
253252 .leftJoin(USER_STARS )
254253 .on(BOOKS .ID .eq(USER_STARS .BOOK_ID ).and (USER_STARS .USER_ID .eq(userSession.userId)))
255254
256- if (conditions.isNotEmpty()) {
257- selectQuery = selectQuery.where(DSL .and (conditions))
255+ val results = if (conditions.isNotEmpty()) {
256+ selectQuery.where(DSL .and (conditions))
257+ } else {
258+ selectQuery
258259 }
259-
260- val results = selectQuery
261260 .orderBy(BOOKS .ID .desc())
262261 .limit(query.limit)
263262 .offset(query.offset)
264263 .fetch()
265264
266- return buildBookSummaryList(dslContext, results)
265+ return buildBookSummaryList(results)
267266 }
268267
269268 context(_: TransactionContext , userSession: UserSession )
270- private fun getBookDetails (bookId : Long ): Book ? = dslContext { dslContext ->
271- val book = dslContext
269+ private fun getBookDetails (dslContext : DSLContext , bookId : Long ): Book ? {
270+ val record = dslContext
272271 .select(
273272 BOOKS .ID ,
274273 BOOKS .TITLE ,
@@ -295,7 +294,7 @@ class BookService {
295294 .leftJoin(USER_NOTES )
296295 .on(BOOKS .ID .eq(USER_NOTES .BOOK_ID ).and (USER_NOTES .USER_ID .eq(userSession.userId)))
297296 .where(BOOKS .ID .eq(bookId))
298- .fetchOne()
297+ .fetchOne() ? : return null
299298
300299 val authors = getBookAuthors(dslContext, bookId)
301300 val series = record.get(" series_name" , String ::class .java)?.let {
@@ -344,7 +343,7 @@ class BookService {
344343 }
345344
346345 context(_: TransactionContext )
347- private fun buildBookSummaryList (results : Result <Record >): List <BookSummary > {
346+ private fun buildBookSummaryList (results : Result <out Record >): List <BookSummary > = dslContext { dslContext ->
348347 val books = mutableListOf<BookSummary >()
349348 val bookIds = mutableSetOf<Long >()
350349
@@ -386,7 +385,7 @@ class BookService {
386385 }
387386
388387 // Update books with authors
389- return books.map { book ->
388+ return @dslContext books.map { book ->
390389 book.copy(authors = bookAuthors[book.id] ? : emptyList())
391390 }
392391 }
@@ -423,16 +422,16 @@ class BookService {
423422 conditions.add(AUTHORS .FULL_NAME .likeIgnoreCase(" %${query.author} %" ))
424423 }
425424
426- var countQuery = dslContext
425+ val countQuery = dslContext
427426 .selectCount()
428427 .from(BOOKS )
429428 .leftJoin(BOOK_AUTHORS ).on(BOOKS .ID .eq(BOOK_AUTHORS .BOOK_ID ))
430429 .leftJoin(AUTHORS ).on(BOOK_AUTHORS .AUTHOR_ID .eq(AUTHORS .ID ))
431430
432- if (conditions.isNotEmpty()) {
433- countQuery = countQuery .where(DSL .and (conditions))
434- }
435-
436- return countQuery .fetchOne(0 , Long ::class .java) ? : 0L
431+ return if (conditions.isNotEmpty()) {
432+ countQuery.where(DSL .and (conditions))
433+ } else {
434+ countQuery
435+ } .fetchOne(0 , Long ::class .java) ? : 0L
437436 }
438437}
0 commit comments