Files
suwayomi-material-you-webui/server/src/main/kotlin/suwayomi/tachidesk/impl/Library.kt

69 lines
2.6 KiB
Kotlin
Raw Normal View History

2021-05-27 01:55:21 +04:30
package suwayomi.tachidesk.impl
2021-02-13 21:12:18 +03:30
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
2021-02-20 01:23:52 +03:30
import org.jetbrains.exposed.sql.and
2021-02-21 04:41:56 +03:30
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
2021-02-13 21:12:18 +03:30
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
2021-05-27 01:55:21 +04:30
import suwayomi.tachidesk.impl.Manga.getManga
import suwayomi.tachidesk.model.database.table.CategoryMangaTable
import suwayomi.tachidesk.model.database.table.CategoryTable
import suwayomi.tachidesk.model.database.table.MangaTable
import suwayomi.tachidesk.model.database.table.toDataClass
import suwayomi.tachidesk.model.dataclass.MangaDataClass
2021-02-13 21:12:18 +03:30
2021-03-30 21:04:06 +04:30
object Library {
// TODO: `Category.isLanding` is to handle the default categories a new library manga gets,
2021-03-30 20:39:40 +04:30
// ..implement that shit at some time...
// ..also Consider to rename it to `isDefault`
2021-04-01 16:07:35 -04:00
suspend fun addMangaToLibrary(mangaId: Int) {
val manga = getManga(mangaId)
if (!manga.inLibrary) {
transaction {
val defaultCategories = CategoryTable.select { CategoryTable.isDefault eq true }.toList()
MangaTable.update({ MangaTable.id eq manga.id }) {
it[MangaTable.inLibrary] = true
it[MangaTable.defaultCategory] = defaultCategories.isEmpty()
}
defaultCategories.forEach { category ->
CategoryMangaTable.insert {
it[CategoryMangaTable.category] = category[CategoryTable.id].value
it[CategoryMangaTable.manga] = mangaId
}
}
2021-02-13 21:12:18 +03:30
}
}
}
2021-04-01 16:07:35 -04:00
suspend fun removeMangaFromLibrary(mangaId: Int) {
val manga = getManga(mangaId)
if (manga.inLibrary) {
transaction {
MangaTable.update({ MangaTable.id eq manga.id }) {
it[inLibrary] = false
it[defaultCategory] = true
}
CategoryMangaTable.deleteWhere { CategoryMangaTable.manga eq mangaId }
2021-02-13 21:12:18 +03:30
}
}
}
fun getLibraryMangas(): List<MangaDataClass> {
return transaction {
MangaTable.select { (MangaTable.inLibrary eq true) and (MangaTable.defaultCategory eq true) }.map {
MangaTable.toDataClass(it)
}
2021-02-13 21:12:18 +03:30
}
}
}