From da6a95309902a701b5aa24d8dc8da00855885477 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Fri, 14 May 2021 17:31:07 +0430 Subject: [PATCH] exposed error --- .../kotlin/ir/armor/tachidesk/impl/Chapter.kt | 22 ++++++++++++++ .../model/database/migration/M0001_Initial.kt | 15 +++++----- .../M0002_ChapterTableIndexRename.kt | 23 ++++++++++++++ .../model/database/table/ChapterTable.kt | 3 +- .../model/dataclass/ChapterDataClass.kt | 2 +- .../ir/armor/tachidesk/server/JavalinSetup.kt | 19 +++++++++++- webUI/react/src/components/ChapterCard.tsx | 30 ++++++++++++++++--- webUI/react/src/components/ReaderNavBar.tsx | 12 ++++---- webUI/react/src/screens/Reader.tsx | 2 +- webUI/react/src/typings.d.ts | 4 +-- 10 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0002_ChapterTableIndexRename.kt diff --git a/server/src/main/kotlin/ir/armor/tachidesk/impl/Chapter.kt b/server/src/main/kotlin/ir/armor/tachidesk/impl/Chapter.kt index a50ffd1..6f8fb5f 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/impl/Chapter.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/impl/Chapter.kt @@ -164,4 +164,26 @@ object Chapter { pageList.count() ) } + + fun modifyChapter(mangaId: Int, chapterIndex: Int, isRead: Boolean?, isBookmarked: Boolean?, markPrevRead: Boolean?, lastPageRead: Int?) { + transaction { + ChapterTable.update({ (ChapterTable.manga eq mangaId) and (ChapterTable.chapterIndex eq chapterIndex) }) { update -> + isRead?.also { + update[ChapterTable.isRead] = it + } + isBookmarked?.also { + update[ChapterTable.isBookmarked] = it + } + lastPageRead?.also { + update[ChapterTable.lastPageRead] = it + } + } + + markPrevRead?.let { + ChapterTable.update({ (ChapterTable.manga eq mangaId) and (ChapterTable.chapterIndex less chapterIndex) }) { + it[ChapterTable.isRead] = markPrevRead + } + } + } + } } diff --git a/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0001_Initial.kt b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0001_Initial.kt index 59e743b..bb8c976 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0001_Initial.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0001_Initial.kt @@ -1,5 +1,12 @@ package ir.armor.tachidesk.model.database.migration +/* + * 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/. */ + import eu.kanade.tachiyomi.source.model.SManga import ir.armor.tachidesk.model.database.migration.lib.Migration import org.jetbrains.exposed.dao.id.IdTable @@ -7,13 +14,6 @@ import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.transactions.transaction -/* - * 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/. */ - class M0001_Initial : Migration() { private object ExtensionTable : IntIdTable() { val apkName = varchar("apk_name", 1024) @@ -100,6 +100,7 @@ class M0001_Initial : Migration() { val manga = reference("manga", ir.armor.tachidesk.model.database.table.MangaTable) } + /** initial migration, create all tables */ override fun run() { transaction { SchemaUtils.create( diff --git a/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0002_ChapterTableIndexRename.kt b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0002_ChapterTableIndexRename.kt new file mode 100644 index 0000000..9c5eee0 --- /dev/null +++ b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/M0002_ChapterTableIndexRename.kt @@ -0,0 +1,23 @@ +package ir.armor.tachidesk.model.database.migration + +/* + * 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/. */ + +import ir.armor.tachidesk.model.database.migration.lib.Migration +import org.jetbrains.exposed.sql.transactions.TransactionManager +import org.jetbrains.exposed.sql.vendors.currentDialect + +class M0002_ChapterTableIndexRename : Migration() { + /** this migration renamed ChapterTable.NUMBER_IN_LIST to ChapterTable.INDEX */ + override fun run() { + with(TransactionManager.current()) { + exec("ALTER TABLE CHAPTER ALTER COLUMN NUMBER_IN_LIST RENAME TO INDEX") + commit() + currentDialect.resetCaches() + } + } +} diff --git a/server/src/main/kotlin/ir/armor/tachidesk/model/database/table/ChapterTable.kt b/server/src/main/kotlin/ir/armor/tachidesk/model/database/table/ChapterTable.kt index 30208ab..674935c 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/model/database/table/ChapterTable.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/model/database/table/ChapterTable.kt @@ -20,7 +20,8 @@ object ChapterTable : IntIdTable() { val isBookmarked = bool("bookmark").default(false) val lastPageRead = integer("last_page_read").default(0) - val chapterIndex = integer("number_in_list") + // index is reserved by a function + val chapterIndex = integer("index") val manga = reference("manga", MangaTable) } diff --git a/server/src/main/kotlin/ir/armor/tachidesk/model/dataclass/ChapterDataClass.kt b/server/src/main/kotlin/ir/armor/tachidesk/model/dataclass/ChapterDataClass.kt index 2f68dbb..c0d824c 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/model/dataclass/ChapterDataClass.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/model/dataclass/ChapterDataClass.kt @@ -25,7 +25,7 @@ data class ChapterDataClass( val lastPageRead: Int, /** this chapter's index, starts with 1 */ - val chapterIndex: Int? = null, + val index: Int? = null, /** total chapter count, used to calculate if there's a next and prev chapter */ val chapterCount: Int? = null, diff --git a/server/src/main/kotlin/ir/armor/tachidesk/server/JavalinSetup.kt b/server/src/main/kotlin/ir/armor/tachidesk/server/JavalinSetup.kt index 727c11b..faded10 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/server/JavalinSetup.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/server/JavalinSetup.kt @@ -13,6 +13,7 @@ import ir.armor.tachidesk.impl.CategoryManga.getMangaCategories import ir.armor.tachidesk.impl.CategoryManga.removeMangaFromCategory import ir.armor.tachidesk.impl.Chapter.getChapter import ir.armor.tachidesk.impl.Chapter.getChapterList +import ir.armor.tachidesk.impl.Chapter.modifyChapter import ir.armor.tachidesk.impl.Extension.getExtensionIcon import ir.armor.tachidesk.impl.Extension.installExtension import ir.armor.tachidesk.impl.Extension.uninstallExtension @@ -142,7 +143,7 @@ object JavalinSetup { } // icon for extension named `apkName` - app.get("/api/v1/extension/icon/:apkName") { ctx -> + app.get("/api/v1/extension/icon/:apkName") { ctx -> // TODO: move to pkgName val apkName = ctx.pathParam("apkName") ctx.result( @@ -263,6 +264,22 @@ object JavalinSetup { ctx.json(future { getChapter(chapterIndex, mangaId) }) } + // used to modify a chapter's parameters + app.patch("/api/v1/manga/:mangaId/chapter/:chapterIndex") { ctx -> + val chapterIndex = ctx.pathParam("chapterIndex").toInt() + val mangaId = ctx.pathParam("mangaId").toInt() + + val read = ctx.formParam("read")?.toBoolean() + val bookmarked = ctx.formParam("bookmarked")?.toBoolean() + val markPrevRead = ctx.formParam("markPrevRead")?.toBoolean() + val lastPageRead = ctx.formParam("lastPageRead")?.toInt() + + modifyChapter(mangaId, chapterIndex, read, bookmarked, markPrevRead, lastPageRead) + + ctx.status(200) + } + + // get page at index "index" app.get("/api/v1/manga/:mangaId/chapter/:chapterIndex/page/:index") { ctx -> val mangaId = ctx.pathParam("mangaId").toInt() val chapterIndex = ctx.pathParam("chapterIndex").toInt() diff --git a/webUI/react/src/components/ChapterCard.tsx b/webUI/react/src/components/ChapterCard.tsx index 068d00e..9877c06 100644 --- a/webUI/react/src/components/ChapterCard.tsx +++ b/webUI/react/src/components/ChapterCard.tsx @@ -16,6 +16,7 @@ import Typography from '@material-ui/core/Typography'; import { Link, useHistory } from 'react-router-dom'; import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; +import client from '../util/client'; const useStyles = makeStyles((theme) => ({ root: { @@ -65,13 +66,24 @@ export default function ChapterCard(props: IProps) { setAnchorEl(null); }; + const sendChange = (key: string, value: any) => { + console.log(`${key} -> ${value}`); + handleClose(); + + const formData = new FormData(); + formData.append(key, value); + client.patch(`/api/v1/manga/${chapter.mangaId}/chapter/${chapter.index}`, formData); + // .finally(() => triggerUpdate() + // ); + }; + return ( <>
  • {/* Download */} - Bookmark - Mark as Read - Mark previous as Read + sendChange('bookmarked', !chapter.bookmarked)}> + {chapter.bookmarked && 'Remove bookmark'} + {!chapter.bookmarked && 'Bookmark'} + + sendChange('read', !chapter.read)}> + Mark as + {' '} + {chapter.read && 'unread'} + {!chapter.read && 'read'} + + sendChange('markPrevRead', true)}> + Mark previous as Read + diff --git a/webUI/react/src/components/ReaderNavBar.tsx b/webUI/react/src/components/ReaderNavBar.tsx index 600661b..baff14c 100644 --- a/webUI/react/src/components/ReaderNavBar.tsx +++ b/webUI/react/src/components/ReaderNavBar.tsx @@ -305,11 +305,11 @@ export default function ReaderNavBar(props: IProps) { {chapter.pageCount}
    - {chapter.chapterIndex > 1 + {chapter.index > 1 && ( )} - {chapter.chapterIndex < chapter.chapterCount + {chapter.index < chapter.chapterCount && ( )} diff --git a/webUI/react/src/screens/Reader.tsx b/webUI/react/src/screens/Reader.tsx index f618b5c..2d84635 100644 --- a/webUI/react/src/screens/Reader.tsx +++ b/webUI/react/src/screens/Reader.tsx @@ -41,7 +41,7 @@ const useStyles = (settings: IReaderSettings) => makeStyles({ }); const range = (n:number) => Array.from({ length: n }, (value, key) => key); -const initialChapter = () => ({ pageCount: -1, chapterIndex: -1, chapterCount: 0 }); +const initialChapter = () => ({ pageCount: -1, index: -1, chapterCount: 0 }); export default function Reader() { const [settings, setSettings] = useLocalStorage('readerSettings', defaultReaderSettings); diff --git a/webUI/react/src/typings.d.ts b/webUI/react/src/typings.d.ts index 426e066..1af443d 100644 --- a/webUI/react/src/typings.d.ts +++ b/webUI/react/src/typings.d.ts @@ -63,14 +63,14 @@ interface IChapter { read: boolean bookmarked: boolean lastPageRead: number - chapterIndex: number + index: number chapterCount: number pageCount: number } interface IPartialChpter { pageCount: number - chapterIndex: number + index: number chapterCount: number }