From 413aa031e5f9a3d97f01886629549011266f9c1f Mon Sep 17 00:00:00 2001 From: Parpaillax Date: Fri, 26 Apr 2024 13:51:57 +0200 Subject: [PATCH] better article --- .../controllers/ArticleController.java | 31 +++++++++++-------- .../controllers/EnchereController.java | 12 +++++-- .../enchere/dal/EnchereRepositoryImpl.java | 4 ++- src/main/resources/templates/article.html | 14 +++++++-- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/main/java/fr/eni/enchere/controllers/ArticleController.java b/src/main/java/fr/eni/enchere/controllers/ArticleController.java index 08de78a..f039e90 100644 --- a/src/main/java/fr/eni/enchere/controllers/ArticleController.java +++ b/src/main/java/fr/eni/enchere/controllers/ArticleController.java @@ -1,12 +1,10 @@ package fr.eni.enchere.controllers; import com.fasterxml.jackson.databind.JsonNode; -import fr.eni.enchere.bll.ArticleService; -import fr.eni.enchere.bll.CategorieService; -import fr.eni.enchere.bll.RetraitService; -import fr.eni.enchere.bll.UserService; +import fr.eni.enchere.bll.*; import fr.eni.enchere.bo.*; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -14,6 +12,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; +import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; @@ -25,6 +24,7 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; @Controller() @@ -38,12 +38,14 @@ public class ArticleController { private final UserService userService; private CategorieService categorieService; private RetraitService retraitService; + private EnchereService enchereService; - public ArticleController(ArticleService articleService, UserService userService, CategorieService categorieService, RetraitService retraitService) { + public ArticleController(ArticleService articleService, UserService userService, CategorieService categorieService, RetraitService retraitService, EnchereService enchereService) { this.articleService = articleService; this.userService = userService; this.categorieService = categorieService; this.retraitService = retraitService; + this.enchereService = enchereService; } @GetMapping @@ -54,31 +56,34 @@ public class ArticleController { //Affichage d'un article @GetMapping("/show") - public String showArticle(@RequestParam()int id, Model model) { + public String showArticle(@RequestParam()int id, Model model, HttpSession session) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (!authentication.getName().equals("anonymousUser")) { Article article = articleService.findArticleById(id); UserProfil user = userService.utilisateurById(article.getUtilisateur()); Categorie cate = categorieService.findCategorieById(article.getNumCategorie()); Retrait retrait = retraitService.retraitByNumarticle(article.getId()); + List lastEnchere = this.enchereService.enchereByArticle(article.getId()); + Optional maxMontantEnchere = lastEnchere.stream() + .map(Enchere::getMontantEnchere) // Récupère seulement les montants d'enchère + .max(Float::compareTo); model.addAttribute("article", article); model.addAttribute("username", user); model.addAttribute("cate", cate.getLibelle()); model.addAttribute("retrait", retrait); model.addAttribute("enchere", new Enchere()); + model.addAttribute("maxEnchere", maxMontantEnchere.get()); + List errors = (List) session.getAttribute("errors"); + if (errors != null) { + model.addAttribute("errors", errors); + session.removeAttribute("errors"); // Supprimer les erreurs de la session après utilisation + } return "article"; } else { return "redirect:/accueil"; } } - @GetMapping("/{slug}") - public String testShowArticle(@PathVariable(name = "slug")int id, Model model) { - Article article = articleService.findArticleById(id); - model.addAttribute("article", article); - return "article"; - } - //Création d'un article @GetMapping("/new") diff --git a/src/main/java/fr/eni/enchere/controllers/EnchereController.java b/src/main/java/fr/eni/enchere/controllers/EnchereController.java index 155ee41..48066c4 100644 --- a/src/main/java/fr/eni/enchere/controllers/EnchereController.java +++ b/src/main/java/fr/eni/enchere/controllers/EnchereController.java @@ -5,7 +5,9 @@ import fr.eni.enchere.bll.EnchereService; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.Article; import fr.eni.enchere.bo.Enchere; +import fr.eni.enchere.bo.UserProfil; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -34,7 +36,7 @@ public class EnchereController { } @PostMapping("/incEnchere") - public String incEnchere(@ModelAttribute("enchere") Enchere enchere, @RequestParam("articleId") int articleId, BindingResult result) { + public String incEnchere(@ModelAttribute("enchere") Enchere enchere, @RequestParam("articleId") int articleId, BindingResult result, HttpSession session) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); enchere.setNoArticle(articleId); enchere.setNoUtilisateur(this.userService.utilisateurByName(authentication.getName()).getId()); @@ -54,12 +56,18 @@ public class EnchereController { Optional maxMontantEnchere = lastEnchere.stream() .map(Enchere::getMontantEnchere) // Récupère seulement les montants d'enchère .max(Float::compareTo); - System.out.println(maxMontantEnchere); if (maxMontantEnchere.isPresent() && encherePrice < maxMontantEnchere.get()) { result.rejectValue("montantEnchere", "error.enchere", "Vous ne pouvez pas enchérir un montant inférieur à la dernière enchère"); } + //Empeche l'enchère si les crédits du user sont inférieur au prix initial de l'article + float userCredit = this.userService.utilisateurByName(authentication.getName()).getCredit(); + if (userCredit < encherePrice) { + result.rejectValue("montantEnchere", "error.enchere", "Vous ne pouvez pas enchérir si vous n'avez pas les fonds suffisant"); + } + if (result.hasErrors()) { + session.setAttribute("errors", result.getAllErrors()); return "redirect:/article/show?id=" + articleId; } diff --git a/src/main/java/fr/eni/enchere/dal/EnchereRepositoryImpl.java b/src/main/java/fr/eni/enchere/dal/EnchereRepositoryImpl.java index 54b1b18..b82a99c 100644 --- a/src/main/java/fr/eni/enchere/dal/EnchereRepositoryImpl.java +++ b/src/main/java/fr/eni/enchere/dal/EnchereRepositoryImpl.java @@ -43,7 +43,9 @@ public class EnchereRepositoryImpl implements EnchereRepository { @Override public List findByIdArticle(int idArticle) { - return List.of(); + String sql = "SELECT * FROM ENCHERES WHERE no_article = ?"; + List encheres = jdbcTemplate.query(sql, new EnchereRowMapper(), idArticle); + return encheres; } @Override diff --git a/src/main/resources/templates/article.html b/src/main/resources/templates/article.html index 5487395..48ba0a1 100644 --- a/src/main/resources/templates/article.html +++ b/src/main/resources/templates/article.html @@ -36,6 +36,10 @@ +
+ + +
@@ -48,15 +52,19 @@
- + - +
    -
  • +
+
+ + +