filtre articles
This commit is contained in:
@@ -5,6 +5,7 @@ public class SearchArticleCritere {
|
||||
Integer noCategorie;
|
||||
Integer noVendeur;
|
||||
String[] venteOptions;
|
||||
String[] achatOptions;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
@@ -37,4 +38,12 @@ public class SearchArticleCritere {
|
||||
public void setVenteOptions(String[] venteOptions) {
|
||||
this.venteOptions = venteOptions;
|
||||
}
|
||||
|
||||
public String[] getAchatOptions() {
|
||||
return achatOptions;
|
||||
}
|
||||
|
||||
public void setAchatOptions(String[] achatOptions) {
|
||||
this.achatOptions = achatOptions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,23 +44,24 @@ public class AccueilController {
|
||||
}
|
||||
|
||||
@GetMapping({"/", "/accueil"})
|
||||
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) {
|
||||
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, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {
|
||||
model.addAttribute("categories", categorieService.findAllCategories());
|
||||
System.out.println("" + model.getAttribute("venteOption"));
|
||||
System.out.println("" + Arrays.toString(venteOptions));
|
||||
SearchArticleCritere critere = new SearchArticleCritere();
|
||||
critere.setNoCategorie(searchCategory);
|
||||
critere.setTitle(searchTitle);
|
||||
critere.setNoVendeur(userService.utilisateurByName(userDetails.getUsername()).getId());
|
||||
critere.setVenteOptions(venteOptions);
|
||||
critere.setAchatOptions(venteOptions);
|
||||
model.addAttribute("articles", articleService.searchArticle(critere));
|
||||
return "accueil";
|
||||
}
|
||||
|
||||
@PostMapping("/accueil")
|
||||
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 ) {
|
||||
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, @RequestParam(value = "achatOption", required = false) String[] achatOptions ) {
|
||||
|
||||
|
||||
return viewAccueil(userDetails, searchTitle, searchCategory, model, venteOptions);
|
||||
return viewAccueil(userDetails, searchTitle, searchCategory, model, venteOptions, achatOptions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,6 +56,24 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
}
|
||||
}
|
||||
|
||||
private class HomeArticleRowMapper implements RowMapper<Article> {
|
||||
@Override
|
||||
public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Article article = new Article();
|
||||
article.setId(rs.getInt("a.no_article"));
|
||||
article.setNom(rs.getString("a.nom_article"));
|
||||
article.setDesc(rs.getString("a.description"));
|
||||
article.setDateDebutEnch(rs.getDate("a.date_debut_encheres"));
|
||||
article.setDateFinEnch(rs.getDate("a.date_fin_encheres"));
|
||||
article.setPrixInitial(rs.getFloat("a.prix_initial"));
|
||||
article.setPrixVente(rs.getFloat("a.prix_vente"));
|
||||
article.setNoUtilisateur(rs.getInt("u.no_utilisateur"));
|
||||
article.setPseudoUtilisateur(rs.getString("u.pseudo"));
|
||||
article.setNumCategorie(rs.getInt("a.no_categorie"));
|
||||
return article;
|
||||
}
|
||||
}
|
||||
|
||||
public ArticleRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, UserService userService) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.namedJdbcTemplate = namedJdbcTemplate;
|
||||
@@ -85,37 +103,77 @@ 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
|
||||
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 "); // Ajouter un OR si ce n'est pas la première condition
|
||||
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 "); // Ajouter un OR si ce n'est pas la première condition
|
||||
sql.append(" OR ");
|
||||
}
|
||||
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(" OR ");
|
||||
}
|
||||
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());
|
||||
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(" /* Ajoutez votre condition ici */ ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
if (option.equals("enchereRemportees")) {
|
||||
if (!isFirstCondition) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append(" /* Ajoutez votre condition ici */ ");
|
||||
isFirstCondition = false;
|
||||
}
|
||||
}
|
||||
|
||||
sql.append(") AND a.no_utilisateur = ?");
|
||||
params.add(critere.getNoVendeur());
|
||||
}
|
||||
|
||||
List<Article> articles = jdbcTemplate.query(sql.toString(), new HomeArticleRowMapper(), params.toArray());
|
||||
|
||||
for (Article article : articles) {
|
||||
|
||||
}
|
||||
|
||||
return articles;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Article findArticleById(int id) {
|
||||
|
||||
@@ -71,11 +71,11 @@
|
||||
</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>
|
||||
<label class="form-check-label" for="ventesNonDebutees">Mes 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>
|
||||
<label class="form-check-label" for="ventesTerminees">Mes ventes terminées</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user