Compare commits
14 Commits
3c330e9800
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58a10fd4b8 | ||
|
|
180e1e1622 | ||
|
|
8bfc801b26 | ||
|
|
dc73603cda | ||
|
|
c1617cbd38 | ||
|
|
a803a9ef22 | ||
|
|
ed83c35f3c | ||
|
|
b63f1ef054 | ||
|
|
8b8fae6e24 | ||
|
|
adbdf0d619 | ||
|
|
0887925477 | ||
|
|
153380f541 | ||
|
|
4cc46d7ac3 | ||
|
|
7fcdee6e2b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,6 +27,7 @@ dist/
|
||||
build/
|
||||
coverage/
|
||||
out/
|
||||
target/
|
||||
|
||||
# Environment and secrets
|
||||
.env
|
||||
|
||||
148
hibernate-project/dependency-reduced-pom.xml
Normal file
148
hibernate-project/dependency-reduced-pom.xml
Normal file
@@ -0,0 +1,148 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.humanbooster</groupId>
|
||||
<artifactId>hibernate-project</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.4.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.humanbooster.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.12.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.6.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer>
|
||||
<mainClass>com.humanbooster.App</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>opentest4j</artifactId>
|
||||
<groupId>org.opentest4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>apiguardian-api</artifactId>
|
||||
<groupId>org.apiguardian</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>apiguardian-api</artifactId>
|
||||
<groupId>org.apiguardian</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>6.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-platform</artifactId>
|
||||
<version>6.6.13.Final</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey</groupId>
|
||||
<artifactId>jersey-bom</artifactId>
|
||||
<version>3.1.5</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<properties>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -30,6 +30,13 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey</groupId>
|
||||
<artifactId>jersey-bom</artifactId>
|
||||
<version>3.1.5</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -62,6 +69,11 @@
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>2.0.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
@@ -73,6 +85,54 @@
|
||||
<version>0.10.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JAX-RS (Jersey) -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.core</groupId>
|
||||
<artifactId>jersey-server</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.inject</groupId>
|
||||
<artifactId>jersey-hk2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-json-jackson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- H2 Database (pour tests, sinon MySQL/PostgreSQL) -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>2.2.224</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- Servlet API -->
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>6.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-server</artifactId>
|
||||
<version>11.0.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-servlet</artifactId>
|
||||
<version>11.0.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.17.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
<artifactId>jersey-container-servlet</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.humanbooster;
|
||||
|
||||
import com.humanbooster.client.RestClient;
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.config.ServerConfig;
|
||||
import com.humanbooster.dao.AdDao;
|
||||
import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.factory.DataFactory;
|
||||
import com.humanbooster.model.Ad;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
@@ -11,73 +14,38 @@ import com.humanbooster.service.AdService;
|
||||
import com.humanbooster.service.ArticleService;
|
||||
import com.humanbooster.service.UserService;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
|
||||
public static final boolean LOCAL_ENVIRONMENT = true;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("Démarrage de l'application");
|
||||
|
||||
SessionFactory sessionFactory;
|
||||
RestClient client = new RestClient();
|
||||
|
||||
if (HibernateConfig.LOCAL) sessionFactory = HibernateConfig.getSessionFactory();
|
||||
|
||||
else {
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure()
|
||||
.build();
|
||||
|
||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||
sessionFactory = metadata.buildSessionFactory();
|
||||
try {
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
serverConfig.startServer();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Erreur lors du démarrage du serveur : " + e.getMessage());
|
||||
}
|
||||
|
||||
User user = new User("Bob", "bob@example.com", null);
|
||||
|
||||
user.setArticles(List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", LocalDate.now(), user, 0),
|
||||
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
||||
));
|
||||
|
||||
List<Ad> ads = List.of(
|
||||
(new Ad(
|
||||
"Ad 1",
|
||||
"Contenu de l'annonce 1",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(7),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(12))),
|
||||
(new Ad(
|
||||
"Ad 2",
|
||||
"Contenu de l'annonce 2",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(10),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(6.7)))
|
||||
|
||||
);
|
||||
SessionFactory sessionFactory = new HibernateConfig().getSessionFactory();
|
||||
|
||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||
AdService adService = new AdService(new AdDao(sessionFactory));
|
||||
|
||||
DataFactory dataFactory = new DataFactory();
|
||||
|
||||
cleanDatabase(userService, articleService, adService);
|
||||
|
||||
userService.createUser(user);
|
||||
|
||||
articleService.findArticlesByCriteria("test", 3L, 1, 1).forEach(article -> {
|
||||
System.out.println("\nArticle trouvé :" + article.toString());
|
||||
}
|
||||
);
|
||||
|
||||
ads.forEach(adService::createAd);
|
||||
userService.createUser(dataFactory.createUser("Michel", "michel@test.fr"));
|
||||
dataFactory.createAds().forEach(adService::createAd);
|
||||
|
||||
sessionFactory.close();
|
||||
System.out.print("Fin du programme");
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.humanbooster.annotation;
|
||||
|
||||
public @interface Pair {
|
||||
|
||||
String message() default "Le nombre doit être pair";
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.humanbooster.annotation;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class PairValidator implements ConstraintValidator<Pair, Integer> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return (integer != null) && (integer % 2 == 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.humanbooster.client;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class RestClient {
|
||||
|
||||
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
public String sendGetRequest(String url, String method, String body) {
|
||||
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("Content-Type", "application/json");
|
||||
|
||||
switch (method.toUpperCase()) {
|
||||
case "GET":
|
||||
builder.GET();
|
||||
break;
|
||||
case "POST":
|
||||
builder.POST(HttpRequest.BodyPublishers.ofString(body));
|
||||
break;
|
||||
case "PUT":
|
||||
builder.PUT(HttpRequest.BodyPublishers.ofString(body));
|
||||
break;
|
||||
case "DELETE":
|
||||
builder.DELETE();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid HTTP method: " + method);
|
||||
}
|
||||
|
||||
HttpRequest request = builder.build();
|
||||
|
||||
HttpResponse<String> response;
|
||||
|
||||
try {
|
||||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return parseResponse(response.body());
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while sending the request: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String parseResponse(String response) {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
JsonNode jsonNode = mapper.readTree(response);
|
||||
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while parsing the response: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,48 @@
|
||||
package com.humanbooster.config;
|
||||
|
||||
import com.humanbooster.App;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
public class HibernateConfig {
|
||||
private static final SessionFactory sessionFactory;
|
||||
public static final boolean LOCAL = true;
|
||||
private static String url;
|
||||
private static String username;
|
||||
private static String password;
|
||||
|
||||
public SessionFactory getSessionFactory() {
|
||||
|
||||
static {
|
||||
SessionFactory sessionFactory;
|
||||
|
||||
if (App.LOCAL_ENVIRONMENT) {
|
||||
|
||||
Configuration config = new Configuration()
|
||||
.setProperty("hibernate.connection.url", "jdbc:mysql://127.0.0.1:3306/testdb")
|
||||
.setProperty("hibernate.connection.username", "admin")
|
||||
.setProperty("hibernate.connection.password", "admin")
|
||||
.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||
.setProperty("hibernate.show_sql", "false")
|
||||
.setProperty("hibernate.format_sql", "true");
|
||||
|
||||
Reflections reflections = new Reflections("com.humanbooster.model");
|
||||
for (Class<?> clazz : reflections.getTypesAnnotatedWith(Entity.class)) {
|
||||
config.addAnnotatedClass(clazz);
|
||||
}
|
||||
|
||||
return sessionFactory = config.buildSessionFactory();
|
||||
|
||||
if (LOCAL) {
|
||||
url = "jdbc:mysql://127.0.0.1:3306/testdb";
|
||||
username = "admin";
|
||||
password = "admin";
|
||||
} else {
|
||||
url = "jdbc:mysql://mysql:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true";
|
||||
username = "root";
|
||||
password = "root";
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure()
|
||||
.build();
|
||||
|
||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||
return sessionFactory = metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
Configuration config = new Configuration()
|
||||
.setProperty("hibernate.connection.url", url)
|
||||
.setProperty("hibernate.connection.username", username)
|
||||
.setProperty("hibernate.connection.password", password)
|
||||
.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||
.setProperty("hibernate.show_sql", "false")
|
||||
.setProperty("hibernate.format_sql", "true");
|
||||
|
||||
Reflections reflections = new Reflections("com.humanbooster.model");
|
||||
for (Class<?> clazz : reflections.getTypesAnnotatedWith(Entity.class)) {
|
||||
config.addAnnotatedClass(clazz);
|
||||
}
|
||||
|
||||
sessionFactory = config.buildSessionFactory();
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.humanbooster.config;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.glassfish.jersey.jackson.JacksonFeature;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.servlet.ServletContainer;
|
||||
|
||||
public class ServerConfig extends ResourceConfig {
|
||||
|
||||
public ServerConfig() {
|
||||
packages("com.humanbooster");
|
||||
register(JacksonFeature.class);
|
||||
}
|
||||
|
||||
public void startServer() throws Exception {
|
||||
System.out.println("Lancement du serveur...");
|
||||
|
||||
ResourceConfig config = this;
|
||||
|
||||
ServletHolder servlet = new ServletHolder(new ServletContainer(config));
|
||||
Server server = new Server(80);
|
||||
|
||||
ServletContextHandler context = new ServletContextHandler(server, "/");
|
||||
context.setServer(server);
|
||||
context.addServlet(servlet, "/*");
|
||||
|
||||
try {
|
||||
server.start();
|
||||
System.out.println("Serveur démarré sur le port 80");
|
||||
server.join();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Echec lors du lancement du serveur: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.humanbooster.controller;
|
||||
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.dao.GenericDao;
|
||||
import com.humanbooster.model.Article;
|
||||
import jakarta.ws.rs.Path;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
@Path("/articles")
|
||||
public class ArticleController extends GenericControllerImpl<Article, Long> {
|
||||
|
||||
public ArticleController() {
|
||||
this(new HibernateConfig().getSessionFactory(), new ArticleDao(new HibernateConfig().getSessionFactory()));
|
||||
}
|
||||
|
||||
public ArticleController(SessionFactory sessionFactory, GenericDao<Article, Long> dao) {
|
||||
super(sessionFactory, dao);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.humanbooster.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GenericController<T, ID> {
|
||||
void create(T entity);
|
||||
T read(ID id);
|
||||
void update(ID id);
|
||||
void delete(ID id);
|
||||
List<T> getAll();
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.humanbooster.controller;
|
||||
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.dao.GenericDao;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public abstract class GenericControllerImpl<T, ID> implements GenericController<T, ID>, GenericDao<T, ID> {
|
||||
|
||||
protected final SessionFactory sessionFactory;
|
||||
private final GenericDao<T, ID> dao;
|
||||
|
||||
public GenericControllerImpl(SessionFactory sessionFactory, GenericDao<T, ID> dao) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Override
|
||||
public void create(T entity) {
|
||||
dao.create(entity);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
@Override
|
||||
public T read(@PathParam("id") ID id) {
|
||||
return dao.read(id);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
@Override
|
||||
public void update(@PathParam("id") ID id) {
|
||||
dao.update(id);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
@Override
|
||||
public void delete(@PathParam("id") ID id) {
|
||||
dao.delete(id);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Override
|
||||
public List<T> getAll() {
|
||||
return dao.getAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.humanbooster.controller;
|
||||
|
||||
import com.humanbooster.config.HibernateConfig;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.User;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
|
||||
@Path("/users")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class UserController extends GenericControllerImpl<User, Long> {
|
||||
|
||||
public UserController() {
|
||||
this(new HibernateConfig().getSessionFactory(), new UserDao(new HibernateConfig().getSessionFactory()));
|
||||
}
|
||||
|
||||
public UserController(SessionFactory sessionFactory, UserDao userDao) {
|
||||
super(sessionFactory, userDao);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import java.util.List;
|
||||
public interface GenericDao<T, ID> {
|
||||
void create(T entity);
|
||||
T read(ID id);
|
||||
void update(T entity);
|
||||
void update(ID id);
|
||||
void delete(ID id);
|
||||
List<T> findAll();
|
||||
List<T> getAll();
|
||||
}
|
||||
|
||||
@@ -35,9 +35,10 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(T entity) {
|
||||
public void update(ID id) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
T entity = session.get(entityClass, id);
|
||||
session.merge(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
@@ -47,14 +48,14 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
||||
public void delete(ID id) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
T entity = session.get(entityClass, id);
|
||||
T entity = session.find(entityClass, id);
|
||||
if (entity != null) session.remove(entity);
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
public List<T> getAll() {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.humanbooster.factory;
|
||||
|
||||
import com.humanbooster.model.Ad;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public class DataFactory {
|
||||
|
||||
public User createUser(String name, String email) {
|
||||
User user = new User(name, email, null);
|
||||
|
||||
user.setArticles(List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", LocalDate.now(), user, 0),
|
||||
new Article("Article 2", "Contenu de l'article 2", LocalDate.now(), user, 0)
|
||||
));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public List<Ad> createAds() {
|
||||
return List.of(
|
||||
(new Ad(
|
||||
"Ad 1",
|
||||
"Contenu de l'annonce 1",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(7),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(12))),
|
||||
(new Ad(
|
||||
"Ad 2",
|
||||
"Contenu de l'annonce 2",
|
||||
LocalDate.now(),
|
||||
LocalDate.now().plusDays(10),
|
||||
"contact@example.com",
|
||||
BigDecimal.valueOf(6.7)))
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -9,6 +10,7 @@ public class Article extends Publication {
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="author_id", nullable = false)
|
||||
@JsonBackReference
|
||||
private User author;
|
||||
|
||||
private int views = 0;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,10 +13,14 @@ public class User {
|
||||
@GeneratedValue (strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||
@JsonManagedReference
|
||||
private List<Article> articles;
|
||||
|
||||
public User() {}
|
||||
|
||||
@@ -15,8 +15,8 @@ public record AdService (AdDao adDao) {
|
||||
return adDao.read(id);
|
||||
}
|
||||
|
||||
public void updateAd(Ad ad) {
|
||||
adDao.update(ad);
|
||||
public void updateAd(Long id) {
|
||||
adDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteAd(Long id) {
|
||||
@@ -24,6 +24,6 @@ public record AdService (AdDao adDao) {
|
||||
}
|
||||
|
||||
public List<Ad> getAllAds() {
|
||||
return adDao.findAll();
|
||||
return adDao.getAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ public record ArticleService(ArticleDao articleDao) {
|
||||
articleDao.read(id);
|
||||
}
|
||||
|
||||
public void updateArticle(Article article) {
|
||||
articleDao.update(article);
|
||||
public void updateArticle(Long id) {
|
||||
articleDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteArticle(Long id) {
|
||||
@@ -24,7 +24,7 @@ public record ArticleService(ArticleDao articleDao) {
|
||||
}
|
||||
|
||||
public List<Article> getAllArticles() {
|
||||
return articleDao.findAll();
|
||||
return articleDao.getAll();
|
||||
}
|
||||
|
||||
public Article findArticleByAuthor(String author) {
|
||||
|
||||
@@ -15,8 +15,8 @@ public record UserService (UserDao userDao) {
|
||||
userDao.read(id);
|
||||
}
|
||||
|
||||
public void updateUser(User user) {
|
||||
userDao.update(user);
|
||||
public void updateUser(Long id) {
|
||||
userDao.update(id);
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
@@ -24,7 +24,7 @@ public record UserService (UserDao userDao) {
|
||||
}
|
||||
|
||||
public List<User> getAllUsers() {
|
||||
return userDao.findAll();
|
||||
return userDao.getAll();
|
||||
}
|
||||
|
||||
public User findUserByEmail(String email) {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="jakarta.persistence.jdbc.driver">com.mysql.cj.jdbc.Driver</property>
|
||||
<property name="jakarta.persistence.jdbc.url">jdbc:mysql://mysql:3306/testdb?useSSL=false&allowPublicKeyRetrieval=true</property>
|
||||
<property name="jakarta.persistence.jdbc.user">root</property>
|
||||
<property name="jakarta.persistence.jdbc.password">root</property>
|
||||
|
||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||
|
||||
|
||||
<property name="hibernate.show_sql">false</property>
|
||||
<property name="hibernate.format_sql">true</property>
|
||||
<mapping class="com.humanbooster.model.User"/>
|
||||
<mapping class="com.humanbooster.model.Article"/>
|
||||
<mapping class="com.humanbooster.model.Ad"/>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
14
hibernate-project/test.http
Normal file
14
hibernate-project/test.http
Normal file
@@ -0,0 +1,14 @@
|
||||
### GET request to example server
|
||||
GET http://localhost/users
|
||||
|
||||
###
|
||||
POST http://localhost/users/
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "John Doe",
|
||||
"email": "john.doe@example.com"
|
||||
}
|
||||
|
||||
###
|
||||
DELETE http://localhost/articles/13
|
||||
Reference in New Issue
Block a user