Merge branch 'request-get' into ziedelth
This commit is contained in:
commit
e266bfdf0b
10 changed files with 285 additions and 25 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/.idea/
|
||||
/target/
|
||||
/src/main/resources/hibernate.cfg.xml
|
||||
|
|
9
example.sql
Normal file
9
example.sql
Normal file
|
@ -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
|
||||
);
|
36
pom.xml
36
pom.xml
|
@ -17,14 +17,15 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
<resteasy-version>3.0.10.Final</resteasy-version>
|
||||
<resteasy-version>3.15.3.Final</resteasy-version>
|
||||
<slf4j-version>1.7.36</slf4j-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- JBoss -->
|
||||
|
@ -43,6 +44,37 @@
|
|||
<artifactId>resteasy-multipart-provider</artifactId>
|
||||
<version>${resteasy-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>5.6.7.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.22</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -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() {
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
52
src/main/java/fr/univ/lyon1/iut/models/Film.java
Normal file
52
src/main/java/fr/univ/lyon1/iut/models/Film.java
Normal file
|
@ -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() {
|
||||
|
||||
}
|
||||
}
|
102
src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java
Normal file
102
src/main/java/fr/univ/lyon1/iut/models/ManageFilm.java
Normal file
|
@ -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<Film> getFilms() {
|
||||
Session session = sessionFactory.openSession();
|
||||
List<Film> 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;
|
||||
}
|
||||
}
|
|
@ -3,17 +3,13 @@
|
|||
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||
<web-app>
|
||||
<display-name>Archetype Created Web Application</display-name>
|
||||
<!--<context-param>
|
||||
<param-name>resteasy.scan</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param> -->
|
||||
<context-param>
|
||||
<param-name>resteasy.resources</param-name>
|
||||
<param-value>fr.univ.lyon1.iut.Resource</param-value>
|
||||
<param-value>fr.univ.lyon1.iut.controllers.AdminController,fr.univ.lyon1.iut.controllers.FilmController</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>resteasy.servlet.mapping.prefix</param-name>
|
||||
<param-value>/service</param-value>
|
||||
<param-value>/api</param-value>
|
||||
</context-param>
|
||||
<listener>
|
||||
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
|
||||
|
@ -24,6 +20,6 @@
|
|||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>api</servlet-name>
|
||||
<url-pattern>/service/*</url-pattern>
|
||||
<url-pattern>/api/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
||||
|
|
|
@ -15,18 +15,8 @@
|
|||
<img class="text-center img-fluid" src="assets/logo.png" alt="FilmFlix logo"/>
|
||||
</div>
|
||||
|
||||
<div class="row row-cols-lg-4 g-3 d-flex justify-content-center text-center mb-3">
|
||||
<div class="col">
|
||||
<div class="border rounded color-border p-2">
|
||||
<h5 class="title">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?</h5>
|
||||
<p class="description-4 mb-0">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!</p>
|
||||
<div class="row row-cols-lg-4 g-3 d-flex justify-content-center text-center mb-3" id="films">
|
||||
|
||||
<div class="d-flex">
|
||||
<span class="me-auto text-muted">Il y a 20 minutes</span>
|
||||
<span class="ms-auto text-muted">1h20min</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
|
|
@ -1,7 +1,27 @@
|
|||
$(document).ready(function () {
|
||||
console.log("OK");
|
||||
|
||||
$.get("https://ziedelth.fr/api/v1/country/fr/page/1/limit/9/episodes", function (data) {
|
||||
// $("#films").append(generateFilmCard("lorem", "lorem"));
|
||||
|
||||
$.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) {
|
||||
return `<div class="col">
|
||||
<div class="border rounded color-border p-2">
|
||||
<h5 class="title">${title}</h5>
|
||||
<p class="description-4 mb-0">${description}</p>
|
||||
|
||||
<div class="d-flex">
|
||||
<span class="me-auto text-muted">Il y a 20 minutes</span>
|
||||
<span class="ms-auto text-muted">1h20min</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
Reference in a new issue