Archived
1
0
Fork 0

Setup Film model, manager and controller

Rest API with GET, POST, PUT, DELETE
This commit is contained in:
Ethanell 2022-03-22 17:40:24 +01:00
parent e01953f0c8
commit 5d6642e696
8 changed files with 290 additions and 12 deletions

9
example.sql Normal file
View 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
View file

@ -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>

View file

@ -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() {

View file

@ -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();
}
}
}

View 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() {
}
}

View 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;
}
}

View file

@ -0,0 +1,29 @@
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name = "hibernate.dialect">
org.hibernate.dialect.MariaDBDialect
</property>
<property name = "hibernate.connection.driver_class">
org.mariadb.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name = "hibernate.connection.url">
jdbc:mariadb://localhost:3306/filmflix
</property>
<property name = "hibernate.connection.username">
filmflix
</property>
<property name = "hibernate.connection.password">
filmflix
</property>
</session-factory>
</hibernate-configuration>

View file

@ -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>