Merge branch 'Johan'
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -10,21 +11,31 @@ import fr.eni.enchere.bo.Retrait;
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
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.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Controller()
|
||||
@RequestMapping("/article")
|
||||
public class ArticleController {
|
||||
|
||||
private final String API_URL = "https://apicarto.ign.fr/api/codes-postaux/communes/";
|
||||
|
||||
@Autowired
|
||||
private final ArticleService articleService;
|
||||
private final UserService userService;
|
||||
@@ -90,7 +101,8 @@ public class ArticleController {
|
||||
@RequestParam("code_postal") String code_postal,
|
||||
@RequestParam("ville") String ville,
|
||||
@RequestParam("dateDebut") String dateDebut,
|
||||
@RequestParam("dateFin") String dateFin) {
|
||||
@RequestParam("dateFin") String dateFin,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
//Récupérer l'utilisateur pour set l'article
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
String username = authentication.getName();
|
||||
@@ -109,10 +121,99 @@ public class ArticleController {
|
||||
dDateDebut = format.parse(dateDebut);
|
||||
dDateFin = format.parse(dateFin);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
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/new";
|
||||
}
|
||||
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/new";
|
||||
}
|
||||
//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/new";
|
||||
}
|
||||
//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/new";
|
||||
}
|
||||
if (!article.getPhoto().getOriginalFilename().toLowerCase().endsWith(".jpg")) {
|
||||
redirectAttributes.addAttribute("erreur", "L'image doit avoir une extension .jpg.");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
}
|
||||
//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/new";
|
||||
}
|
||||
//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/new";
|
||||
}
|
||||
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/new";
|
||||
}
|
||||
|
||||
//Vérification rue
|
||||
if (!Pattern.matches("^[a-zA-Z0-9 ]+$", retrait.getRue())){
|
||||
redirectAttributes.addAttribute("erreur", "Le rue n'est pas valide.");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
if (retrait.getRue().isEmpty()){
|
||||
redirectAttributes.addAttribute("erreur", "Entrer une rue.");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
//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);
|
||||
System.out.println(cityName);
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "Une erreur est survenue !");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
if (!villeCodePostal.contains(userProfile.getVille())) {
|
||||
String showCity = String.join(", ", villeCodePostal);
|
||||
redirectAttributes.addAttribute("erreur", "Essayer : " + showCity);
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "La ville n'est pas valide.");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
} else {
|
||||
redirectAttributes.addAttribute("erreur", "Le code postal n'est pas valide.");
|
||||
return "redirect:/article/new";
|
||||
}
|
||||
//Validation du formulaire
|
||||
retrait.setNumArticle(articleService.saveArticle(article));
|
||||
retraitService.setRetrait(retrait);
|
||||
return "redirect:/accueil";
|
||||
|
||||
Reference in New Issue
Block a user