| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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.JsValue;
- import play.api.libs.json.Json;
- import scala.collection.IndexedSeq;
- import scala.concurrent.Future;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutionException;
-
-
- @SuppressWarnings("SpellCheckingInspection")
- @RestController
- public class TvSeriesController {
-
- private final AkkaStreamFileProcessing akkaStreamFilesProcessing;
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- private static final int BORN_INF = 500, BORN_SUP = 5000;
-
- public TvSeriesController(AkkaStreamFileProcessing akkaStreamFilesProcessing) {
- this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
- }
-
- @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
- private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
- Future<Models.Person> futurePerson = akkaStreamFilesProcessing.getPersonById(personID);
- CompletableFuture<Models.Person> completableFuture = CompletableFutureResult.buildcompletableFuture1(futurePerson);
- while (!completableFuture.isDone()) {
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- Models.Person person = completableFuture.get();
-
- return Json.toJson(person, person.personFormat()).toString();
- }
-
- @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
- private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
- throws ExecutionException, InterruptedException {
-
- Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByName(primaryName);
- CompletableFuture<Models.Person> personCompletableFuture = CompletableFutureResult.buildcompletableFuture1(personFuture);
- while (!personCompletableFuture.isDone()) {
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- Models.Person person = personCompletableFuture.get();
- return Json.toJson(person, person.personFormat()).toString();
- }
-
- @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
- private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
- throws ExecutionException, InterruptedException {
- Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieById(tvSerieID);
- CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = CompletableFutureResult.buildcompletableFuture1(tvSerieFuture);
- while (!tvSerieCompletableFuture.isDone()) {
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
- return Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString();
- }
-
- @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
- private List<String> getTvserieByPrimaryTitle(
- @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
-
- Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle);
- CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
- CompletableFutureResult.buildcompletableFuture2(listFuture);
- while (!completableFuture.isDone()) {
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
- IndexedSeq<Models.TvSerie> tvSerieList = completableFuture.get();
- List<String> tvSeries = new ArrayList<>();
- tvSerieList.foreach(tvSerie -> {
- JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
- tvSeries.add(tvSerieJs.toString());
- return null;
- });
-
- return tvSeries;
- }
-
- @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
- private List<String> getPersonsForTvSerie(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
- throws InterruptedException, ExecutionException {
-
- Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing.getTeamOfPersonsForTvSerie(tvSerieTitle);
- CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureResult.buildcompletableFuture2(futurePersonSeq);
- while (!completableFuture.isDone()){
- logger.info("IS PROCESSING...");
- Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
- }
-
- IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
- List<String> persons = new ArrayList<>();
- personIndexedSeq.foreach(person -> {
- JsValue personJs = Json.toJson(person, person.personFormat());
- persons.add(personJs.toString());
- return null;
- });
-
- return persons;
- }
-
- @RequestMapping(value = "/persons", method = RequestMethod.GET)
- private ResponseEntity<String> getAllPersons() {
- akkaStreamFilesProcessing.getAllPersons();
- return new ResponseEntity<>("is running", HttpStatus.OK);
- }
-
- @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
- private ResponseEntity<String> getAllvSeries() {
- akkaStreamFilesProcessing.getAllTvSeries();
- return new ResponseEntity<>("is running", HttpStatus.OK);
- }
- }
|