Un peu de tout
This commit is contained in:
@@ -2,6 +2,8 @@ package fr.eni.enchere.bll;
|
||||
|
||||
import fr.eni.enchere.bo.Article;
|
||||
import fr.eni.enchere.bo.SearchArticleCritere;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -13,5 +15,5 @@ public interface ArticleService {
|
||||
void deleteArticle(int id);
|
||||
void updateArticle(int id);
|
||||
List<Article> findArticleByTitle(String title);
|
||||
List<Article> searchArticle(SearchArticleCritere critere);
|
||||
Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package fr.eni.enchere.bll;
|
||||
import fr.eni.enchere.bo.Article;
|
||||
import fr.eni.enchere.bo.SearchArticleCritere;
|
||||
import fr.eni.enchere.dal.ArticleRepository;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -45,8 +47,9 @@ public class ArticleServiceImpl implements ArticleService{
|
||||
return articleRepository.findArticleByTitle(title);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Article> searchArticle(SearchArticleCritere critere) {
|
||||
return articleRepository.searchArticle(critere);
|
||||
public Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable) {
|
||||
return articleRepository.searchArticlePageable(critere, pageable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -44,7 +46,7 @@ public class AccueilController {
|
||||
}
|
||||
|
||||
@GetMapping({"/", "/accueil"})
|
||||
public String viewAccueil(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {
|
||||
public String viewAccueil(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6") int size, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {
|
||||
model.addAttribute("categories", categorieService.findAllCategories());
|
||||
model.addAttribute("requestURI", request.getRequestURI());
|
||||
SearchArticleCritere critere = new SearchArticleCritere();
|
||||
@@ -55,15 +57,21 @@ public class AccueilController {
|
||||
}
|
||||
critere.setVenteOptions(venteOptions);
|
||||
critere.setAchatOptions(achatOptions);
|
||||
model.addAttribute("articles", articleService.searchArticle(critere));
|
||||
|
||||
// Pagination
|
||||
Page<Article> articlePage = articleService.searchArticlePageable(critere, PageRequest.of(page, size));
|
||||
model.addAttribute("articles", articlePage.getContent());
|
||||
model.addAttribute("currentPage", page);
|
||||
model.addAttribute("totalPages", articlePage.getTotalPages());
|
||||
|
||||
return "accueil";
|
||||
}
|
||||
|
||||
@PostMapping("/accueil")
|
||||
public String handleSearch(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions ) {
|
||||
public String handleSearch(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6") int size, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {
|
||||
|
||||
|
||||
return viewAccueil(request, userDetails, searchTitle, searchCategory, model, venteOptions, achatOptions);
|
||||
return viewAccueil(request, userDetails, searchTitle, searchCategory, page, size, model, venteOptions, achatOptions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ public class LanguageController {
|
||||
@GetMapping("/change-language")
|
||||
public String changeLanguage(HttpServletRequest request, HttpServletResponse response, Locale locale) {
|
||||
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
|
||||
System.out.println(locale.getLanguage());
|
||||
if (localeResolver != null) {
|
||||
if (locale.getLanguage().equals("en")) {
|
||||
localeResolver.setLocale(request, response, Locale.FRENCH); // Changer la langue en français
|
||||
@@ -31,7 +30,15 @@ public class LanguageController {
|
||||
localeResolver.setLocale(request, response, Locale.ENGLISH); // Changer la langue en anglais
|
||||
}
|
||||
}
|
||||
return "redirect:/";
|
||||
|
||||
String referer = request.getHeader("Referer");
|
||||
|
||||
if (referer != null && !referer.isEmpty()) {
|
||||
return "redirect:" + referer;
|
||||
} else {
|
||||
return "redirect:/";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package fr.eni.enchere.dal;
|
||||
|
||||
import fr.eni.enchere.bo.Article;
|
||||
import fr.eni.enchere.bo.SearchArticleCritere;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ArticleRepository {
|
||||
List<Article> findAllArticle();
|
||||
List<Article> searchArticle(SearchArticleCritere critere);
|
||||
Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable);
|
||||
Article findArticleById(int id);
|
||||
List<Article> findArticleByTitle(String title);
|
||||
int saveArticle(Article article);
|
||||
|
||||
@@ -8,6 +8,9 @@ import fr.eni.enchere.controllers.AccueilController;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
@@ -87,7 +90,7 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Article> searchArticle(SearchArticleCritere critere) {
|
||||
public Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable) {
|
||||
StringBuilder sql = new StringBuilder("SELECT DISTINCT a.*, u.* FROM ARTICLES_VENDUS a ");
|
||||
sql.append("JOIN UTILISATEURS u ON a.no_utilisateur = u.no_utilisateur ");
|
||||
sql.append("LEFT JOIN ENCHERES e ON a.no_article = e.no_article ");
|
||||
@@ -165,8 +168,104 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
sql.append(")");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
// Compte le nombre total d'articles
|
||||
int totalCount = countArticlePageable(critere);
|
||||
|
||||
// Ajoute la pagination à la requête SQL
|
||||
sql.append(" LIMIT ? OFFSET ?");
|
||||
params.add(pageable.getPageSize());
|
||||
params.add(pageable.getOffset());
|
||||
|
||||
// Exécute la requête paginée
|
||||
List<Article> articles = jdbcTemplate.query(sql.toString(), new HomeArticleRowMapper(), params.toArray());
|
||||
|
||||
// Crée une Page<Article> à partir des résultats et des informations de pagination
|
||||
return new PageImpl<>(articles, pageable, totalCount);
|
||||
}
|
||||
|
||||
public int countArticlePageable(SearchArticleCritere critere) {
|
||||
StringBuilder sql = new StringBuilder("SELECT COUNT(a.no_article) FROM ARTICLES_VENDUS a ");
|
||||
sql.append("JOIN UTILISATEURS u ON a.no_utilisateur = u.no_utilisateur ");
|
||||
sql.append("LEFT JOIN ENCHERES e ON a.no_article = e.no_article ");
|
||||
sql.append("WHERE 1 = 1 AND a.isDelete = 0");
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
if (critere.getNoCategorie() != null) {
|
||||
sql.append(" AND a.no_categorie = ?");
|
||||
params.add(critere.getNoCategorie());
|
||||
}
|
||||
if (critere.getTitle() != null && !critere.getTitle().isEmpty()) {
|
||||
sql.append(" AND a.nom_article LIKE ?");
|
||||
params.add('%' + critere.getTitle() + '%');
|
||||
}
|
||||
|
||||
if (critere.getVenteOptions() != null && critere.getVenteOptions().length > 0) {
|
||||
sql.append(" AND (");
|
||||
boolean isFirstCondition = true;
|
||||
for (String option : critere.getVenteOptions()) {
|
||||
if (option.equals("venteEnCours")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("ventesNonDebutees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres > NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("ventesTerminees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_fin_encheres < NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
}
|
||||
sql.append(") AND a.no_utilisateur = ?");
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
|
||||
if (critere.getAchatOptions() != null && critere.getAchatOptions().length > 0) {
|
||||
sql.append(" AND (");
|
||||
boolean isFirstCondition = true;
|
||||
for (String option : critere.getAchatOptions()) {
|
||||
if (option.equals("encheresOuvertes")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("enchereEnCours")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (e.no_utilisateur = ? AND a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
|
||||
isFirstCondition = false;
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
if (option.equals("enchereRemportees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" (e.no_utilisateur = ? AND e.montant_enchere = (SELECT MAX(montant_enchere) FROM ENCHERES WHERE no_article = a.no_article)) ");
|
||||
isFirstCondition = false;
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
}
|
||||
|
||||
sql.append(")");
|
||||
}
|
||||
|
||||
// Exécute la requête paginée
|
||||
int articles = jdbcTemplate.queryForObject(sql.toString(), Integer.class, params.toArray());
|
||||
|
||||
// Retourne le nombre d'articles
|
||||
return articles;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user