diff --git a/komga-webui/src/services/komga-books.service.ts b/komga-webui/src/services/komga-books.service.ts index 674a4e393..705d9c869 100644 --- a/komga-webui/src/services/komga-books.service.ts +++ b/komga-webui/src/services/komga-books.service.ts @@ -45,6 +45,30 @@ export default class KomgaBooksService { } } + async getBookSiblingNext (bookId: number): Promise { + try { + return (await this.http.get(`${API_BOOKS}/${bookId}/next`)).data + } catch (e) { + let msg = 'An error occurred while trying to retrieve book' + if (e.response.data.message) { + msg += `: ${e.response.data.message}` + } + throw new Error(msg) + } + } + + async getBookSiblingPrevious (bookId: number): Promise { + try { + return (await this.http.get(`${API_BOOKS}/${bookId}/previous`)).data + } catch (e) { + let msg = 'An error occurred while trying to retrieve book' + if (e.response.data.message) { + msg += `: ${e.response.data.message}` + } + throw new Error(msg) + } + } + async getBookPages (bookId: number): Promise { try { return (await this.http.get(`${API_BOOKS}/${bookId}/pages`)).data diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt index 16805a118..0a91ba5fc 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt @@ -128,6 +128,37 @@ class BookController( it.toDto(includeFullUrl = principal.user.isAdmin()) } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + @GetMapping("api/v1/books/{bookId}/previous") + fun getBookSiblingPrevious( + @AuthenticationPrincipal principal: KomgaPrincipal, + @PathVariable bookId: Long + ): BookDto = + bookRepository.findByIdOrNull(bookId)?.let { book -> + if (!principal.user.canAccessBook(book)) throw ResponseStatusException(HttpStatus.UNAUTHORIZED) + + val previousBook = book.series.books + .sortedByDescending { it.number } + .find { it.number < book.number } + + previousBook?.toDto(includeFullUrl = principal.user.isAdmin()) + ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + + @GetMapping("api/v1/books/{bookId}/next") + fun getBookSiblingNext( + @AuthenticationPrincipal principal: KomgaPrincipal, + @PathVariable bookId: Long + ): BookDto = + bookRepository.findByIdOrNull(bookId)?.let { book -> + if (!principal.user.canAccessBook(book)) throw ResponseStatusException(HttpStatus.UNAUTHORIZED) + + val nextBook = book.series.books + .sortedBy { it.number } + .find { it.number > book.number } + + nextBook?.toDto(includeFullUrl = principal.user.isAdmin()) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + @GetMapping(value = [ "api/v1/books/{bookId}/thumbnail",