Skip to content

Commit

Permalink
Query for mangas in specific categories (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda authored Oct 16, 2023
1 parent 0ba6c88 commit e70730e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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 @@ -201,6 +205,7 @@ class MangaQuery {
andFilterWithCompareString(MangaTable.realUrl, realUrl),
andFilterWithCompare(MangaTable.lastFetchedAt, lastFetchedAt),
andFilterWithCompare(MangaTable.chaptersLastFetchedAt, chaptersLastFetchedAt),
andFilterWithCompareEntity(CategoryMangaTable.category, categoryId),
)
}
}
Expand All @@ -218,7 +223,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 @@ -392,10 +401,22 @@ class OpAnd(var op: Op<Boolean>? = null) {
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 like(
value: String?,
column: Column<String?>,
) = andWhere(value) { column like it }

}

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

0 comments on commit e70730e

Please sign in to comment.