Нет описания

TvSeriesController.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 TvSeriesController {
  25. private final AkkaStreamFileProcessing akkaStreamFilesProcessing;
  26. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  27. private static final int BORN_INF = 500, BORN_SUP = 5000;
  28. public TvSeriesController(AkkaStreamFileProcessing akkaStreamFilesProcessing) {
  29. this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
  30. }
  31. @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
  32. private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
  33. Future<Models.Person> futurePerson = akkaStreamFilesProcessing.getPersonById(personID);
  34. CompletableFuture<Models.Person> completableFuture = CompletableFutureResult.buildcompletableFuture1(futurePerson);
  35. while (!completableFuture.isDone()) {
  36. logger.info("IS PROCESSING...");
  37. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  38. }
  39. Models.Person person = completableFuture.get();
  40. return Json.toJson(person, person.personFormat()).toString();
  41. }
  42. @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
  43. private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
  44. throws ExecutionException, InterruptedException {
  45. Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByName(primaryName);
  46. CompletableFuture<Models.Person> personCompletableFuture = CompletableFutureResult.buildcompletableFuture1(personFuture);
  47. while (!personCompletableFuture.isDone()) {
  48. logger.info("IS PROCESSING...");
  49. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  50. }
  51. Models.Person person = personCompletableFuture.get();
  52. return Json.toJson(person, person.personFormat()).toString();
  53. }
  54. @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
  55. private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
  56. throws ExecutionException, InterruptedException {
  57. Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieById(tvSerieID);
  58. CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = CompletableFutureResult.buildcompletableFuture1(tvSerieFuture);
  59. while (!tvSerieCompletableFuture.isDone()) {
  60. logger.info("IS PROCESSING...");
  61. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  62. }
  63. Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
  64. return Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString();
  65. }
  66. @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
  67. private List<String> getTvserieByPrimaryTitle(
  68. @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
  69. Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing.getTvSerieByPrimaryTitle(tvseriePrimaryTitle);
  70. CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
  71. CompletableFutureResult.buildcompletableFuture2(listFuture);
  72. while (!completableFuture.isDone()) {
  73. logger.info("IS PROCESSING...");
  74. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  75. }
  76. IndexedSeq<Models.TvSerie> tvSerieList = completableFuture.get();
  77. List<String> tvSeries = new ArrayList<>();
  78. tvSerieList.foreach(tvSerie -> {
  79. JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
  80. tvSeries.add(tvSerieJs.toString());
  81. return null;
  82. });
  83. return tvSeries;
  84. }
  85. @RequestMapping(value = "/persons/tvseries/title/{tvSerieTitle}", method = RequestMethod.GET)
  86. private List<String> getPersonsForTvSerie(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
  87. throws InterruptedException, ExecutionException {
  88. Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing.getTeamOfPersonsForTvSerie(tvSerieTitle);
  89. CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureResult.buildcompletableFuture2(futurePersonSeq);
  90. while (!completableFuture.isDone()){
  91. logger.info("IS PROCESSING...");
  92. Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
  93. }
  94. IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
  95. List<String> persons = new ArrayList<>();
  96. personIndexedSeq.foreach(person -> {
  97. JsValue personJs = Json.toJson(person, person.personFormat());
  98. persons.add(personJs.toString());
  99. return null;
  100. });
  101. return persons;
  102. }
  103. @RequestMapping(value = "/persons", method = RequestMethod.GET)
  104. private ResponseEntity<String> getAllPersons() {
  105. akkaStreamFilesProcessing.getAllPersons();
  106. return new ResponseEntity<>("is running", HttpStatus.OK);
  107. }
  108. @RequestMapping(value = "/tvseries", method = RequestMethod.GET)
  109. private ResponseEntity<String> getAllvSeries() {
  110. akkaStreamFilesProcessing.getAllTvSeries();
  111. return new ResponseEntity<>("is running", HttpStatus.OK);
  112. }
  113. }

Powered by TurnKey Linux.