Ajout article final

This commit is contained in:
jleroy
2024-04-24 16:55:05 +02:00
parent bc49d8eeee
commit 09e611656d
8 changed files with 106 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ public interface ArticleService {
List<Article> findAllArticle();
Article findArticleById(int id);
void saveArticle(Article article);
int saveArticle(Article article);
void deleteArticle(int id);
void updateArticle(int id);
List<Article> findArticleByTitle(String title);

View File

@@ -26,8 +26,8 @@ public class ArticleServiceImpl implements ArticleService{
}
@Override
public void saveArticle(Article article) {
public int saveArticle(Article article) {
return articleRepository.saveArticle(article);
}
@Override

View File

@@ -15,6 +15,7 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -71,26 +72,31 @@ 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) {
//Récupérer l'utilisateur pour set l'article
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = authentication.getName();
UserProfil userProfile = userService.utilisateurByName(username);
article.setUtilisateur(userProfile.getId());
article.setNoUtilisateur(userProfile.getId());
//Récupérer le lieu de retrait
Retrait retrait = new Retrait();
retrait.setRue(rue);
retrait.setCode_postale(code_postal);
retrait.setVille(ville);
//Reste de l'article
//Date dDateDebut = new SimpleDateFormat(dateDebut);
//article.setDateDebutEnch();
if (article.getId() == 0){
//Création d'un article
} else {
//Mise à jour d'un 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) {
e.printStackTrace();
}
article.setDateDebutEnch(dDateDebut);
article.setDateFinEnch(dDateFin);
retrait.setNumArticle(articleService.saveArticle(article));
retraitService.setRetrait(retrait);
return "redirect:/accueil";
}

View File

@@ -10,7 +10,7 @@ public interface ArticleRepository {
List<Article> searchArticle(SearchArticleCritere critere);
Article findArticleById(int id);
List<Article> findArticleByTitle(String title);
void saveArticle(Article article);
int saveArticle(Article article);
void deleteArticle(int id);
void updateArticle(int id);
}

View File

@@ -10,9 +10,19 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -22,6 +32,7 @@ import java.util.List;
@Primary
public class ArticleRepositoryImpl implements ArticleRepository {
private static final String UPLOAD_DIR = "src/main/resources/static/images/articles";
private static final Logger logger = LoggerFactory.getLogger(ArticleRepositoryImpl.class);
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
@@ -91,8 +102,48 @@ public class ArticleRepositoryImpl implements ArticleRepository {
}
@Override
public void saveArticle(Article article) {
public int saveArticle(Article article) {
if (article.getId() == 0) {
//Nouvel article
String sql = "INSERT INTO ARTICLES_VENDUS (nom_article, description, date_debut_encheres, date_fin_encheres, prix_initial, prix_vente, no_utilisateur, no_categorie, isDelete) " +
"VALUES (:nom_article, :description, :date_debut_encheres, :date_fin_encheres, :prix_initial, null, :no_utilisateur, :no_categorie, 0)";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("nom_article", article.getNom());
parameters.addValue("description", article.getDesc());
parameters.addValue("date_debut_encheres", article.getDateDebutEnch());
parameters.addValue("date_fin_encheres", article.getDateFinEnch());
parameters.addValue("prix_initial", article.getPrixInitial());
parameters.addValue("no_utilisateur", article.getUtilisateur());
parameters.addValue("no_categorie", article.getNumCategorie());
KeyHolder keyHolder = new GeneratedKeyHolder();
namedJdbcTemplate.update(sql, parameters, keyHolder, new String[] {"no_article"});
if (keyHolder.getKey() != null) {
article.setId(keyHolder.getKey().intValue());
}
//Enregistrement du fichier
MultipartFile file = article.getPhoto();
if (file != null && !file.isEmpty()) {
try {
// Renommer le fichier avec l'ID de l'article
String newFileName = article.getId() + ".jpg";
// Chemin du dossier de destination
Path uploadPath = Paths.get(UPLOAD_DIR);
// Créer le dossier s'il n'existe pas
Files.createDirectories(uploadPath);
// Chemin complet du fichier de destination
Path filePath = uploadPath.resolve(newFileName);
// Copier le fichier dans le dossier de destination
FileCopyUtils.copy(file.getInputStream(), Files.newOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
// Gérer l'erreur
}
}
}else {
//Mettre à jour l'article
}
return article.getId();
}
@Override

View File

@@ -2,12 +2,23 @@ package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Retrait;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
@Primary
public class RetraitRepositoryImpl implements RetraitRepository {
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
public RetraitRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.namedJdbcTemplate = namedJdbcTemplate;
}
@Override
public Retrait findById(int id) {
return null;
@@ -15,6 +26,16 @@ public class RetraitRepositoryImpl implements RetraitRepository {
@Override
public void save(Retrait retrait) {
String sql = "SELECT COUNT(*) FROM RETRAITS WHERE no_article = :numArticle";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("numArticle", retrait.getNumArticle());
int count = namedJdbcTemplate.queryForObject(sql, parameters, Integer.class);
if (count > 0) {
//Mettre à jour les informations
} else {
//Ajouter le retrait
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 365 KiB

View File

@@ -37,7 +37,7 @@
<!-- Mise à prix -->
<div>
<label for="prix">Mise à prix:</label>
<input type="number" id="prix" th:field="*{prixInitial}" min="0" required>
<input type="number" id="prix" th:field="*{prixInitial}" min="0" step="0.01" required>
</div>
<!-- Date début enchère -->
@@ -75,6 +75,19 @@
<form th:action="@{/accueil}" method="post">
<button type="submit">Annuler</button>
</form>
<script>
// Obtenez la date actuelle et j+1
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
// Formatez la date au format YYYY-MM-DD pour l'attribut min
var formattedDateToday = today.toISOString().split('T')[0];
var formattedDateTomorrow = tomorrow.toISOString().split('T')[0];
// Attribuez la date formatée à l'attribut min de l'élément input
document.getElementById('dateDebut').min = formattedDateToday;
document.getElementById('dateFin').min = formattedDateTomorrow;
// Attribuez la date formatée à l'attribut min de l'élément input
</script>
</div>
</body>
</html>