mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-06-30 17:34:39 -05:00
Start working on mutations
This commit is contained in:
@@ -1,76 +1,91 @@
|
|||||||
package suwayomi.tachidesk.graphql.mutations
|
package suwayomi.tachidesk.graphql.mutations
|
||||||
|
|
||||||
|
import com.expediagroup.graphql.server.extensions.getValueFromDataLoader
|
||||||
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
|
import com.expediagroup.graphql.server.extensions.getValuesFromDataLoader
|
||||||
import graphql.schema.DataFetchingEnvironment
|
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.transactions.transaction
|
||||||
import org.jetbrains.exposed.sql.update
|
import org.jetbrains.exposed.sql.update
|
||||||
import suwayomi.tachidesk.graphql.types.ChapterType
|
import suwayomi.tachidesk.graphql.types.ChapterType
|
||||||
import suwayomi.tachidesk.manga.model.table.ChapterMetaTable
|
|
||||||
import suwayomi.tachidesk.manga.model.table.ChapterTable
|
import suwayomi.tachidesk.manga.model.table.ChapterTable
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.util.concurrent.CompletableFuture
|
import java.util.concurrent.CompletableFuture
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO Mutations
|
||||||
|
* - Check for updates?
|
||||||
|
* - Download
|
||||||
|
* - Delete download
|
||||||
|
*/
|
||||||
class ChapterMutation {
|
class ChapterMutation {
|
||||||
data class MetaTypeInput(
|
data class UpdateChapterPatch(
|
||||||
val key: String,
|
|
||||||
val value: String?
|
|
||||||
)
|
|
||||||
|
|
||||||
data class ChapterAttributesInput(
|
|
||||||
val isBookmarked: Boolean? = null,
|
val isBookmarked: Boolean? = null,
|
||||||
val isRead: Boolean? = null,
|
val isRead: Boolean? = null,
|
||||||
val lastPageRead: Int? = null,
|
val lastPageRead: Int? = null
|
||||||
val meta: List<MetaTypeInput>? = null
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class UpdateChapterPayload(
|
||||||
|
val clientMutationId: String?,
|
||||||
|
val chapter: ChapterType
|
||||||
|
)
|
||||||
data class UpdateChapterInput(
|
data class UpdateChapterInput(
|
||||||
val ids: List<Int>,
|
val clientMutationId: String?,
|
||||||
val attributes: ChapterAttributesInput
|
val id: Int,
|
||||||
|
val patch: UpdateChapterPatch
|
||||||
)
|
)
|
||||||
|
|
||||||
fun updateChapters(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChapterInput): CompletableFuture<List<ChapterType>> {
|
data class UpdateChaptersPayload(
|
||||||
val (ids, attributes) = input
|
val clientMutationId: String?,
|
||||||
|
val chapters: List<ChapterType>
|
||||||
|
)
|
||||||
|
data class UpdateChaptersInput(
|
||||||
|
val clientMutationId: String?,
|
||||||
|
val ids: List<Int>,
|
||||||
|
val patch: UpdateChapterPatch
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun updateChapters(ids: List<Int>, patch: UpdateChapterPatch) {
|
||||||
transaction {
|
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
|
val now = Instant.now().epochSecond
|
||||||
ChapterTable.update({ ChapterTable.id inList ids }) { update ->
|
ChapterTable.update({ ChapterTable.id inList ids }) { update ->
|
||||||
attributes.isRead?.also {
|
patch.isRead?.also {
|
||||||
update[isRead] = it
|
update[isRead] = it
|
||||||
}
|
}
|
||||||
attributes.isBookmarked?.also {
|
patch.isBookmarked?.also {
|
||||||
update[isBookmarked] = it
|
update[isBookmarked] = it
|
||||||
}
|
}
|
||||||
attributes.lastPageRead?.also {
|
patch.lastPageRead?.also {
|
||||||
update[lastPageRead] = it
|
update[lastPageRead] = it
|
||||||
update[lastReadAt] = now
|
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<Int, ChapterType>("ChapterDataLoader", ids)
|
fun updateChapter(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChapterInput): CompletableFuture<UpdateChapterPayload> {
|
||||||
|
val (clientMutationId, id, patch) = input
|
||||||
|
|
||||||
|
updateChapters(listOf(id), patch)
|
||||||
|
|
||||||
|
return dataFetchingEnvironment.getValueFromDataLoader<Int, ChapterType>("ChapterDataLoader", id).thenApply { chapter ->
|
||||||
|
UpdateChapterPayload(
|
||||||
|
clientMutationId = clientMutationId,
|
||||||
|
chapter = chapter
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateChapters(dataFetchingEnvironment: DataFetchingEnvironment, input: UpdateChaptersInput): CompletableFuture<UpdateChaptersPayload> {
|
||||||
|
val (clientMutationId, ids, patch) = input
|
||||||
|
|
||||||
|
updateChapters(ids, patch)
|
||||||
|
|
||||||
|
return dataFetchingEnvironment.getValuesFromDataLoader<Int, ChapterType>("ChapterDataLoader", ids).thenApply { chapters ->
|
||||||
|
UpdateChaptersPayload(
|
||||||
|
clientMutationId = clientMutationId,
|
||||||
|
chapters = chapters
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,14 +46,6 @@ import java.util.concurrent.CompletableFuture
|
|||||||
* TODO Queries
|
* TODO Queries
|
||||||
* - Filter in library
|
* - Filter in library
|
||||||
* - Get page list?
|
* - Get page list?
|
||||||
*
|
|
||||||
* TODO Mutations
|
|
||||||
* - Last page read
|
|
||||||
* - Read status
|
|
||||||
* - bookmark status
|
|
||||||
* - Check for updates?
|
|
||||||
* - Download
|
|
||||||
* - Delete download
|
|
||||||
*/
|
*/
|
||||||
class ChapterQuery {
|
class ChapterQuery {
|
||||||
fun chapter(dataFetchingEnvironment: DataFetchingEnvironment, id: Int): CompletableFuture<ChapterType?> {
|
fun chapter(dataFetchingEnvironment: DataFetchingEnvironment, id: Int): CompletableFuture<ChapterType?> {
|
||||||
|
|||||||
Reference in New Issue
Block a user