better article
This commit is contained in:
@@ -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<Enchere> lastEnchere = this.enchereService.enchereByArticle(article.getId());
|
||||
Optional<Float> 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<ObjectError> errors = (List<ObjectError>) 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")
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,9 @@ public class EnchereRepositoryImpl implements EnchereRepository {
|
||||
|
||||
@Override
|
||||
public List<Enchere> findByIdArticle(int idArticle) {
|
||||
return List.of();
|
||||
String sql = "SELECT * FROM ENCHERES WHERE no_article = ?";
|
||||
List<Enchere> encheres = jdbcTemplate.query(sql, new EnchereRowMapper(), idArticle);
|
||||
return encheres;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -36,6 +36,10 @@
|
||||
<strong><label class="col-form-label">Prix de vente</label></strong>
|
||||
<span th:text="${article.prixInitial}"></span>
|
||||
</div>
|
||||
<div class="mt-2 d-flex flex-row align-items-end justify-content-between">
|
||||
<strong><label class="col-form-label">Dernière offre</label></strong>
|
||||
<span th:text="${maxEnchere}"></span>
|
||||
</div>
|
||||
<div class="mt-2 d-flex flex-row align-items-end justify-content-between">
|
||||
<strong><label class="col-form-label">Date fin enchère</label></strong>
|
||||
<span th:text="${article.dateFinEnch}"></span>
|
||||
@@ -48,15 +52,19 @@
|
||||
</div>
|
||||
<form th:action="@{/enchere/incEnchere}" method="post" th:object="${enchere}" class="mt-2 d-flex flex-row align-items-end justify-content-between">
|
||||
<input type="hidden" id="articleId" name="articleId" th:value="${article.id}">
|
||||
<label for="montantEnchere">Montant</label>
|
||||
<strong><label for="montantEnchere">Montant</label></strong>
|
||||
<input type="number" th:field="*{montantEnchere}" id="montantEnchere" step="0.01" min="0">
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('montantEnchere')}">
|
||||
<span style="color: red;" th:if="${errors != null}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('montantEnchere')}" th:text="${erreur}"></li>
|
||||
<li th:each="erreur: ${errors[0]}" th:text="${erreur.defaultMessage}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
<button type="submit" class="btn btn-primary">Enchérir</button>
|
||||
</form>
|
||||
<div class="mt-5">
|
||||
<a class="btn btn-secondary" href="/accueil" th:text="'Retour'"></a>
|
||||
<!-- <a th:if="${#strings.equals(${username.pseudo}, ${article.getPseudoUtilisateur()})}" class="btn btn-secondary" href="/accueil" th:text="'Modifier'"></a>-->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user