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.transactions.transaction import org.jetbrains.exposed.sql.update import suwayomi.tachidesk.graphql.types.ChapterType 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 UpdateChapterPatch( val isBookmarked: Boolean? = null, val isRead: Boolean? = null, val lastPageRead: Int? = null ) data class UpdateChapterPayload( val clientMutationId: String?, val chapter: ChapterType ) data class UpdateChapterInput( val clientMutationId: String?, val id: Int, val patch: UpdateChapterPatch ) 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 (patch.isRead != null || patch.isBookmarked != null || patch.lastPageRead != null) { val now = Instant.now().epochSecond ChapterTable.update({ ChapterTable.id inList ids }) { update -> patch.isRead?.also { update[isRead] = it } patch.isBookmarked?.also { update[isBookmarked] = it } patch.lastPageRead?.also { update[lastPageRead] = it update[lastReadAt] = now } } } } } 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 ) } } }