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 = ?";

View File

@@ -8,36 +8,84 @@
<div class="container mt-4">
<div class="row mb-4">
<div class="col-12">
<!-- Formulaire de recherche avec sélection de catégorie -->
<!-- Card principale pour le formulaire -->
<div class="card">
<div class="card-body">
<form th:action="@{/accueil}" method="post" class="row g-3 align-items-center">
<div class="col-md-8">
<!-- Barre de recherche -->
<!-- Formulaire de recherche avec sélection de catégorie -->
<form th:action="@{/accueil}" method="post" class="mb-3">
<div class="row">
<!-- Colonne pour la zone de recherche et Achats/Ventes -->
<div class="col-md-6">
<!-- Zone de recherche -->
<div class="mb-3">
<input type="text" class="form-control" th:attr="placeholder=#{home.search.title}" name="searchTitle">
<select class="form-control mt-2" name="searchCategory">
</div>
<div class="mb-4">
<select class="form-control" name="searchCategory">
<option value="" th:text="#{home.search.cat}"></option>
<!-- Options de catégorie -->
<option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.libelle}"></option>
</select>
</div>
<div class="col-md-4 text-center">
<button type="submit" class="btn btn-primary" th:text="#{home.search.button}">Search</button>
</div>
</form>
<div class="row">
<div class="col-12 col-lg-6">
<div class="card mt-3">
<!-- Achats et Ventes -->
<div class="card">
<div class="card-body">
<form class="row">
<div class="col-6">
<div class="form-check">
<input class="form-check-input" type="radio" name="transactionType" id="achats" value="achats">
<label class="form-check-label" type="chec" >Achats</label>
<div class="row">
<div class="col-md-6">
<h5 class="card-title">Achats</h5>
<div class="mb-3">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="typeTransaction" id="achats" value="achats" onchange="toggleCheckbox(this.value)">
<label class="form-check-label" for="achats">Achats</label>
</div>
</div><div class="col-6">
</div>
<!-- Checkboxes pour Achats -->
<div id="achatsOptions">
<div class="form-check">
<input class="form-check-input" type="radio" name="transactionType" id="ventes" value="ventes">
<label class="form-check-label" for="ventes">Mes ventes</label>
<input class="form-check-input" type="checkbox" name="achatOption" id="encheresOuvertes" th:value="encheresOuvertes" disabled>
<label class="form-check-label" for="encheresOuvertes">Enchères ouvertes</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="achatOption" id="enchereEnCours" th:value="enchereEnCours" disabled>
<label class="form-check-label" for="enchereEnCours">Mes enchères en cours</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="achatOption" id="enchereRemportees" th:value="enchereRemportees" disabled>
<label class="form-check-label" for="enchereRemportees">Mes enchères remportées</label>
</div>
</div>
</div>
<div class="col-md-6">
<h5 class="card-title">Ventes</h5>
<!-- Boutons radio pour Ventes -->
<div class="mb-3">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="typeTransaction" id="ventes" value="ventes" onchange="toggleCheckbox(this.value)">
<label class="form-check-label" for="ventes">Ventes</label>
</div>
</div>
<!-- Checkboxes pour Ventes -->
<div id="ventesOptions">
<div class="form-check">
<input class="form-check-input" type="checkbox" th:name="venteOption" id="venteEnCours" th:value="venteEnCours" disabled>
<label class="form-check-label" for="venteEnCours">Mes ventes en cours</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" th:name="venteOption" id="ventesNonDebutees" th:value="ventesNonDebutees" disabled>
<label class="form-check-label" for="ventesNonDebutees">Ventes non débutées</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" th:name="venteOption" id="ventesTerminees" th:value="ventesTerminees" disabled>
<label class="form-check-label" for="ventesTerminees">Ventes terminées</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Colonne pour le bouton de recherche -->
<div class="col-md-6 d-flex justify-content-center align-items-center">
<button type="submit" style="font-size: 2em;" class="btn btn-primary btn-lg">Recherche</button>
</div>
</div>
</form>
@@ -45,13 +93,8 @@
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div th:each="article : ${articles}" class="col-md-6 mb-4">
<a th:href="@{/article/show(id=${article.id})}">
<div class="card shadow-sm h-100">
<div class="row g-0">
<div class="col-md-4 d-flex align-items-center justify-content-center p-3">
@@ -64,21 +107,18 @@
<div class="d-flex justify-content-between align-items-center">
<div>
<h6>Prix de vente: <span th:text="${article.prixVente}"></span></h6>
<h6>Vendeur: <span th:text="${article.pseudoUtilisateur}"></span> </h6>
</div>
</div>
</br>
<h6 class="text-muted">Fin de l'enchere: <span th:text="${#dates.format(article.dateFinEnch, 'dd/MM/yyyy')}"></span></h6>
<br>
<h6 class="text-muted">Fin de l'enchère: <span th:text="${#dates.format(article.dateFinEnch, 'dd/MM/yyyy')}"></span></h6>
</div>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
</body>
</html>

View File

@@ -102,5 +102,6 @@
</footer>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="/js/bootstrap/bootstrap.min.js"></script>
<script th:src="@{/js/toggleCheckbox.js}"></script>
</body>
</html>