update article + retrait
This commit is contained in:
@@ -13,7 +13,7 @@ public interface ArticleService {
|
||||
Article findArticleById(int id);
|
||||
int saveArticle(Article article);
|
||||
void deleteArticle(int id);
|
||||
void updateArticle(int id);
|
||||
int updateArticle(Article article);
|
||||
List<Article> findArticleByTitle(String title);
|
||||
Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class ArticleServiceImpl implements ArticleService{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateArticle(int id) {
|
||||
|
||||
public int updateArticle(Article article) {
|
||||
return articleRepository.updateArticle(article);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,4 +5,5 @@ import fr.eni.enchere.bo.Retrait;
|
||||
public interface RetraitService {
|
||||
Retrait findByNumArticle(int id);
|
||||
void setRetrait(Retrait retrait);
|
||||
void updateRetrait(Retrait retrait);
|
||||
}
|
||||
|
||||
@@ -22,4 +22,9 @@ public class RetraitServiceImpl implements RetraitService {
|
||||
public void setRetrait(Retrait retrait) {
|
||||
retraitRepository.save(retrait);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateRetrait(Retrait retrait) {
|
||||
retraitRepository.update(retrait);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -82,6 +83,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);
|
||||
@@ -239,11 +245,126 @@ public class ArticleController {
|
||||
Article article = this.articleService.findArticleById(id);
|
||||
Retrait retrait = this.retraitService.findByNumArticle(id);
|
||||
|
||||
System.out.println(retrait.getRue());
|
||||
model.addAttribute("article", article);
|
||||
model.addAttribute("retrait", retrait);
|
||||
model.addAttribute("categories", this.categorieService.findAllCategories());
|
||||
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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,5 +14,5 @@ public interface ArticleRepository {
|
||||
List<Article> findArticleByTitle(String title);
|
||||
int saveArticle(Article article);
|
||||
void deleteArticle(int id);
|
||||
void updateArticle(int id);
|
||||
int updateArticle(Article article);
|
||||
}
|
||||
|
||||
@@ -168,7 +168,6 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
sql.append(")");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
// Compte le nombre total d'articles
|
||||
int totalCount = countArticlePageable(critere);
|
||||
|
||||
@@ -334,7 +333,16 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateArticle(int id) {
|
||||
|
||||
public int updateArticle(Article article) {
|
||||
String sql = "UPDATE ARTICLES_VENDUS " +
|
||||
"SET nom_article = ?, " +
|
||||
"description = ?," +
|
||||
"date_debut_encheres = ?," +
|
||||
"date_fin_encheres = ?," +
|
||||
"prix_initial = ?," +
|
||||
"no_categorie = ? " +
|
||||
"WHERE no_article = ?";
|
||||
jdbcTemplate.update(sql, article.getNom(), article.getDesc(), article.getDateDebutEnch(), article.getDateFinEnch(), article.getPrixInitial(), article.getNumCategorie(), article.getId());
|
||||
return article.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@ public interface RetraitRepository {
|
||||
Retrait findById(int id);
|
||||
Retrait findByNumArticle(int idArticle);
|
||||
void save(Retrait retrait);
|
||||
void update(Retrait retrait);
|
||||
}
|
||||
|
||||
@@ -47,7 +47,11 @@ public class RetraitRepositoryImpl implements RetraitRepository {
|
||||
public Retrait findByNumArticle(int numArticle) {
|
||||
String sql = "select * from RETRAITS where no_article = ?";
|
||||
Retrait retrait = jdbcTemplate.queryForObject(sql, new RetraitRowMapper(), numArticle);
|
||||
return retrait;
|
||||
if (retrait == null) {
|
||||
return null;
|
||||
} else {
|
||||
return retrait;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,7 +60,6 @@ public class RetraitRepositoryImpl implements RetraitRepository {
|
||||
MapSqlParameterSource parametersIsUpdate = new MapSqlParameterSource();
|
||||
parametersIsUpdate.addValue("numArticle", retrait.getNumArticle());
|
||||
int count = namedJdbcTemplate.queryForObject(sqlIsUpdate, parametersIsUpdate, Integer.class);
|
||||
System.out.println(count);
|
||||
if (count > 0) {
|
||||
//Mettre à jour les informations
|
||||
|
||||
@@ -71,4 +74,14 @@ public class RetraitRepositoryImpl implements RetraitRepository {
|
||||
namedJdbcTemplate.update(sql, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Retrait retrait) {
|
||||
String sql = "UPDATE RETRAITS " +
|
||||
"SET rue = ?, " +
|
||||
"code_postal = ?, " +
|
||||
"ville = ? " +
|
||||
"WHERE no_article = ?";
|
||||
jdbcTemplate.update(sql, retrait.getRue(), retrait.getCode_postale(), retrait.getVille(), retrait.getNumArticle());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,11 @@
|
||||
<strong><label class="col-form-label" th:text="#{article.details.label.sale_price}"></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} ? ${maxEnchere} : 'Aucune offre en cours'"></span>
|
||||
</div>
|
||||
|
||||
<div class="mt-2 d-flex flex-row align-items-end justify-content-between">
|
||||
<strong><label class="col-form-label" th:text="#{article.details.label.end_date}"></label></strong>
|
||||
<span th:text="${article.dateFinEnch}"></span>
|
||||
@@ -57,7 +62,10 @@
|
||||
</span>
|
||||
<button type="submit" class="btn btn-primary" th:text="#{article.details.button.bid}"></button>
|
||||
</form>
|
||||
<button th:if="${isArticleCurrentUser}" type="button" class="btn btn-success"> Modifier</button>
|
||||
<div class="mt-5 d-flex justify-content-end align-items-center">
|
||||
<a class="btn btn-secondary mr-2" href="/accueil" th:text="#{edit.article.back}"></a>
|
||||
<a th:if="${#strings.equals(user.getPseudo(), article.pseudoUtilisateur)} and ${#dates.format(article.dateDebutEnch, 'yyyy-MM-dd')} > ${#dates.format(#dates.createNow(), 'yyyy-MM-dd')}" class="btn btn-primary" th:href="@{/article/edit(id=${article.id})}" th:text="#{edit.article.update}"></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -7,9 +7,10 @@
|
||||
<body>
|
||||
<div id="container-main" class="container mt-5">
|
||||
<h1 th:text="#{edit.article.title}"></h1>
|
||||
<form th:action="@{/article/new}" th:object="${article}">
|
||||
<form th:action="@{/article/edit}" method="post" th:object="${article}">
|
||||
<!-- Titre article-->
|
||||
<div class="mb-3">
|
||||
<input type="hidden" id="id" name="id" th:field="*{id}">
|
||||
<strong><label for="nom" class="form-label">Titre</label></strong>
|
||||
<input type="text" class="form-control" th:field="*{nom}" id="nom">
|
||||
</div>
|
||||
@@ -28,7 +29,7 @@
|
||||
<!-- Photo article-->
|
||||
<div class="mb-3">
|
||||
<label for="photo" class="form-label" th:text="#{article.add.form.label.photo}">Photo de l'article:</label>
|
||||
<input type="file" class="form-control" th:field="*{photo}" id="photo" accept="image/jpeg">
|
||||
<input type="file" class="form-control" id="photo" accept="image/jpeg">
|
||||
</div>
|
||||
<!-- Prix de l'article-->
|
||||
<div class="mb-3">
|
||||
@@ -37,30 +38,36 @@
|
||||
</div>
|
||||
<!-- Date début enchère-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="dateStart" class="form-label">Date début d'enchère</label></strong>
|
||||
<input type="date" class="form-control" th:field="*{dateDebutEnch}" id="dateStart">
|
||||
<strong><label for="dateDebut" class="form-label">Date début d'enchère</label></strong>
|
||||
<input type="date" class="form-control" th:value="*{dateDebutEnch}" id="dateDebut" name="dateDebut">
|
||||
</div>
|
||||
<!-- Date fin enchère-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="dateEnd" class="form-label">Date fin d'enchère</label></strong>
|
||||
<input type="date" class="form-control" th:field="*{dateFinEnch}" id="dateEnd">
|
||||
<strong><label for="dateFin" class="form-label">Date fin d'enchère</label></strong>
|
||||
<input type="date" class="form-control" th:value="*{dateFinEnch}" id="dateFin" name="dateFin">
|
||||
</div>
|
||||
<!-- Retrait-->
|
||||
<h4 th:text="#{article.add.form.label.removal}">Retrait</h4>
|
||||
<!-- Rue-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="street" class="form-label">Rue</label></strong>
|
||||
<input type="text" class="form-control" th:field="${retrait.rue}" id="street">
|
||||
<div th:object="${retrait}">
|
||||
<!-- Rue-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="street" class="form-label">Rue</label></strong>
|
||||
<input type="text" class="form-control" th:field="*{rue}" id="street">
|
||||
</div>
|
||||
<!-- Code postal-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="cp" class="form-label">Code postal</label></strong>
|
||||
<input type="text" class="form-control" th:field="*{code_postale}" id="cp">
|
||||
</div>
|
||||
<!-- Ville-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="city" class="form-label">Ville</label></strong>
|
||||
<input type="text" class="form-control" th:field="*{ville}" id="city">
|
||||
</div>
|
||||
</div>
|
||||
<!-- Code postal-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="cp" class="form-label">Code postal</label></strong>
|
||||
<input type="text" class="form-control" th:field="${retrait.code_postale}" id="cp">
|
||||
</div>
|
||||
<!-- Ville-->
|
||||
<div class="mb-3">
|
||||
<strong><label for="city" class="form-label">Ville</label></strong>
|
||||
<input type="text" class="form-control" th:field="${retrait.ville}" id="city">
|
||||
<div class="d-flex justify-content-end align-items-center">
|
||||
<a class="btn btn-secondary" th:href="@{/article/show(id=*{id})}" th:text="#{edit.profil.button.cancel}"></a>
|
||||
<button type="submit" class="btn btn-primary border-right ml-2" th:text="#{edit.profil.button.edit}"></button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user