Compare commits
33 Commits
8fd72d599d
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58a10fd4b8 | ||
|
|
180e1e1622 | ||
|
|
8bfc801b26 | ||
|
|
dc73603cda | ||
|
|
c1617cbd38 | ||
|
|
a803a9ef22 | ||
|
|
ed83c35f3c | ||
|
|
b63f1ef054 | ||
|
|
8b8fae6e24 | ||
|
|
adbdf0d619 | ||
|
|
0887925477 | ||
|
|
153380f541 | ||
|
|
4cc46d7ac3 | ||
|
|
7fcdee6e2b | ||
|
|
3c330e9800 | ||
|
|
47ee6f7ef1 | ||
|
|
eaba56b92d | ||
|
|
7f3157b7a2 | ||
|
|
5de8594095 | ||
|
|
3da2712bdf | ||
|
|
24eab5f2cc | ||
|
|
5f279e3e8a | ||
|
|
a7701a59e5 | ||
|
|
0e219efac4 | ||
|
|
02b837a0ea | ||
|
|
e368538fed | ||
|
|
1a5b7b1a35 | ||
|
|
eafd02fc07 | ||
|
|
ad130294fd | ||
|
|
bd62504d87 | ||
|
|
14d1264091 | ||
|
|
0f8f09e0d7 | ||
|
|
7c89d637f7 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,6 +27,7 @@ dist/
|
|||||||
build/
|
build/
|
||||||
coverage/
|
coverage/
|
||||||
out/
|
out/
|
||||||
|
target/
|
||||||
|
|
||||||
# Environment and secrets
|
# Environment and secrets
|
||||||
.env
|
.env
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ services:
|
|||||||
MYSQL_DATABASE: testdb
|
MYSQL_DATABASE: testdb
|
||||||
MYSQL_USER: user
|
MYSQL_USER: user
|
||||||
MYSQL_PASSWORD: password
|
MYSQL_PASSWORD: password
|
||||||
# ports:
|
ports:
|
||||||
# - "3306:3306"
|
- "3366:3306"
|
||||||
volumes:
|
volumes:
|
||||||
- mysql_data:/var/lib/mysql
|
- mysql_data:/var/lib/mysql
|
||||||
|
|
||||||
@@ -26,13 +26,5 @@ services:
|
|||||||
DB_USER: user
|
DB_USER: user
|
||||||
DB_PASSWORD: password
|
DB_PASSWORD: password
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mysql_data:
|
mysql_data:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
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>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey</groupId>
|
||||||
|
<artifactId>jersey-bom</artifactId>
|
||||||
|
<version>3.1.5</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
@@ -57,16 +64,73 @@
|
|||||||
<groupId>jakarta.persistence</groupId>
|
<groupId>jakarta.persistence</groupId>
|
||||||
<artifactId>jakarta.persistence-api</artifactId>
|
<artifactId>jakarta.persistence-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.validation</groupId>
|
||||||
|
<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>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
<version>9.3.0</version>
|
<version>9.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains</groupId>
|
<groupId>org.reflections</groupId>
|
||||||
<artifactId>annotations</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>RELEASE</version>
|
<version>0.10.2</version>
|
||||||
<scope>compile</scope>
|
</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>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -1,41 +1,64 @@
|
|||||||
package com.humanbooster;
|
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.dao.UserDao;
|
||||||
|
import com.humanbooster.factory.DataFactory;
|
||||||
|
import com.humanbooster.model.Ad;
|
||||||
import com.humanbooster.model.Article;
|
import com.humanbooster.model.Article;
|
||||||
import com.humanbooster.model.User;
|
import com.humanbooster.model.User;
|
||||||
|
import com.humanbooster.service.AdService;
|
||||||
|
import com.humanbooster.service.ArticleService;
|
||||||
import com.humanbooster.service.UserService;
|
import com.humanbooster.service.UserService;
|
||||||
import org.hibernate.SessionFactory;
|
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.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
public static final boolean LOCAL_ENVIRONMENT = true;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
System.out.println("Démarrage de l'application");
|
System.out.println("Démarrage de l'application");
|
||||||
|
|
||||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
RestClient client = new RestClient();
|
||||||
.configure()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
try {
|
||||||
SessionFactory sessionFactory = metadata.buildSessionFactory();
|
ServerConfig serverConfig = new ServerConfig();
|
||||||
System.out.println("Connexion réussie !");
|
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);
|
SessionFactory sessionFactory = new HibernateConfig().getSessionFactory();
|
||||||
|
|
||||||
user.setArticles(List.of(
|
|
||||||
new Article("Article 1", "Contenu de l'article 1", user),
|
|
||||||
new Article("Article 2", "Contenu de l'article 2", user)
|
|
||||||
));
|
|
||||||
|
|
||||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
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(dataFactory.createUser("Michel", "michel@test.fr"));
|
||||||
|
dataFactory.createAds().forEach(adService::createAd);
|
||||||
|
|
||||||
|
sessionFactory.close();
|
||||||
|
System.out.print("Fin du programme");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cleanDatabase(UserService userService, ArticleService articleService, AdService adService) {
|
||||||
List<User> existingUsers = userService.getAllUsers();
|
List<User> existingUsers = userService.getAllUsers();
|
||||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
||||||
|
|
||||||
userService.createUser(user);
|
List<Article> existingArticles = articleService.getAllArticles();
|
||||||
|
existingArticles.forEach(a -> articleService.deleteArticle(a.getId()));
|
||||||
|
|
||||||
|
List<Ad> existingAds = adService.getAllAds();
|
||||||
|
existingAds.forEach(a -> adService.deleteAd(a.getId()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +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 {
|
||||||
|
|
||||||
|
public SessionFactory getSessionFactory() {
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||||
|
.configure()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Metadata metadata = new MetadataSources(registry).buildMetadata();
|
||||||
|
return sessionFactory = metadata.buildSessionFactory();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.humanbooster.dao;
|
||||||
|
|
||||||
|
import com.humanbooster.model.Ad;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
|
public class AdDao extends GenericDaoImpl<Ad, Long> {
|
||||||
|
|
||||||
|
public AdDao(SessionFactory sessionFactory) {
|
||||||
|
super(sessionFactory, Ad.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,24 @@
|
|||||||
package com.humanbooster.dao;
|
package com.humanbooster.dao;
|
||||||
|
|
||||||
import com.humanbooster.model.Article;
|
import com.humanbooster.model.Article;
|
||||||
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
|
import jakarta.persistence.criteria.Predicate;
|
||||||
|
import jakarta.persistence.criteria.Root;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
public class ArticleDao extends GenericDaoImpl<Article,Long> {
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ArticleDao extends GenericDaoImpl<Article, Long> {
|
||||||
|
|
||||||
public ArticleDao(SessionFactory sessionFactory) {
|
public ArticleDao(SessionFactory sessionFactory) {
|
||||||
super(sessionFactory, Article.class);
|
super(sessionFactory, Article.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Article findByAuthor(String author) {
|
public Article findByAuthor(String author) {
|
||||||
try(Session session = sessionFactory.openSession()){
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
Article article = session.createQuery("FROM Article WHERE author = :author", Article.class)
|
Article article = session.createQuery("FROM Article WHERE author = :author", Article.class)
|
||||||
.setParameter("author", author)
|
.setParameter("author", author)
|
||||||
@@ -22,7 +29,7 @@ public class ArticleDao extends GenericDaoImpl<Article,Long> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Article findByTitle(String title) {
|
public Article findByTitle(String title) {
|
||||||
try(Session session = sessionFactory.openSession()){
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
Article article = session.createQuery("FROM Article WHERE title = :title", Article.class)
|
Article article = session.createQuery("FROM Article WHERE title = :title", Article.class)
|
||||||
.setParameter("title", title)
|
.setParameter("title", title)
|
||||||
@@ -31,4 +38,37 @@ public class ArticleDao extends GenericDaoImpl<Article,Long> {
|
|||||||
return article;
|
return article;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Article> findByCriteria(String keyword, Long authorId, int page, int size) {
|
||||||
|
try (Session session = sessionFactory.openSession()) {
|
||||||
|
|
||||||
|
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Article> query = cb.createQuery(Article.class);
|
||||||
|
|
||||||
|
Root<Article> root = query.from(Article.class);
|
||||||
|
query.select(root);
|
||||||
|
|
||||||
|
List<Predicate> predicates = new ArrayList<>();
|
||||||
|
|
||||||
|
if (keyword != null && !keyword.isEmpty()) {
|
||||||
|
predicates.add(
|
||||||
|
cb.like(root.get("title"), "%" + keyword + "%")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authorId != null) {
|
||||||
|
predicates.add(
|
||||||
|
cb.equal(root.get("author").get("id"), authorId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
query.where(predicates.toArray(Predicate[]::new));
|
||||||
|
query.orderBy(cb.asc(root.get("title")));
|
||||||
|
|
||||||
|
return session.createQuery(query)
|
||||||
|
.setFirstResult(page * size)
|
||||||
|
.setMaxResults(size)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import java.util.List;
|
|||||||
public interface GenericDao<T, ID> {
|
public interface GenericDao<T, ID> {
|
||||||
void create(T entity);
|
void create(T entity);
|
||||||
T read(ID id);
|
T read(ID id);
|
||||||
void update(T entity);
|
void update(ID id);
|
||||||
void delete(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
|
@Override
|
||||||
public void update(T entity) {
|
public void update(ID id) {
|
||||||
try (Session session = sessionFactory.openSession()) {
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
T entity = session.get(entityClass, id);
|
||||||
session.merge(entity);
|
session.merge(entity);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
@@ -47,14 +48,14 @@ public abstract class GenericDaoImpl<T, ID> implements GenericDao<T, ID> {
|
|||||||
public void delete(ID id) {
|
public void delete(ID id) {
|
||||||
try (Session session = sessionFactory.openSession()) {
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
T entity = session.get(entityClass, id);
|
T entity = session.find(entityClass, id);
|
||||||
if (entity != null) session.remove(entity);
|
if (entity != null) session.remove(entity);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<T> findAll() {
|
public List<T> getAll() {
|
||||||
try (Session session = sessionFactory.openSession()) {
|
try (Session session = sessionFactory.openSession()) {
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
List<T> entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
|
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)))
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Ad extends Publication {
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private LocalDate expirationDate;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
@Email
|
||||||
|
private String contactEmail;
|
||||||
|
|
||||||
|
private BigDecimal price;
|
||||||
|
|
||||||
|
public Ad() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ad(String title, String content, LocalDate publishDate, LocalDate expirationDate, String contactEmail, BigDecimal price) {
|
||||||
|
super(title, content, publishDate);
|
||||||
|
this.expirationDate = expirationDate;
|
||||||
|
this.contactEmail = contactEmail;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getExpirationDate() {
|
||||||
|
return expirationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpirationDate(LocalDate expirationDate) {
|
||||||
|
this.expirationDate = expirationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContactEmail() {
|
||||||
|
return contactEmail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContactEmail(String contactEmail) {
|
||||||
|
this.contactEmail = contactEmail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(BigDecimal price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,55 +1,26 @@
|
|||||||
package com.humanbooster.model;
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Article {
|
public class Article extends Publication {
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(name = "title", nullable = false, length = 100)
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
@Column(name = "content", nullable = false)
|
|
||||||
private String content;
|
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="author_id", nullable=false)
|
@JoinColumn(name="author_id", nullable = false)
|
||||||
|
@JsonBackReference
|
||||||
private User author;
|
private User author;
|
||||||
|
|
||||||
|
private int views = 0;
|
||||||
|
|
||||||
public Article() {}
|
public Article() {}
|
||||||
|
|
||||||
public Article (String title, String content, User author){
|
public Article (String title, String content, LocalDate publishDate, User author, int views) {
|
||||||
this.title = title;
|
super(title, content, publishDate);
|
||||||
this.content = content;
|
|
||||||
this.author = author;
|
this.author = author;
|
||||||
}
|
this.views = views;
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContent() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContent(String content) {
|
|
||||||
this.content = content;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getAuthor() {
|
public User getAuthor() {
|
||||||
@@ -59,4 +30,16 @@ public class Article {
|
|||||||
public void setAuthor(User author) {
|
public void setAuthor(User author) {
|
||||||
this.author = author;
|
this.author = author;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.append("Article ID: ").append(super.getId()).append("\n");
|
||||||
|
sb.append("Title: ").append(super.getTitle()).append("\n");
|
||||||
|
sb.append("Content: ").append(super.getContent()).append("\n");
|
||||||
|
sb.append("Author: ").append(author != null ? author.getName() : "Unknown").append("\n");
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
|
@DiscriminatorColumn
|
||||||
|
public abstract class Publication {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false, length = 200)
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private LocalDate publishDate;
|
||||||
|
|
||||||
|
public Publication() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Publication(String title, String content, LocalDate publishDate) {
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getPublishDate() {
|
||||||
|
return publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublishDate(LocalDate publishDate) {
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.humanbooster.model;
|
package com.humanbooster.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -11,10 +13,14 @@ public class User {
|
|||||||
@GeneratedValue (strategy = GenerationType.IDENTITY)
|
@GeneratedValue (strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@OneToMany(mappedBy="author", cascade= CascadeType.ALL, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
|
||||||
|
@JsonManagedReference
|
||||||
private List<Article> articles;
|
private List<Article> articles;
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.humanbooster.service;
|
||||||
|
|
||||||
|
import com.humanbooster.dao.AdDao;
|
||||||
|
import com.humanbooster.model.Ad;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record AdService (AdDao adDao) {
|
||||||
|
|
||||||
|
public void createAd(Ad ad) {
|
||||||
|
adDao.create(ad);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ad getAdById(Long id) {
|
||||||
|
return adDao.read(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateAd(Long id) {
|
||||||
|
adDao.update(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAd(Long id) {
|
||||||
|
adDao.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ad> getAllAds() {
|
||||||
|
return adDao.getAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,13 +5,7 @@ import com.humanbooster.model.Article;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ArticleService {
|
public record ArticleService(ArticleDao articleDao) {
|
||||||
|
|
||||||
public final ArticleDao articleDao;
|
|
||||||
|
|
||||||
public ArticleService(ArticleDao articleDao) {
|
|
||||||
this.articleDao = articleDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createArticle(Article article) {
|
public void createArticle(Article article) {
|
||||||
articleDao.create(article);
|
articleDao.create(article);
|
||||||
@@ -21,8 +15,8 @@ public class ArticleService {
|
|||||||
articleDao.read(id);
|
articleDao.read(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateArticle(Article article) {
|
public void updateArticle(Long id) {
|
||||||
articleDao.update(article);
|
articleDao.update(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteArticle(Long id) {
|
public void deleteArticle(Long id) {
|
||||||
@@ -30,7 +24,7 @@ public class ArticleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Article> getAllArticles() {
|
public List<Article> getAllArticles() {
|
||||||
return articleDao.findAll();
|
return articleDao.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Article findArticleByAuthor(String author) {
|
public Article findArticleByAuthor(String author) {
|
||||||
@@ -40,4 +34,8 @@ public class ArticleService {
|
|||||||
public Article findArticleByTitle(String title) {
|
public Article findArticleByTitle(String title) {
|
||||||
return articleDao.findByTitle(title);
|
return articleDao.findByTitle(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Article> findArticlesByCriteria(String keyword, Long authorId, int page, int size) {
|
||||||
|
return articleDao.findByCriteria(keyword, authorId, page, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,7 @@ import com.humanbooster.model.User;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class UserService {
|
public record UserService (UserDao userDao) {
|
||||||
|
|
||||||
private final UserDao userDao;
|
|
||||||
|
|
||||||
public UserService(UserDao userDao) {
|
|
||||||
this.userDao = userDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createUser(User user) {
|
public void createUser(User user) {
|
||||||
userDao.create(user);
|
userDao.create(user);
|
||||||
@@ -21,8 +15,8 @@ public class UserService {
|
|||||||
userDao.read(id);
|
userDao.read(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateUser(User user) {
|
public void updateUser(Long id) {
|
||||||
userDao.update(user);
|
userDao.update(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(Long id) {
|
public void deleteUser(Long id) {
|
||||||
@@ -30,7 +24,7 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<User> getAllUsers() {
|
public List<User> getAllUsers() {
|
||||||
return userDao.findAll();
|
return userDao.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findUserByEmail(String email) {
|
public User findUserByEmail(String email) {
|
||||||
|
|||||||
@@ -10,13 +10,13 @@
|
|||||||
<property name="jakarta.persistence.jdbc.user">root</property>
|
<property name="jakarta.persistence.jdbc.user">root</property>
|
||||||
<property name="jakarta.persistence.jdbc.password">root</property>
|
<property name="jakarta.persistence.jdbc.password">root</property>
|
||||||
|
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
|
||||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||||
|
|
||||||
|
<property name="hibernate.show_sql">false</property>
|
||||||
<property name="hibernate.show_sql">true</property>
|
|
||||||
<property name="hibernate.format_sql">true</property>
|
<property name="hibernate.format_sql">true</property>
|
||||||
|
|
||||||
<mapping class="com.humanbooster.model.User"/>
|
<mapping class="com.humanbooster.model.User"/>
|
||||||
<mapping class="com.humanbooster.model.Article"/>
|
<mapping class="com.humanbooster.model.Article"/>
|
||||||
|
<mapping class="com.humanbooster.model.Ad"/>
|
||||||
</session-factory>
|
</session-factory>
|
||||||
</hibernate-configuration>
|
</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