From da8ca2349688237cba6d390c65ef32c62f9c1be3 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Fri, 28 Apr 2023 21:29:06 -0400 Subject: [PATCH] Start working on mutations --- .../graphql/mutations/ChapterMutation.kt | 95 +++++++++++-------- .../tachidesk/graphql/queries/ChapterQuery.kt | 8 -- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt index c1019cd6..9e75b86e 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt @@ -1,76 +1,91 @@ package suwayomi.tachidesk.graphql.mutations +import com.expediagroup.graphql.server.extensions.getValueFromDataLoader import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader import graphql.schema.DataFetchingEnvironment -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq -import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList -import org.jetbrains.exposed.sql.and -import org.jetbrains.exposed.sql.batchInsert -import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update 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 +/** + * TODO Mutations + * - Check for updates? + * - Download + * - Delete download + */ class ChapterMutation { - data class MetaTypeInput( - val key: String, - val value: String? - ) - - data class ChapterAttributesInput( + data class UpdateChapterPatch( val isBookmarked: Boolean? = null, val isRead: Boolean? = null, - val lastPageRead: Int? = null, - val meta: List? = null + val lastPageRead: Int? = null ) + data class UpdateChapterPayload( + val clientMutationId: String?, + val chapter: ChapterType + ) data class UpdateChapterInput( - val ids: List, - val attributes: ChapterAttributesInput + val clientMutationId: String?, + val id: Int, + val patch: UpdateChapterPatch ) - fun updateChapters(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChapterInput): CompletableFuture> { - val (ids, attributes) = input + data class UpdateChaptersPayload( + val clientMutationId: String?, + val chapters: List + ) + data class UpdateChaptersInput( + val clientMutationId: String?, + val ids: List, + val patch: UpdateChapterPatch + ) + private fun updateChapters(ids: List, patch: UpdateChapterPatch) { transaction { - if (attributes.isRead != null || attributes.isBookmarked != null || attributes.lastPageRead != null) { + if (patch.isRead != null || patch.isBookmarked != null || patch.lastPageRead != null) { val now = Instant.now().epochSecond ChapterTable.update({ ChapterTable.id inList ids }) { update -> - attributes.isRead?.also { + patch.isRead?.also { update[isRead] = it } - attributes.isBookmarked?.also { + patch.isBookmarked?.also { update[isBookmarked] = it } - attributes.lastPageRead?.also { + patch.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) + fun updateChapter(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChapterInput): CompletableFuture { + val (clientMutationId, id, patch) = input + + updateChapters(listOf(id), patch) + + return dataFetchingEnvironment.getValueFromDataLoader("ChapterDataLoader", id).thenApply { chapter -> + UpdateChapterPayload( + clientMutationId = clientMutationId, + chapter = chapter + ) + } + } + + fun updateChapters(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChaptersInput): CompletableFuture { + val (clientMutationId, ids, patch) = input + + updateChapters(ids, patch) + + return dataFetchingEnvironment.getValuesFromDataLoader("ChapterDataLoader", ids).thenApply { chapters -> + UpdateChaptersPayload( + clientMutationId = clientMutationId, + chapters = chapters + ) + } } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/ChapterQuery.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/ChapterQuery.kt index 821639fc..f3aedcfd 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/ChapterQuery.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/ChapterQuery.kt @@ -46,14 +46,6 @@ import java.util.concurrent.CompletableFuture * TODO Queries * - Filter in library * - Get page list? - * - * TODO Mutations - * - Last page read - * - Read status - * - bookmark status - * - Check for updates? - * - Download - * - Delete download */ class ChapterQuery { fun chapter(dataFetchingEnvironment: DataFetchingEnvironment, id: Int): CompletableFuture {