better article

This commit is contained in:
Parpaillax
2024-04-26 13:51:57 +02:00
parent b970ad4c30
commit 413aa031e5
4 changed files with 42 additions and 19 deletions

View File

@@ -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<Float> 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;
}