mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-04 11:24:35 -05:00
Add mutations to update multiple metas (#1874)
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package suwayomi.tachidesk.graphql.mutations
|
||||
|
||||
import graphql.execution.DataFetcherResult
|
||||
import org.jetbrains.exposed.sql.LikePattern
|
||||
import org.jetbrains.exposed.sql.Op
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.like
|
||||
import org.jetbrains.exposed.sql.deleteWhere
|
||||
import org.jetbrains.exposed.sql.or
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import suwayomi.tachidesk.global.impl.GlobalMeta
|
||||
@@ -10,6 +15,7 @@ import suwayomi.tachidesk.global.model.table.GlobalMetaTable
|
||||
import suwayomi.tachidesk.graphql.asDataFetcherResult
|
||||
import suwayomi.tachidesk.graphql.directives.RequireAuth
|
||||
import suwayomi.tachidesk.graphql.types.GlobalMetaType
|
||||
import suwayomi.tachidesk.graphql.types.MetaInput
|
||||
|
||||
class MetaMutation {
|
||||
data class SetGlobalMetaInput(
|
||||
@@ -68,4 +74,86 @@ class MetaMutation {
|
||||
DeleteGlobalMetaPayload(clientMutationId, meta)
|
||||
}
|
||||
}
|
||||
|
||||
data class SetGlobalMetasInput(
|
||||
val clientMutationId: String? = null,
|
||||
val metas: List<MetaInput>,
|
||||
)
|
||||
|
||||
data class SetGlobalMetasPayload(
|
||||
val clientMutationId: String?,
|
||||
val metas: List<GlobalMetaType>,
|
||||
)
|
||||
|
||||
@RequireAuth
|
||||
fun setGlobalMetas(input: SetGlobalMetasInput): DataFetcherResult<SetGlobalMetasPayload?> {
|
||||
val (clientMutationId, metas) = input
|
||||
|
||||
return asDataFetcherResult {
|
||||
val metaMap = metas.associate { it.key to it.value }
|
||||
GlobalMeta.modifyMetas(metaMap)
|
||||
|
||||
val updatedMetas =
|
||||
transaction {
|
||||
GlobalMetaTable
|
||||
.selectAll()
|
||||
.where { GlobalMetaTable.key inList metaMap.keys }
|
||||
.map { GlobalMetaType(it) }
|
||||
}
|
||||
|
||||
SetGlobalMetasPayload(clientMutationId, updatedMetas)
|
||||
}
|
||||
}
|
||||
|
||||
data class DeleteGlobalMetasInput(
|
||||
val clientMutationId: String? = null,
|
||||
val keys: List<String>? = null,
|
||||
val prefixes: List<String>? = null,
|
||||
)
|
||||
|
||||
data class DeleteGlobalMetasPayload(
|
||||
val clientMutationId: String?,
|
||||
val metas: List<GlobalMetaType>,
|
||||
)
|
||||
|
||||
@RequireAuth
|
||||
fun deleteGlobalMetas(input: DeleteGlobalMetasInput): DataFetcherResult<DeleteGlobalMetasPayload?> {
|
||||
val (clientMutationId, keys, prefixes) = input
|
||||
|
||||
return asDataFetcherResult {
|
||||
require(!keys.isNullOrEmpty() || !prefixes.isNullOrEmpty()) {
|
||||
"Either 'keys' or 'prefixes' must be provided"
|
||||
}
|
||||
|
||||
val metas =
|
||||
transaction {
|
||||
val keyCondition: Op<Boolean>? = keys?.takeIf { it.isNotEmpty() }?.let { GlobalMetaTable.key inList it }
|
||||
|
||||
val prefixCondition: Op<Boolean>? =
|
||||
prefixes
|
||||
?.filter { it.isNotEmpty() }
|
||||
?.map { (GlobalMetaTable.key like LikePattern("$it%")) as Op<Boolean> }
|
||||
?.reduceOrNull { acc, op -> acc or op }
|
||||
|
||||
val finalCondition =
|
||||
if (keyCondition != null && prefixCondition != null) {
|
||||
keyCondition or prefixCondition
|
||||
} else {
|
||||
keyCondition ?: prefixCondition!!
|
||||
}
|
||||
|
||||
val metas =
|
||||
GlobalMetaTable
|
||||
.selectAll()
|
||||
.where { finalCondition }
|
||||
.map { GlobalMetaType(it) }
|
||||
|
||||
GlobalMetaTable.deleteWhere { finalCondition }
|
||||
|
||||
metas
|
||||
}
|
||||
|
||||
DeleteGlobalMetasPayload(clientMutationId, metas)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user