No Description

MovieController.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package fr.natan.akkastreamfileprocessingapi.controller;
  2. import fr.natan.akkastreamfileprocessingapi.futurecompleteness.CompletableFutureResult;
  3. import fr.natan.akkastreamfileprocessingapi.models.Models;
  4. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamFileProcessing;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import play.api.libs.json.JsValue;
  14. import play.api.libs.json.Json;
  15. import scala.collection.IndexedSeq;
  16. import scala.concurrent.Future;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Random;
  20. import java.util.concurrent.CompletableFuture;
  21. import java.util.concurrent.ExecutionException;
  22. @SuppressWarnings("SpellCheckingInspection")
  23. @RestController
  24. public class MovieController {
  25. private final AkkaStreamFileProcessing akkaStreamFilesProcessing;
  26. private final CompletableFutureResult completableFutureResult = new CompletableFutureResult();
  27. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  28. private static final int BORN_INF = 1000, BORN_SUP = 5000;
  29. public MovieController(AkkaStreamFileProcessing akkaStreamFilesProcessing) {
  30. this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
  31. }
  32. @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
  33. private ResponseEntity<String> getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
  34. Future<Models.Person> futurePerson = akkaStreamFilesProcessing.getPersonById(personID);
  35. CompletableFuture<Models.Person> completableFuture = completableFutureResult.buildcompletableFuture(futurePerson);
  36. while (!completableFuture.isDone()) {
  37. logger.info("IS PROCESSING...");
  38. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  39. }
  40. Models.Person person = completableFuture.get();
  41. return new ResponseEntity<>(Json.toJson(person, person.personFormat()).toString(), HttpStatus.OK);
  42. }
  43. @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
  44. private ResponseEntity<String> getPersonByName(@PathVariable(name = "primaryName") String primaryName)
  45. throws ExecutionException, InterruptedException {
  46. Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByName(primaryName);
  47. CompletableFuture<Models.Person> personCompletableFuture = completableFutureResult.buildcompletableFuture(personFuture);
  48. while (!personCompletableFuture.isDone()) {
  49. logger.info("IS PROCESSING...");
  50. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  51. }
  52. Models.Person person = personCompletableFuture.get();
  53. return new ResponseEntity<>(Json.toJson(person, person.personFormat()).toString(), HttpStatus.OK);
  54. }
  55. @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
  56. private ResponseEntity<String> getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
  57. throws ExecutionException, InterruptedException {
  58. Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieById(tvSerieID);
  59. CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = completableFutureResult.buildcompletableFuture(tvSerieFuture);
  60. while (!tvSerieCompletableFuture.isDone()) {
  61. logger.info("IS PROCESSING...");
  62. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  63. }
  64. Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
  65. return new ResponseEntity<>(Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString(), HttpStatus.OK);
  66. }
  67. @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
  68. private List<String> getTvserieByPrimaryTitle(
  69. @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
  70. Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle);
  71. CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
  72. completableFutureResult.buildcompletableFuture_(listFuture);
  73. while (!completableFuture.isDone()) {
  74. logger.info("IS PROCESSING...");
  75. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  76. }
  77. IndexedSeq<Models.TvSerie> tvSerieList = completableFuture.get();
  78. List<String> tvSeries = new ArrayList<>();
  79. tvSerieList.foreach(tvSerie -> {
  80. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
  81. tvSeries.add(tvSerieJs.toString());
  82. return null;
  83. });
  84. return tvSeries;
  85. }
  86. ;
  87. @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
  88. private List<String> getPersonsForTvSerie(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
  89. throws InterruptedException, ExecutionException {
  90. Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing.getTeamOfPersonsForTvSerie(tvSerieTitle);
  91. CompletableFuture<IndexedSeq<Models.Person>> completableFuture = completableFutureResult.buildcompletableFuture_(futurePersonSeq);
  92. while (!completableFuture.isDone()){
  93. logger.info("IS PROCESSING...");
  94. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  95. }
  96. IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
  97. List<String> persons = new ArrayList<>();
  98. personIndexedSeq.foreach(person -> {
  99. JsValue personJs = Json.toJson(person, person.personFormat());
  100. persons.add(personJs.toString());
  101. return null;
  102. });
  103. return persons;
  104. }
  105. @RequestMapping(value = "/persons", method = RequestMethod.GET)
  106. private ResponseEntity<String> getAllPersons() {
  107. akkaStreamFilesProcessing.getAllPersons();
  108. return new ResponseEntity<>("is running", HttpStatus.OK);
  109. }
  110. @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
  111. private ResponseEntity<String> getAllvSeries() {
  112. akkaStreamFilesProcessing.getAllTvSeries();
  113. return new ResponseEntity<>("is running", HttpStatus.OK);
  114. }
  115. }

Powered by TurnKey Linux.