From fa65e94ae6397fb61e04ded733db10bc997d07de Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Mon, 6 Jan 2020 11:17:02 +0800 Subject: [PATCH] feat(api): reAnalyze library --- .../domain/persistence/BookRepository.kt | 2 ++ .../interfaces/web/rest/LibraryController.kt | 22 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/BookRepository.kt b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/BookRepository.kt index 14cf95f7cae..a2727d9f8f4 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/BookRepository.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/BookRepository.kt @@ -27,6 +27,8 @@ interface BookRepository : JpaRepository, JpaSpecificationExecutor, pageable: Pageable): Page + fun findBySeriesLibraryIn(seriesLibrary: Collection): List + fun findByUrl(url: URL): Book? fun findAllByMediaStatus(status: Media.Status): List fun findAllByMediaThumbnailIsNull(): List diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/LibraryController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/LibraryController.kt index f5fa3ef1916..cf81519014a 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/LibraryController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/web/rest/LibraryController.kt @@ -5,7 +5,9 @@ import org.gotson.komga.domain.model.DirectoryNotFoundException import org.gotson.komga.domain.model.DuplicateNameException import org.gotson.komga.domain.model.Library import org.gotson.komga.domain.model.PathContainedInPath +import org.gotson.komga.domain.persistence.BookRepository import org.gotson.komga.domain.persistence.LibraryRepository +import org.gotson.komga.domain.service.AsyncOrchestrator import org.gotson.komga.domain.service.LibraryLifecycle import org.gotson.komga.infrastructure.security.KomgaPrincipal import org.springframework.data.domain.Sort @@ -24,6 +26,7 @@ import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import org.springframework.web.server.ResponseStatusException import java.io.FileNotFoundException +import java.util.concurrent.RejectedExecutionException import javax.validation.Valid import javax.validation.constraints.NotBlank @@ -32,8 +35,10 @@ private val logger = KotlinLogging.logger {} @RestController @RequestMapping("api/v1/libraries", produces = [MediaType.APPLICATION_JSON_VALUE]) class LibraryController( - private val libraryLifecycle: LibraryLifecycle, - private val libraryRepository: LibraryRepository + private val libraryLifecycle: LibraryLifecycle, + private val libraryRepository: LibraryRepository, + private val bookRepository: BookRepository, + private val asyncOrchestrator: AsyncOrchestrator ) { @GetMapping @@ -83,6 +88,19 @@ class LibraryController( libraryLifecycle.deleteLibrary(it) } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) } + + @PostMapping("{libraryId}/analyze") + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ResponseStatus(HttpStatus.ACCEPTED) + fun analyze(@PathVariable libraryId: Long) { + libraryRepository.findByIdOrNull(libraryId)?.let { library -> + try { + asyncOrchestrator.reAnalyzeBooks(bookRepository.findBySeriesLibraryIn(listOf(library))) + } catch (e: RejectedExecutionException) { + throw ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Another book analysis task is already running") + } + } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) + } } data class LibraryCreationDto(