Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query for mangas in specific categories #712

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import suwayomi.tachidesk.graphql.server.primitives.lessNotUnique
import suwayomi.tachidesk.graphql.server.primitives.maybeSwap
import suwayomi.tachidesk.graphql.types.MangaNodeList
import suwayomi.tachidesk.graphql.types.MangaType
import suwayomi.tachidesk.manga.model.table.CategoryMangaTable
import suwayomi.tachidesk.manga.model.table.MangaStatus
import suwayomi.tachidesk.manga.model.table.MangaTable
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -110,6 +111,7 @@ class MangaQuery {
val realUrl: String? = null,
val lastFetchedAt: Long? = null,
val chaptersLastFetchedAt: Long? = null,
val categoryIds: List<Int>? = null,
) : HasGetOp {
override fun getOp(): Op<Boolean>? {
val opAnd = OpAnd()
Expand All @@ -129,6 +131,7 @@ class MangaQuery {
opAnd.eq(realUrl, MangaTable.realUrl)
opAnd.eq(lastFetchedAt, MangaTable.lastFetchedAt)
opAnd.eq(chaptersLastFetchedAt, MangaTable.chaptersLastFetchedAt)
opAnd.inList(categoryIds, CategoryMangaTable.category)

return opAnd.op
}
Expand Down Expand Up @@ -179,6 +182,7 @@ class MangaQuery {
val realUrl: StringFilter? = null,
val lastFetchedAt: LongFilter? = null,
val chaptersLastFetchedAt: LongFilter? = null,
val categoryId: IntFilter? = null,
override val and: List<MangaFilter>? = null,
override val or: List<MangaFilter>? = null,
override val not: MangaFilter? = null,
Expand All @@ -200,6 +204,7 @@ class MangaQuery {
andFilterWithCompareString(MangaTable.realUrl, realUrl),
andFilterWithCompare(MangaTable.lastFetchedAt, lastFetchedAt),
andFilterWithCompare(MangaTable.chaptersLastFetchedAt, chaptersLastFetchedAt),
andFilterWithCompareEntity(CategoryMangaTable.category, categoryId),
)
}
}
Expand All @@ -217,7 +222,7 @@ class MangaQuery {
): MangaNodeList {
val queryResults =
transaction {
val res = MangaTable.selectAll()
val res = MangaTable.leftJoin(CategoryMangaTable).selectAll()

res.applyOps(condition, filter)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ class OpAnd(var op: Op<Boolean>? = null) {
op = if (op == null) expr else (op!! and expr)
}

fun <T : Any> andWhere(
values: List<T>?,
andPart: SqlExpressionBuilder.(List<T>) -> Op<Boolean>,
) {
values ?: return
val expr = Op.build { andPart(values) }
op = if (op == null) expr else (op!! and expr)
}

fun <T> eq(
value: T?,
column: Column<T>,
Expand All @@ -391,6 +400,17 @@ class OpAnd(var op: Op<Boolean>? = null) {
value: T?,
column: Column<EntityID<T>>,
) = andWhere(value) { column eq it }

fun <T> inList(
values: List<T>?,
column: Column<T>,
) = andWhere(values) { column inList it }

@JvmName("inListComparable")
fun <T : Comparable<T>> inList(
values: List<T>?,
column: Column<EntityID<T>>,
) = andWhere(values) { column inList it }
}

fun <T : Comparable<T>> andFilterWithCompare(
Expand Down