feat(api): add endpoints to get previous/next book of a book

This commit is contained in:
Gauthier Roebroeck 2020-01-10 17:12:57 +08:00
parent 78c181e130
commit 54f583f0ce
2 changed files with 55 additions and 0 deletions

View file

@ -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[]> {
try {
return (await this.http.get(`${API_BOOKS}/${bookId}/pages`)).data

View file

@ -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",