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/*