movie importer and show planner
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package de.infinimotion.backend.endpoint;
|
||||
|
||||
import de.infinimotion.model.backend.*;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import org.eclipse.microprofile.rest.client.inject.RestClient;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@ApplicationScoped
|
||||
@Path("/importer")
|
||||
public class MovieImporter {
|
||||
|
||||
/*
|
||||
Schnittstelle Abfrage für Vorstellung: Liste mit allen Sitzplätzen mit gebucht/reserviert/frei
|
||||
*/
|
||||
|
||||
@Inject
|
||||
RequestReply requester;
|
||||
|
||||
@RestClient
|
||||
OmdbApi omdbApi;
|
||||
|
||||
@POST
|
||||
@Path("/import")
|
||||
public Film importMovie(@QueryParam("id") String id) throws Exception {
|
||||
OmdbMovie omdbMovie = omdbApi.getById(id);
|
||||
Film film = new Film();
|
||||
film.setTitle(omdbMovie.getTitle());
|
||||
film.setImage(omdbMovie.getPoster());
|
||||
|
||||
CommandWrapper requestCategories = new CommandListFilmkategorie().serialize().generateIds();
|
||||
CommandWrapper responseCategories = requester.request(requestCategories);
|
||||
List<Filmkategorie> categories = CommandListFilmkategorieResponse.deserialize(responseCategories).getList();
|
||||
String[] splitGenres = omdbMovie.getGenre().split(",");
|
||||
List<String> genres = Arrays.stream(splitGenres).map(String::toLowerCase).toList();
|
||||
Filmkategorie category = categories.stream().filter(c -> genres.contains(c.getName().toLowerCase())).findFirst().orElse(null);
|
||||
|
||||
if (category == null) {
|
||||
CommandCreateFilmkategorie requestNewCategory = new CommandCreateFilmkategorie();
|
||||
Filmkategorie newCategory = new Filmkategorie();
|
||||
newCategory.setName(splitGenres[0]);
|
||||
requestNewCategory.setFilmkategorie(newCategory);
|
||||
|
||||
CommandWrapper responseNewCategory = requester.request(requestNewCategory.serialize().tx(requestCategories));
|
||||
category = CommandCreateFilmkategorie.deserialize(responseNewCategory).getFilmkategorie();
|
||||
}
|
||||
|
||||
film.setCategory(category);
|
||||
film.setDuration(Integer.parseInt(omdbMovie.getRuntime().split(" ")[0]));
|
||||
film.setDescription(omdbMovie.getPlot());
|
||||
short rating = switch (omdbMovie.getRated()) {
|
||||
case "G" -> 0;
|
||||
case "PG" -> 6;
|
||||
case "PG-13", "TV-14" -> 12;
|
||||
case "R" -> 16;
|
||||
case "NC-17" -> 18;
|
||||
default -> 0;
|
||||
};
|
||||
film.setRating(rating);
|
||||
|
||||
CommandCreateFilm requestNewFilm = new CommandCreateFilm();
|
||||
requestNewFilm.setFilm(film);
|
||||
CommandWrapper responseNewFilm = requester.request(requestNewFilm.serialize().tx(requestCategories).commit());
|
||||
return CommandCreateFilm.deserialize(responseNewFilm).getFilm();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/search")
|
||||
public OmdbSearch search(@QueryParam("title") String title) {
|
||||
return omdbApi.searchByTitle(title);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/id")
|
||||
public OmdbMovie getById(@QueryParam("id") String id) {
|
||||
return omdbApi.getById(id);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/title")
|
||||
public OmdbMovie getByTitle(@QueryParam("title") String title) {
|
||||
return omdbApi.getByTitle(title);
|
||||
}
|
||||
|
||||
}
|
||||
27
src/main/java/de/infinimotion/backend/endpoint/OmdbApi.java
Normal file
27
src/main/java/de/infinimotion/backend/endpoint/OmdbApi.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package de.infinimotion.backend.endpoint;
|
||||
|
||||
import de.infinimotion.model.backend.OmdbMovie;
|
||||
import de.infinimotion.model.backend.OmdbSearch;
|
||||
import io.quarkus.rest.client.reactive.ClientQueryParam;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
|
||||
|
||||
@Path("/")
|
||||
@RegisterRestClient(configKey = "omdb-api")
|
||||
@ClientQueryParam(name = "apikey", value = "${omdb.api-key}")
|
||||
public interface OmdbApi {
|
||||
|
||||
@GET
|
||||
OmdbSearch searchByTitle(@QueryParam("s") String title);
|
||||
|
||||
@GET
|
||||
@ClientQueryParam(name = "plot", value = "full")
|
||||
OmdbMovie getById(@QueryParam("i") String id);
|
||||
|
||||
@GET
|
||||
@ClientQueryParam(name = "plot", value = "full")
|
||||
OmdbMovie getByTitle(@QueryParam("t") String title);
|
||||
|
||||
}
|
||||
61
src/main/java/de/infinimotion/backend/task/ShowPlanner.java
Normal file
61
src/main/java/de/infinimotion/backend/task/ShowPlanner.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package de.infinimotion.backend.task;
|
||||
|
||||
import de.infinimotion.model.backend.*;
|
||||
import io.quarkus.scheduler.Scheduled;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ShowPlanner {
|
||||
|
||||
@Inject
|
||||
RequestReply requester;
|
||||
|
||||
@Scheduled(every = "1h")
|
||||
void planShows() throws Exception {
|
||||
CommandWrapper requestPlans = new CommandListPlan().serialize().generateIds();
|
||||
List<Plan> plans = CommandListPlanResponse.deserialize(requester.request(requestPlans)).getList();
|
||||
|
||||
CommandWrapper requestShows = new CommandListVorstellung().serialize().tx(requestPlans);
|
||||
List<Vorstellung> shows = CommandListVorstellungResponse.deserialize(requester.request(requestShows)).getList();
|
||||
|
||||
boolean changed = false;
|
||||
LocalDate todayPlus14 = LocalDate.now().plusDays(14);
|
||||
for (Plan plan : plans) {
|
||||
if (plan.getFirst().isAfter(todayPlus14)) continue;
|
||||
|
||||
for (int i = 0; i < 14; i++) {
|
||||
LocalDate day = LocalDate.now().plusDays(i);
|
||||
if (day.getDayOfWeek().getValue() != plan.getWeekday()) continue;
|
||||
|
||||
boolean exists = shows.stream().anyMatch(show -> show.getHall().getId().equals(plan.getHall().getId())
|
||||
&& show.getMovie().getId().equals(plan.getMovie().getId())
|
||||
&& show.getStart().toLocalTime().equals(plan.getTime())
|
||||
&& show.getStart().toLocalDate().equals(day));
|
||||
if (exists) continue;
|
||||
|
||||
Vorstellung show = new Vorstellung();
|
||||
show.setHall(plan.getHall());
|
||||
show.setMovie(plan.getMovie());
|
||||
show.setStart(LocalDateTime.of(day, plan.getTime()));
|
||||
|
||||
CommandCreateVorstellung requestNewShow = new CommandCreateVorstellung();
|
||||
requestNewShow.setVorstellung(show);
|
||||
Vorstellung newShow = CommandCreateVorstellungResponse.deserialize(requester.request(requestNewShow.serialize().tx(requestPlans))).getVorstellung();
|
||||
System.out.println("Creating show: " + newShow);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
CommandWrapper commit = new CommandListVorstellung().serialize().tx(requestPlans).commit();
|
||||
CommandListVorstellungResponse.deserialize(requester.request(commit));
|
||||
System.out.println("Committed");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.infinimotion.backend;
|
||||
package de.infinimotion.backend.util;
|
||||
|
||||
import de.infinimotion.model.backend.CommandException;
|
||||
import de.infinimotion.model.backend.CommandWrapper;
|
||||
@@ -20,3 +20,13 @@ mp.messaging.outgoing.command.connector=smallrye-kafka
|
||||
|
||||
mp.messaging.incoming.command-replies.connector=smallrye-kafka
|
||||
mp.messaging.incoming.command-replies.auto.offset.reset=earliest
|
||||
|
||||
omdb.api-key=2a3264fc
|
||||
quarkus.rest-client.omdb-api.url=https://omdbapi.com/
|
||||
quarkus.rest-client.extensions-api.scope=jakarta.inject.Singleton
|
||||
|
||||
quarkus.http.cors.enabled=true
|
||||
quarkus.http.cors.origins=*
|
||||
quarkus.http.cors.methods=*
|
||||
quarkus.http.cors.headers=*
|
||||
quarkus.http.cors.access-control-allow-credentials=true
|
||||
Reference in New Issue
Block a user