merge
This commit is contained in:
@@ -6,6 +6,7 @@ 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.cglib.core.Local;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
@@ -60,7 +61,7 @@ public class ArticleController {
|
||||
Article article = articleService.findArticleById(id);
|
||||
UserProfil user = userService.utilisateurById(article.getUtilisateur());
|
||||
Categorie cate = categorieService.findCategorieById(article.getNumCategorie());
|
||||
Retrait retrait = retraitService.retraitByNumarticle(article.getId());
|
||||
Retrait retrait = retraitService.findByNumArticle(article.getId());
|
||||
article.setPseudoUtilisateur(user.getPseudo());
|
||||
List<Enchere> lastEnchere = this.enchereService.enchereByArticle(article.getId());
|
||||
|
||||
@@ -86,6 +87,11 @@ public class ArticleController {
|
||||
if (maxMontantEnchere.isPresent()) {
|
||||
model.addAttribute("maxEnchere", maxMontantEnchere.get());
|
||||
}
|
||||
if (article.getId() != 0) {
|
||||
model.addAttribute("imagePath", "/images/articles/" + article.getId() + ".jpg");
|
||||
} else {
|
||||
model.addAttribute("imagePath", "/images/articles/no-data.jpg");
|
||||
}
|
||||
List<ObjectError> errors = (List<ObjectError>) session.getAttribute("errors");
|
||||
if (errors != null) {
|
||||
model.addAttribute("errors", errors);
|
||||
@@ -241,8 +247,7 @@ public class ArticleController {
|
||||
@GetMapping("/edit")
|
||||
public String edit(Model model, @RequestParam()int id) {
|
||||
Article article = this.articleService.findArticleById(id);
|
||||
Retrait retrait = this.retraitService.retraitByNumarticle(id);
|
||||
System.out.println(article.getNom());
|
||||
Retrait retrait = this.retraitService.findByNumArticle(id);
|
||||
|
||||
model.addAttribute("article", article);
|
||||
model.addAttribute("retrait", retrait);
|
||||
@@ -250,4 +255,126 @@ public class ArticleController {
|
||||
return "editArticle";
|
||||
}
|
||||
|
||||
@PostMapping("/edit")
|
||||
public String edit(@ModelAttribute("article") Article article,
|
||||
@ModelAttribute("retrait") Retrait retrait,
|
||||
@RequestParam("dateDebut") String dateDebut,
|
||||
@RequestParam("dateFin") String dateFin,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
//Récupérer l'utilisateur pour set l'article
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
String username = authentication.getName();
|
||||
UserProfil userProfile = userService.utilisateurByName(username);
|
||||
article.setNoUtilisateur(userProfile.getId());
|
||||
//Reste de l'article
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date dDateDebut = null;
|
||||
Date dDateFin = null;
|
||||
try {
|
||||
dDateDebut = format.parse(dateDebut);
|
||||
dDateFin = format.parse(dateFin);
|
||||
} catch (ParseException e) {
|
||||
redirectAttributes.addAttribute("erreur", "La date de début n'est pas valide.");
|
||||
redirectAttributes.addAttribute("erreur", "La date de fin n'est pas valide.");
|
||||
}
|
||||
article.setDateDebutEnch(dDateDebut);
|
||||
article.setDateFinEnch(dDateFin);
|
||||
//Vérification du formulaire
|
||||
//Vérification du nom de l'article
|
||||
String regex = "^[a-zA-Z0-9 ]*$";
|
||||
if (article.getNom().length() < 3){
|
||||
redirectAttributes.addAttribute("erreur", "Le nom de l'article doit contenir au moin 3 caractères.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
if (!Pattern.matches(regex, article.getNom())){
|
||||
redirectAttributes.addAttribute("erreur", "Le nom de l'article ne doit pas contenir de caractère speciaux.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
//Vérification description de l'article
|
||||
if (article.getDesc().length() < 20){
|
||||
redirectAttributes.addAttribute("erreur", "La description de l'article doit contenir au moin 20 caractères.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
//Vérification de la photo
|
||||
if (article.getPhoto() != null && !article.getPhoto().isEmpty()) {
|
||||
if (article.getPhoto().getSize() > 5 * 1024 * 1024) {
|
||||
redirectAttributes.addAttribute("erreur", "La photo ne doit pas faire plus de 5 Mo.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
if (!article.getPhoto().getOriginalFilename().toLowerCase().endsWith(".jpg")) {
|
||||
redirectAttributes.addAttribute("erreur", "L'image doit avoir une extension .jpg.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
}
|
||||
//Vérification du prix initial
|
||||
if (article.getPrixInitial() > 2000000000 && article.getPrixInitial() < 0){
|
||||
redirectAttributes.addAttribute("erreur", "Le prix doit être compris entre 0 et 2 000 000 000 crédits.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
//Vérifier les dates
|
||||
LocalDate dateDebutEnch = LocalDate.parse(dateDebut);
|
||||
LocalDate dateActuelle = LocalDate.now();
|
||||
if (dateDebutEnch.isBefore(dateActuelle)) {
|
||||
redirectAttributes.addAttribute("erreur", "La date de début d'enchère ne peux pas être infèrieur à la date du jour.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
LocalDate dateFinEnch = LocalDate.parse(dateFin);
|
||||
LocalDate datePlusUnJour = LocalDate.now().plusDays(1);
|
||||
if (dateFinEnch.isBefore(datePlusUnJour)) {
|
||||
redirectAttributes.addAttribute("erreur", "La date de début d'enchère ne peux pas être infàrieur à la date du jour + 1.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
|
||||
//Vérification rue
|
||||
if (!Pattern.matches("^[a-zA-Z0-9 ]+$", retrait.getRue())){
|
||||
redirectAttributes.addAttribute("erreur", "Le rue n'est pas valide.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
if (retrait.getRue().isEmpty()){
|
||||
redirectAttributes.addAttribute("erreur", "Entrer une rue.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
//Vérifier code postal et ville
|
||||
if(Pattern.matches("^\\d{5}$", retrait.getCode_postale())){
|
||||
//Récupérer les villes en fonction du code postal
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
List<String> villeCodePostal = new ArrayList<>(); // Initialisez la liste pour éviter les NullPointerException
|
||||
String apiUrl = API_URL + retrait.getCode_postale();
|
||||
ResponseEntity<JsonNode> response = restTemplate.getForEntity(apiUrl, JsonNode.class); // Désérialiser en JsonNode
|
||||
if (response.getStatusCode().is2xxSuccessful()) {
|
||||
JsonNode responseBody = response.getBody();
|
||||
if (responseBody.isArray()) { // Vérifiez si le corps de la réponse est un tableau JSON
|
||||
for (JsonNode node : responseBody) {
|
||||
String cityName = node.get("nomCommune").asText();
|
||||
villeCodePostal.add(cityName);
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "Une erreur est survenue !");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
if (!villeCodePostal.contains(userProfile.getVille())) {
|
||||
String showCity = String.join(", ", villeCodePostal);
|
||||
redirectAttributes.addAttribute("erreur", "Essayer : " + showCity);
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "La ville n'est pas valide.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "Le code postal n'est pas valide.");
|
||||
return "redirect:/article/edit?id="+article.getId();
|
||||
}
|
||||
//Validation du formulaire
|
||||
retrait.setNumArticle(articleService.updateArticle(article));
|
||||
retraitService.updateRetrait(retrait);
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public String delte(@ModelAttribute("article") Article article) {
|
||||
article.setIsDelete(true);
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user