From 3d95aa9fc2817e5ed235663e7efd265512cb7bee Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Tue, 22 Mar 2022 17:27:11 +0100 Subject: [PATCH 1/3] Prepare --- src/main/webapp/index.jsp | 12 +----------- src/main/webapp/js/main.js | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 9d4aea4..6ae2bc8 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -15,18 +15,8 @@ FilmFlix logo -
-
-
-
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At debitis expedita quaerat? Ad delectus dolorem dolorum excepturi impedit quae recusandae! Alias at aut corporis eius, hic ipsum recusandae sequi velit?
-

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque dicta necessitatibus sequi. Accusantium architecto corporis, deleniti dolorem eligendi et excepturi fugit id in iste, magni officia quos ut vel velit? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum, numquam quibusdam? Adipisci corporis dicta eos, esse eveniet hic id iusto maxime minima, natus neque nihil, porro quas soluta unde voluptate. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium architecto commodi dignissimos dolore ducimus eum fuga hic ipsa laborum minima mollitia, nulla officia quasi recusandae repellendus reprehenderit, sed similique, temporibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores aut autem beatae cupiditate hic magnam maxime natus neque odit perferendis porro quaerat quidem repellendus sequi sunt temporibus, ut velit voluptatum!

+
-
- Il y a 20 minutes - 1h20min -
-
-
diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index c3667bf..36f4b98 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -1,7 +1,23 @@ $(document).ready(function () { console.log("OK"); - $.get("https://ziedelth.fr/api/v1/country/fr/page/1/limit/9/episodes", function (data) { - console.table(data); - }); -}); \ No newline at end of file + $("#films").append(generateFilmCard("lorem", "lorem")); + + // $.get("https://ziedelth.fr/api/v1/country/fr/page/1/limit/9/episodes", function (data) { + // console.table(data); + // }); +}); + +function generateFilmCard(title, description) { + return `
+
+
${title}
+

${description}

+ +
+ Il y a 20 minutes + 1h20min +
+
+
`; +} \ No newline at end of file From 5d6642e69612228e25a570d486711a8ce658e46c Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 22 Mar 2022 17:40:24 +0100 Subject: [PATCH 2/3] Setup Film model, manager and controller Rest API with GET, POST, PUT, DELETE --- example.sql | 9 ++ pom.xml | 36 ++++++- .../AdminController.java} | 6 +- .../lyon1/iut/controllers/FilmController.java | 58 ++++++++++ .../java/fr/univ/lyon1/iut/models/Film.java | 52 +++++++++ .../fr/univ/lyon1/iut/models/ManageFilm.java | 102 ++++++++++++++++++ src/main/resources/hibernate.cfg.xml | 29 +++++ src/main/webapp/WEB-INF/web.xml | 10 +- 8 files changed, 290 insertions(+), 12 deletions(-) create mode 100644 example.sql rename src/main/java/fr/univ/lyon1/iut/{Resource.java => controllers/AdminController.java} (80%) create mode 100644 src/main/java/fr/univ/lyon1/iut/controllers/FilmController.java create mode 100644 src/main/java/fr/univ/lyon1/iut/models/Film.java create mode 100644 src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/example.sql b/example.sql new file mode 100644 index 0000000..5ceef6a --- /dev/null +++ b/example.sql @@ -0,0 +1,9 @@ +create table FILM +( + id int auto_increment + primary key, + name varchar(248) not null, + description text not null, + date datetime not null, + duration float not null +); diff --git a/pom.xml b/pom.xml index 6b32426..8272cae 100644 --- a/pom.xml +++ b/pom.xml @@ -17,14 +17,15 @@ UTF-8 1.7 1.7 - 3.0.10.Final + 3.15.3.Final + 1.7.36 junit junit - 4.11 + 4.13.2 test @@ -43,6 +44,37 @@ resteasy-multipart-provider ${resteasy-version} + + org.hibernate + hibernate-core + 5.6.7.Final + + + org.slf4j + slf4j-api + ${slf4j-version} + + + org.slf4j + slf4j-simple + ${slf4j-version} + + + org.mariadb.jdbc + mariadb-java-client + 3.0.3 + + + org.projectlombok + lombok + 1.18.22 + compile + + + com.google.code.gson + gson + 2.9.0 + diff --git a/src/main/java/fr/univ/lyon1/iut/Resource.java b/src/main/java/fr/univ/lyon1/iut/controllers/AdminController.java similarity index 80% rename from src/main/java/fr/univ/lyon1/iut/Resource.java rename to src/main/java/fr/univ/lyon1/iut/controllers/AdminController.java index d69e123..8ddcf7e 100644 --- a/src/main/java/fr/univ/lyon1/iut/Resource.java +++ b/src/main/java/fr/univ/lyon1/iut/controllers/AdminController.java @@ -1,11 +1,11 @@ -package fr.univ.lyon1.iut; +package fr.univ.lyon1.iut.controllers; import javax.ws.rs.*; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -@Path("/api") -public class Resource { +@Path("/admin") +public class AdminController { @GET @Path("/isAlive") public Response isAlive() { diff --git a/src/main/java/fr/univ/lyon1/iut/controllers/FilmController.java b/src/main/java/fr/univ/lyon1/iut/controllers/FilmController.java new file mode 100644 index 0000000..36bf2f0 --- /dev/null +++ b/src/main/java/fr/univ/lyon1/iut/controllers/FilmController.java @@ -0,0 +1,58 @@ +package fr.univ.lyon1.iut.controllers; + +import com.google.gson.Gson; +import fr.univ.lyon1.iut.models.Film; +import fr.univ.lyon1.iut.models.ManageFilm; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Request; +import javax.ws.rs.core.Response; + +@Path("/film") +public class FilmController { + @GET + @Path("/") + public Response getFilm() { + ManageFilm mf = new ManageFilm(); + return Response.ok(new Gson().toJson(mf.getFilms()), MediaType.APPLICATION_JSON).build(); + } + + @POST + @Path("/") + @Consumes(MediaType.APPLICATION_JSON) + public Response addFilm(Film f) { + f.setId(null); + try { + ManageFilm mf = new ManageFilm(); + return Response.ok(mf.addFilm(f), MediaType.APPLICATION_JSON).build(); + } catch (Exception e) { + return Response.serverError().build(); + } + } + + @PUT + @Path("/") + @Consumes(MediaType.APPLICATION_JSON) + public Response updateFilm(Film f) { + try { + ManageFilm mf = new ManageFilm(); + mf.updateFilm(f); + return Response.ok("ok").build(); + } catch (Exception e) { + return Response.serverError().build(); + } + } + + @DELETE + @Path("/") + public Response deleteFilm(Film f) { + try { + ManageFilm mf = new ManageFilm(); + mf.deleteFilm(f); + return Response.ok("ok").build(); + } catch (Exception e) { + return Response.serverError().build(); + } + } +} diff --git a/src/main/java/fr/univ/lyon1/iut/models/Film.java b/src/main/java/fr/univ/lyon1/iut/models/Film.java new file mode 100644 index 0000000..2890315 --- /dev/null +++ b/src/main/java/fr/univ/lyon1/iut/models/Film.java @@ -0,0 +1,52 @@ +package fr.univ.lyon1.iut.models; + +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.GenericGenerator; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.Date; + +@Entity +@Table(name = "FILM") +public class Film implements Serializable { + @Id + @GeneratedValue(generator = "increment") + @GenericGenerator(name="increment", strategy = "increment") + @Getter + @Setter + private Long id; + + @Column(name = "name") + @Getter + @Setter + private String name; + + @Column(name = "description") + @Getter + @Setter + private String description; + + @Column(name = "date") + @Temporal(TemporalType.TIMESTAMP) + @Getter + @Setter + private Date date; + + @Column(name = "duration") + @Getter + @Setter + private long duration; + + public Film(String name, String description, Date date, long duration) { + this.name = name; + this.description = description; + this.date = date; + this.duration = duration; + } + + public Film() { + + } +} diff --git a/src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java b/src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java new file mode 100644 index 0000000..e5dc7a1 --- /dev/null +++ b/src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java @@ -0,0 +1,102 @@ +package fr.univ.lyon1.iut.models; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.ServiceRegistry; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class ManageFilm { + private static SessionFactory sessionFactory; + + public ManageFilm() { + try { + Configuration configuration = new Configuration(); + configuration.configure(); + configuration.addAnnotatedClass(Film.class); + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + } catch (Throwable th) { + System.err.println("Enitial SessionFactory creation failed" + th); + throw new ExceptionInInitializerError(th); + } + } + + public Long addFilm(Film film) { + Session session = sessionFactory.openSession(); + Transaction tx = null; + Long filmID = null; + + try { + tx = session.beginTransaction(); + filmID = (Long) session.save(film); + tx.commit(); + } catch (HibernateException e) { + if (tx!=null) tx.rollback(); + e.printStackTrace(); + } finally { + session.close(); + } + return filmID; + } + + public List getFilms() { + Session session = sessionFactory.openSession(); + List films = null; + + try { + films = session.createQuery("FROM Film", Film.class).list(); + } catch (HibernateException e) { + e.printStackTrace(); + } finally { + session.close(); + } + return films; + } + + public boolean updateFilm(Film film) { + Session session = sessionFactory.openSession(); + Transaction tx = null; + boolean result; + + try { + tx = session.beginTransaction(); + session.merge(film); + tx.commit(); + result = true; + } catch (HibernateException e) { + if (tx!=null) tx.rollback(); + e.printStackTrace(); + result = false; + } finally { + session.close(); + } + return result; + } + + public boolean deleteFilm(Film film) { + Session session = sessionFactory.openSession(); + Transaction tx = null; + boolean result; + + try { + tx = session.beginTransaction(); + session.delete(film); + tx.commit(); + result = true; + } catch (HibernateException e) { + if (tx!=null) tx.rollback(); + e.printStackTrace(); + result = false; + } finally { + session.close(); + } + return result; + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000..8bfcb25 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,29 @@ + + + + + + + org.hibernate.dialect.MariaDBDialect + + + + org.mariadb.jdbc.Driver + + + + + + jdbc:mariadb://localhost:3306/filmflix + + + + filmflix + + + + filmflix + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 4f55962..9032602 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -3,17 +3,13 @@ "http://java.sun.com/dtd/web-app_2_3.dtd" > Archetype Created Web Application - resteasy.resources - fr.univ.lyon1.iut.Resource + fr.univ.lyon1.iut.controllers.AdminController,fr.univ.lyon1.iut.controllers.FilmController resteasy.servlet.mapping.prefix - /service + /api org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap @@ -24,6 +20,6 @@ api - /service/* + /api/* From 460bdf8e1c7b456e220ebb4278ced6c4f5ba2b36 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Tue, 22 Mar 2022 17:52:42 +0100 Subject: [PATCH 3/3] Add request --- .gitignore | 1 + src/main/resources/hibernate.cfg.xml | 29 ---------------------------- src/main/webapp/js/main.js | 12 ++++++++---- 3 files changed, 9 insertions(+), 33 deletions(-) delete mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/.gitignore b/.gitignore index 2d513a0..a2b99ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea/ /target/ +/src/main/resources/hibernate.cfg.xml diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml deleted file mode 100644 index 8bfcb25..0000000 --- a/src/main/resources/hibernate.cfg.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - org.hibernate.dialect.MariaDBDialect - - - - org.mariadb.jdbc.Driver - - - - - - jdbc:mariadb://localhost:3306/filmflix - - - - filmflix - - - - filmflix - - - \ No newline at end of file diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index 36f4b98..31f59be 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -1,11 +1,15 @@ $(document).ready(function () { console.log("OK"); - $("#films").append(generateFilmCard("lorem", "lorem")); + // $("#films").append(generateFilmCard("lorem", "lorem")); - // $.get("https://ziedelth.fr/api/v1/country/fr/page/1/limit/9/episodes", function (data) { - // console.table(data); - // }); + $.get("http://localhost:8080/api/film", function (data) { + console.table(data); + + for (let dataKey of data) { + $("#films").append(generateFilmCard(dataKey.name, dataKey.description)); + } + }); }); function generateFilmCard(title, description) {