Skip to content

Commit

Permalink
feat(rest api): ability to re-analyze a book
Browse files Browse the repository at this point in the history
working towards #51
  • Loading branch information
gotson committed Jan 3, 2020
1 parent 7e6e190 commit 8e81356
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.WebRequest
import org.springframework.web.server.ResponseStatusException
import java.io.File
import java.io.FileNotFoundException
import java.nio.file.NoSuchFileException
import java.time.ZoneOffset
import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.TimeUnit

private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -231,6 +235,19 @@ class BookController(
}
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)

@PostMapping("api/v1/books/{bookId}/analyze")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ResponseStatus(HttpStatus.ACCEPTED)
fun analyze(@PathVariable bookId: Long) {
bookRepository.findByIdOrNull((bookId))?.let { book ->
try {
bookLifecycle.analyzeAndPersist(book)
} catch (e: RejectedExecutionException) {
throw ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Another book analysis task is already running")
}
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
}

private fun ResponseEntity.BodyBuilder.setNotModified(book: Book) =
this.cacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS)
.cachePrivate()
Expand Down

0 comments on commit 8e81356

Please sign in to comment.