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.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.JsValue; import play.api.libs.json.Json; import scala.collection.IndexedSeq; import scala.collection.immutable.List; 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; public MovieController(AkkaStreamFileProcessing akkaStreamFilesProcessing) { this.akkaStreamFilesProcessing = akkaStreamFilesProcessing; completableFutureResult = new CompletableFutureResult(); } @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET) private ResponseEntity getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException { Future personFuture = akkaStreamFilesProcessing.getPersonById(personID); CompletableFuture personCompletableFuture = this.completableFutureResult.buildcompletableFuture(personFuture); Models.Person person = personCompletableFuture.get(); JsValue personJs = Json.toJson(person, person.personFormat()); return new ResponseEntity<>(personJs.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 completableFutureResult = this.completableFutureResult.buildcompletableFuture(personFuture); Models.Person person = (Models.Person) completableFutureResult.get(); JsValue personJs = Json.toJson(person, person.personFormat()); return new ResponseEntity<>(personJs.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 = this.completableFutureResult.buildcompletableFuture(tvSerieFuture); Models.TvSerie tvSerie = tvSerieCompletableFuture.get(); JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat()); return new ResponseEntity<>(tvSerieJs.toString(), HttpStatus.OK); } @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET) private ResponseEntity> getTvserieByPrimaryTitle(@PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) { List listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle); return new ResponseEntity<>(listFuture, 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); } }