Compare commits
8 Commits
bd62504d87
...
5f279e3e8a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f279e3e8a | ||
|
|
a7701a59e5 | ||
|
|
0e219efac4 | ||
|
|
02b837a0ea | ||
|
|
e368538fed | ||
|
|
1a5b7b1a35 | ||
|
|
eafd02fc07 | ||
|
|
ad130294fd |
@@ -1,9 +1,12 @@
|
||||
package com.humanbooster;
|
||||
|
||||
import com.humanbooster.dao.AdDao;
|
||||
import com.humanbooster.dao.ArticleDao;
|
||||
import com.humanbooster.dao.UserDao;
|
||||
import com.humanbooster.model.Ad;
|
||||
import com.humanbooster.model.Article;
|
||||
import com.humanbooster.model.User;
|
||||
import com.humanbooster.service.AdService;
|
||||
import com.humanbooster.service.ArticleService;
|
||||
import com.humanbooster.service.UserService;
|
||||
import org.hibernate.SessionFactory;
|
||||
@@ -12,6 +15,9 @@ import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
@@ -29,15 +35,17 @@ public class App {
|
||||
User user = new User("Bob", "bob@example.com", null);
|
||||
|
||||
user.setArticles(List.of(
|
||||
new Article("Article 1", "Contenu de l'article 1", user),
|
||||
new Article("Article 2", "Contenu de l'article 2", user)
|
||||
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)
|
||||
));
|
||||
|
||||
Ad ad = new Ad("Ad 1", "Contenu de l'annonce 1", LocalDate.now(), LocalDate.now().plusDays(7), "contact@example.com", 12);
|
||||
|
||||
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||
AdService adService = new AdService(new AdDao(sessionFactory));
|
||||
|
||||
List<User> existingUsers = userService.getAllUsers();
|
||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
||||
cleanDatabase(userService, articleService, adService);
|
||||
|
||||
userService.createUser(user);
|
||||
|
||||
@@ -45,5 +53,21 @@ public class App {
|
||||
System.out.println("\nArticle trouvé :" + article.toString());
|
||||
}
|
||||
);
|
||||
|
||||
adService.createAd(ad);
|
||||
|
||||
sessionFactory.close();
|
||||
System.out.print("Fin du programme");
|
||||
}
|
||||
|
||||
static void cleanDatabase(UserService userService, ArticleService articleService, AdService adService) {
|
||||
List<User> existingUsers = userService.getAllUsers();
|
||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
||||
|
||||
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,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);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import org.hibernate.SessionFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ArticleDao extends GenericDaoImpl<Article, Long> {
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.humanbooster.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
public class Ad extends Publication {
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDate expirationDate;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String contactEmail;
|
||||
|
||||
private int price;
|
||||
|
||||
public Ad() {
|
||||
}
|
||||
|
||||
public Ad(String title, String content, LocalDate publishDate, LocalDate expirationDate, String contactEmail, int 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 int getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(int price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
@@ -2,53 +2,23 @@ package com.humanbooster.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@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;
|
||||
public class Article extends Publication {
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="author_id", nullable=false)
|
||||
@JoinColumn(name="author_id", nullable = false)
|
||||
private User author;
|
||||
|
||||
private int views = 0;
|
||||
|
||||
public Article() {}
|
||||
|
||||
public Article (String title, String content, User author){
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
public Article (String title, String content, LocalDate publishDate, User author, int views) {
|
||||
super(title, content, publishDate);
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
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;
|
||||
this.views = views;
|
||||
}
|
||||
|
||||
public User getAuthor() {
|
||||
@@ -63,9 +33,9 @@ public class Article {
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("Article ID: ").append(id).append("\n");
|
||||
sb.append("Title: ").append(title).append("\n");
|
||||
sb.append("Content: ").append(content).append("\n");
|
||||
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.TABLE_PER_CLASS)
|
||||
@DiscriminatorColumn
|
||||
public abstract class Publication {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class User {
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy="author", cascade= CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private List<Article> articles;
|
||||
|
||||
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(Ad ad) {
|
||||
adDao.update(ad);
|
||||
}
|
||||
|
||||
public void deleteAd(Long id) {
|
||||
adDao.delete(id);
|
||||
}
|
||||
|
||||
public List<Ad> getAllAds() {
|
||||
return adDao.findAll();
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,7 @@ import com.humanbooster.model.Article;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ArticleService {
|
||||
|
||||
public final ArticleDao articleDao;
|
||||
|
||||
public ArticleService(ArticleDao articleDao) {
|
||||
this.articleDao = articleDao;
|
||||
}
|
||||
public record ArticleService(ArticleDao articleDao) {
|
||||
|
||||
public void createArticle(Article article) {
|
||||
articleDao.create(article);
|
||||
|
||||
@@ -5,13 +5,7 @@ import com.humanbooster.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserService {
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
public UserService(UserDao userDao) {
|
||||
this.userDao = userDao;
|
||||
}
|
||||
public record UserService (UserDao userDao) {
|
||||
|
||||
public void createUser(User user) {
|
||||
userDao.create(user);
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
<property name="jakarta.persistence.jdbc.user">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>
|
||||
|
||||
|
||||
@@ -18,5 +17,6 @@
|
||||
<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>
|
||||
Reference in New Issue
Block a user