mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-01 18:04:35 -05:00
add pagination to recentChapters (#246)
* add pagination to recentChapters * Use kotlin native library Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com> Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com>
This commit is contained in:
@@ -112,7 +112,7 @@ object MangaAPI {
|
||||
}
|
||||
|
||||
path("update") {
|
||||
get("recentChapters", UpdateController::recentChapters)
|
||||
get("recentChapters/{pageNum}", UpdateController::recentChapters)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,11 @@ import suwayomi.tachidesk.server.JavalinSetup.future
|
||||
object UpdateController {
|
||||
/** get recently updated manga chapters */
|
||||
fun recentChapters(ctx: Context) {
|
||||
val pageNum = ctx.pathParam("pageNum").toInt()
|
||||
|
||||
ctx.future(
|
||||
future {
|
||||
Chapter.getRecentChapters()
|
||||
Chapter.getRecentChapters(pageNum)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource.getCatalogue
|
||||
import suwayomi.tachidesk.manga.impl.util.storage.ImageResponse
|
||||
import suwayomi.tachidesk.manga.model.dataclass.ChapterDataClass
|
||||
import suwayomi.tachidesk.manga.model.dataclass.MangaChapterDataClass
|
||||
import suwayomi.tachidesk.manga.model.dataclass.PaginatedList
|
||||
import suwayomi.tachidesk.manga.model.dataclass.paginatedFrom
|
||||
import suwayomi.tachidesk.manga.model.table.ChapterMetaTable
|
||||
import suwayomi.tachidesk.manga.model.table.ChapterTable
|
||||
import suwayomi.tachidesk.manga.model.table.MangaTable
|
||||
@@ -321,17 +323,19 @@ object Chapter {
|
||||
}
|
||||
}
|
||||
|
||||
fun getRecentChapters(): List<MangaChapterDataClass> {
|
||||
return transaction {
|
||||
(ChapterTable innerJoin MangaTable)
|
||||
.select { (MangaTable.inLibrary eq true) and (ChapterTable.fetchedAt greater MangaTable.inLibraryAt) }
|
||||
.orderBy(ChapterTable.fetchedAt to SortOrder.DESC)
|
||||
.map {
|
||||
MangaChapterDataClass(
|
||||
MangaTable.toDataClass(it),
|
||||
ChapterTable.toDataClass(it)
|
||||
)
|
||||
}
|
||||
fun getRecentChapters(pageNum: Int): PaginatedList<MangaChapterDataClass> {
|
||||
return paginatedFrom(pageNum) {
|
||||
transaction {
|
||||
(ChapterTable innerJoin MangaTable)
|
||||
.select { (MangaTable.inLibrary eq true) and (ChapterTable.fetchedAt greater MangaTable.inLibraryAt) }
|
||||
.orderBy(ChapterTable.fetchedAt to SortOrder.DESC)
|
||||
.map {
|
||||
MangaChapterDataClass(
|
||||
MangaTable.toDataClass(it),
|
||||
ChapterTable.toDataClass(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package suwayomi.tachidesk.manga.model.dataclass
|
||||
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import kotlin.math.min
|
||||
|
||||
data class PaginatedList<T>(
|
||||
val page: List<T>,
|
||||
val hasNextPage: Boolean,
|
||||
)
|
||||
|
||||
const val PaginationFactor = 50
|
||||
|
||||
fun <T> paginatedFrom(
|
||||
pageNum: Int,
|
||||
paginationFactor: Int = PaginationFactor,
|
||||
lister: () -> List<T>
|
||||
): PaginatedList<T> {
|
||||
val list = lister()
|
||||
val lastIndex = list.size - 1
|
||||
|
||||
val lowerIndex = pageNum * paginationFactor
|
||||
val higherIndex = (pageNum + 1) * paginationFactor - 1
|
||||
|
||||
if (lowerIndex > lastIndex) {
|
||||
return PaginatedList(emptyList(), false)
|
||||
}
|
||||
|
||||
val sliced = list.slice(lowerIndex..min(lastIndex, higherIndex))
|
||||
|
||||
return PaginatedList(
|
||||
sliced,
|
||||
higherIndex < lastIndex
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user