mirror of
https://github.com/gotson/komga.git
synced 2026-05-08 12:35:30 +02:00
feat(api): add endpoints to get previous/next book of a book
This commit is contained in:
parent
78c181e130
commit
54f583f0ce
2 changed files with 55 additions and 0 deletions
|
|
@ -45,6 +45,30 @@ export default class KomgaBooksService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getBookSiblingNext (bookId: number): Promise<BookDto> {
|
||||||
|
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<BookDto> {
|
||||||
|
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<PageDto[]> {
|
async getBookPages (bookId: number): Promise<PageDto[]> {
|
||||||
try {
|
try {
|
||||||
return (await this.http.get(`${API_BOOKS}/${bookId}/pages`)).data
|
return (await this.http.get(`${API_BOOKS}/${bookId}/pages`)).data
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,37 @@ class BookController(
|
||||||
it.toDto(includeFullUrl = principal.user.isAdmin())
|
it.toDto(includeFullUrl = principal.user.isAdmin())
|
||||||
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
|
} ?: 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 = [
|
@GetMapping(value = [
|
||||||
"api/v1/books/{bookId}/thumbnail",
|
"api/v1/books/{bookId}/thumbnail",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue