Add Ad model integration in App and implement database cleanup method
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package com.humanbooster;
|
package com.humanbooster;
|
||||||
|
|
||||||
|
import com.humanbooster.dao.AdDao;
|
||||||
import com.humanbooster.dao.ArticleDao;
|
import com.humanbooster.dao.ArticleDao;
|
||||||
import com.humanbooster.dao.UserDao;
|
import com.humanbooster.dao.UserDao;
|
||||||
|
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.ArticleService;
|
||||||
import com.humanbooster.service.UserService;
|
import com.humanbooster.service.UserService;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
@@ -12,6 +15,9 @@ import org.hibernate.boot.MetadataSources;
|
|||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
@@ -29,15 +35,17 @@ public class App {
|
|||||||
User user = new User("Bob", "bob@example.com", null);
|
User user = new User("Bob", "bob@example.com", null);
|
||||||
|
|
||||||
user.setArticles(List.of(
|
user.setArticles(List.of(
|
||||||
new Article("Article 1", "Contenu de l'article 1", user),
|
new Article("Article 1", "Contenu de l'article 1", LocalDate.now(), user, 0),
|
||||||
new Article("Article 2", "Contenu de l'article 2", user)
|
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));
|
UserService userService = new UserService(new UserDao(sessionFactory));
|
||||||
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
ArticleService articleService = new ArticleService(new ArticleDao(sessionFactory));
|
||||||
|
AdService adService = new AdService(new AdDao(sessionFactory));
|
||||||
|
|
||||||
List<User> existingUsers = userService.getAllUsers();
|
cleanDatabase(userService, articleService, adService);
|
||||||
existingUsers.forEach(u -> userService.deleteUser(u.getId()));
|
|
||||||
|
|
||||||
userService.createUser(user);
|
userService.createUser(user);
|
||||||
|
|
||||||
@@ -45,5 +53,21 @@ public class App {
|
|||||||
System.out.println("\nArticle trouvé :" + article.toString());
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user