critere de recherche

This commit is contained in:
ionak
2024-04-24 23:19:42 +02:00
parent 6ffe121111
commit 01f120cb2f
5 changed files with 155 additions and 56 deletions

View File

@@ -3,6 +3,8 @@ package fr.eni.enchere.bo;
public class SearchArticleCritere {
String title;
Integer noCategorie;
Integer noVendeur;
String[] venteOptions;
public String getTitle() {
return title;
@@ -19,4 +21,20 @@ public class SearchArticleCritere {
public void setNoCategorie(Integer noCategorie) {
this.noCategorie = noCategorie;
}
public Integer getNoVendeur() {
return noVendeur;
}
public void setNoVendeur(Integer noVendeur) {
this.noVendeur = noVendeur;
}
public String[] getVenteOptions() {
return venteOptions;
}
public void setVenteOptions(String[] venteOptions) {
this.venteOptions = venteOptions;
}
}

View File

@@ -2,12 +2,16 @@ package fr.eni.enchere.controllers;
import fr.eni.enchere.bll.ArticleService;
import fr.eni.enchere.bll.CategorieService;
import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bll.UserServiceImpl;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.SearchArticleCritere;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -17,6 +21,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@@ -27,28 +33,34 @@ public class AccueilController {
private static final Logger logger = LoggerFactory.getLogger(AccueilController.class);
private ArticleService articleService;
private CategorieService categorieService;
private UserService userService;
public AccueilController(ArticleService articleService, CategorieService categorieService) {
public AccueilController(ArticleService articleService, CategorieService categorieService, UserService userService) {
super();
this.categorieService = categorieService;
this.articleService = articleService;
this.userService = userService;
}
@GetMapping({"/", "/accueil"})
public String viewAccueil(@RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, Model model) {
public String viewAccueil(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions) {
model.addAttribute("categories", categorieService.findAllCategories());
System.out.println("" + model.getAttribute("venteOption"));
SearchArticleCritere critere = new SearchArticleCritere();
critere.setNoCategorie(searchCategory);
critere.setTitle(searchTitle);
critere.setNoVendeur(userService.utilisateurByName(userDetails.getUsername()).getId());
critere.setVenteOptions(venteOptions);
model.addAttribute("articles", articleService.searchArticle(critere));
return "accueil";
}
@PostMapping("/accueil")
public String handleSearch(@RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model) {
return viewAccueil(searchTitle, searchCategory, model);
public String handleSearch(@AuthenticationPrincipal UserDetails userDetails, @RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions ) {
return viewAccueil(userDetails, searchTitle, searchCategory, model, venteOptions);
}

View File

@@ -74,7 +74,6 @@ public class ArticleRepositoryImpl implements ArticleRepository {
StringBuilder sql = new StringBuilder("SELECT a.*, u.* FROM ARTICLES_VENDUS a ");
sql.append("JOIN UTILISATEURS u ON a.no_utilisateur = u.no_utilisateur ");
sql.append("WHERE 1 = 1 AND a.isDelete = 0");
List<Object> params = new ArrayList<>();
if (critere.getNoCategorie() != null) {
@@ -86,9 +85,38 @@ public class ArticleRepositoryImpl implements ArticleRepository {
params.add('%' + critere.getTitle() + '%');
}
// Ajouter les conditions supplémentaires basées sur les options de vente
sql.append(" AND ("); // Début du bloc des conditions supplémentaires
boolean isFirstCondition = true; // Pour vérifier la première condition
for (String option : critere.getVenteOptions()) {
if (option.equals("venteEnCours")) {
if (!isFirstCondition) {
sql.append(" OR "); // Ajouter un OR si ce n'est pas la première condition
}
sql.append(" (a.date_debut_encheres <= NOW() AND a.date_fin_encheres >= NOW()) ");
isFirstCondition = false;
}
if (option.equals("ventesNonDebutees")) {
if (!isFirstCondition) {
sql.append(" OR "); // Ajouter un OR si ce n'est pas la première condition
}
sql.append(" (a.date_debut_encheres > NOW()) ");
isFirstCondition = false;
}
if (option.equals("ventesTerminees")) {
if (!isFirstCondition) {
sql.append(" OR "); // Ajouter un OR si ce n'est pas la première condition
}
sql.append(" (a.date_fin_encheres < NOW()) ");
isFirstCondition = false;
}
}
sql.append(")"); // Fin du bloc des conditions supplémentaires
return jdbcTemplate.query(sql.toString(), new ArticleRowMapper(), params.toArray());
}
@Override
public Article findArticleById(int id) {
String sql = "SELECT * FROM ARTICLES_VENDUS a WHERE no_article = ?";