package fr.natan.akkastreamfileprocessingapi.controller; import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureResult; import fr.natan.akkastreamfileprocessingapi.models.Models; import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamFileProcessing; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import play.api.libs.json.Json; import scala.collection.IndexedSeq; import scala.concurrent.Future; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @SuppressWarnings("SpellCheckingInspection") @RestController public class MovieController { private final AkkaStreamFileProcessing akkaStreamFilesProcessing; private final CompletableFutureResult completableFutureResult = new CompletableFutureResult(); private final Logger logger = LoggerFactory.getLogger(this.getClass()); public MovieController(AkkaStreamFileProcessing akkaStreamFilesProcessing) { this.akkaStreamFilesProcessing = akkaStreamFilesProcessing; } @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET) private ResponseEntity getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException { Future futurePerson = akkaStreamFilesProcessing.getPersonById(personID); CompletableFuture completableFuture = completableFutureResult.buildcompletableFuture(futurePerson); while (!completableFuture.isDone()){ logger.info("IS PROCESSING..."); Thread.sleep(5000); } Models.Person person = completableFuture.get(); return new ResponseEntity<>(Json.toJson(person, person.personFormat()).toString(), HttpStatus.OK); } @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET) private ResponseEntity getPersonByName(@PathVariable(name = "primaryName") String primaryName) throws ExecutionException, InterruptedException { Future personFuture = akkaStreamFilesProcessing.getPersonByName(primaryName); CompletableFuture personCompletableFuture = completableFutureResult.buildcompletableFuture(personFuture); while (!personCompletableFuture.isDone()){ logger.info("IS PROCESSING..."); Thread.sleep(5000); } Models.Person person = personCompletableFuture.get(); return new ResponseEntity<>(Json.toJson(person, person.personFormat()).toString(), HttpStatus.OK); } @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET) private ResponseEntity getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID) throws ExecutionException, InterruptedException { Future tvSerieFuture = akkaStreamFilesProcessing.getTvSerieById(tvSerieID); CompletableFuture tvSerieCompletableFuture = completableFutureResult.buildcompletableFuture(tvSerieFuture); while (!tvSerieCompletableFuture.isDone()){ logger.info("IS PROCESSING..."); Thread.sleep(5000); } Models.TvSerie tvSerie = tvSerieCompletableFuture.get(); return new ResponseEntity<>(Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString(), HttpStatus.OK); } @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET) private ResponseEntity> getTvserieByPrimaryTitle( @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) { Future> listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle); IndexedSeq indexedSeq = listFuture.value().get().get(); return new ResponseEntity<>(indexedSeq, HttpStatus.OK); } @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET) private Future> getPersonsForTvSerie(@PathVariable(name = "tvSerieTitle") String tvSerieTitle) { return akkaStreamFilesProcessing.getTeamOfPersonsForTvSerie(tvSerieTitle); } @RequestMapping(value = "/persons", method = RequestMethod.GET) private ResponseEntity getAllPersons() { akkaStreamFilesProcessing.getAllPersons(); return new ResponseEntity<>("is running", HttpStatus.OK); } @RequestMapping(value = "/tvseries", method = RequestMethod.GET) private ResponseEntity getAllvSeries() { akkaStreamFilesProcessing.getAllTvSeries(); return new ResponseEntity<>("is running", HttpStatus.OK); } }