From 623172af6d2d26ecb67b9ea2ea70a129d6ff1d35 Mon Sep 17 00:00:00 2001 From: Valter Martinek Date: Fri, 11 Nov 2022 00:53:43 +0100 Subject: [PATCH] Add mutation for updating chapters --- .../graphql/impl/TachideskGraphQLSchema.kt | 5 +- .../graphql/mutations/ChapterMutation.kt | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/impl/TachideskGraphQLSchema.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/impl/TachideskGraphQLSchema.kt index dc9ed9c5..e4d7763d 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/impl/TachideskGraphQLSchema.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/impl/TachideskGraphQLSchema.kt @@ -15,6 +15,7 @@ import com.expediagroup.graphql.generator.toSchema import graphql.GraphQL import graphql.scalars.ExtendedScalars import graphql.schema.GraphQLType +import suwayomi.tachidesk.graphql.mutations.ChapterMutation import suwayomi.tachidesk.graphql.queries.ChapterQuery import suwayomi.tachidesk.graphql.queries.MangaQuery import kotlin.reflect.KClass @@ -37,7 +38,9 @@ val schema = toSchema( TopLevelObject(MangaQuery()), TopLevelObject(ChapterQuery()) ), - mutations = listOf() + mutations = listOf( + TopLevelObject(ChapterMutation()) + ) ) fun getGraphQLObject(): GraphQL = GraphQL.newGraphQL(schema) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt new file mode 100644 index 00000000..c84ec775 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt @@ -0,0 +1,73 @@ +package suwayomi.tachidesk.graphql.mutations + +import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader +import graphql.schema.DataFetchingEnvironment +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList +import org.jetbrains.exposed.sql.transactions.transaction +import suwayomi.tachidesk.graphql.types.ChapterType +import suwayomi.tachidesk.manga.model.table.ChapterMetaTable +import suwayomi.tachidesk.manga.model.table.ChapterTable +import java.time.Instant +import java.util.concurrent.CompletableFuture + +class ChapterMutation { + data class MetaTypeInput( + val key: String, + val value: String? + ) + + data class ChapterAttributesInput( + val isBookmarked: Boolean? = null, + val isRead: Boolean? = null, + val lastPageRead: Int? = null, + val meta: List? = null + ) + + data class UpdateChapterInput( + val ids: List, + val attributes: ChapterAttributesInput + ) + + fun updateChapters(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChapterInput): CompletableFuture> { + val (ids, attributes) = input + + transaction { + if (attributes.isRead != null || attributes.isBookmarked != null || attributes.lastPageRead != null) { + val now = Instant.now().epochSecond + ChapterTable.update({ ChapterTable.id inList ids }) { update -> + attributes.isRead?.also { + update[isRead] = it + } + attributes.isBookmarked?.also { + update[isBookmarked] = it + } + attributes.lastPageRead?.also { + update[lastPageRead] = it + update[lastReadAt] = now + } + } + } + + if (attributes.meta != null) { + attributes.meta.forEach { metaItem -> + // Delete any existing values + // Even when updating, it is easier to just delete all and create new + ChapterMetaTable.deleteWhere { + (key eq metaItem.key) and (ref inList ids) + } + if (metaItem.value != null) { + ChapterMetaTable.batchInsert(ids) { chapterId -> + this[ChapterMetaTable.ref] = chapterId + this[ChapterMetaTable.key] = metaItem.key + this[ChapterMetaTable.value] = metaItem.value + } + } + } + } + } + + return dataFetchingEnvironment.getValuesFromDataLoader("ChapterDataLoader", ids) + } +}