lien bdd & html liste des articles

This commit is contained in:
ionak
2024-04-22 22:08:12 +02:00
parent 3480ce9aeb
commit 2fcafc0ea1
22 changed files with 211 additions and 34 deletions

View File

@@ -0,0 +1,14 @@
package fr.eni.enchere.bll;
import fr.eni.enchere.bo.Article;
import java.util.List;
public interface ArticleService {
List<Article> findAllArticle();
Article findArticleById(int id);
void saveArticle(Article article);
void deleteArticle(int id);
void updateArticle(int id);
}

View File

@@ -0,0 +1,41 @@
package fr.eni.enchere.bll;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.dal.ArticleRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService{
private ArticleRepository articleRepository;
public ArticleServiceImpl(ArticleRepository articleRepository) {
this.articleRepository = articleRepository;
}
@Override
public List<Article> findAllArticle() {
return articleRepository.findAllArticle();
}
@Override
public Article findArticleById(int id) {
return null;
}
@Override
public void saveArticle(Article article) {
}
@Override
public void deleteArticle(int id) {
}
@Override
public void updateArticle(int id) {
}
}

View File

@@ -1,7 +1,7 @@
package fr.eni.enchere.bll;
import fr.eni.enchere.bo.User;
import fr.eni.enchere.dall.UserRepository;
import fr.eni.enchere.dal.UserRepository;
import org.springframework.stereotype.Service;
import java.util.List;

View File

@@ -1,5 +1,8 @@
package fr.eni.enchere.controllers;
import fr.eni.enchere.bll.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -8,11 +11,21 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AccueilController {
public AccueilController() {
private static final Logger logger = LoggerFactory.getLogger(AccueilController.class);
private ArticleService articleService;
public AccueilController(ArticleService articleService) {
super();
this.articleService = articleService;
}
@GetMapping({"/", "/accueil"})
public String viewAccueil(Model model) {
logger.info("Liste des articles : {}", articleService.findAllArticle().getLast());
model.addAttribute("articles", articleService.findAllArticle());
return "accueil";
}

View File

@@ -0,0 +1,13 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import java.util.List;
public interface ArticleRepository {
List<Article> findAllArticle();
Article findArticleById(int id);
void saveArticle(Article article);
void deleteArticle(int id);
void updateArticle(int id);
}

View File

@@ -0,0 +1,73 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.controllers.AccueilController;
import org.slf4j.Logger;
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.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
@Primary
public class ArticleRepositoryImpl implements ArticleRepository {
private static final Logger logger = LoggerFactory.getLogger(ArticleRepositoryImpl.class);
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
private class ArticleRowMapper implements RowMapper<Article> {
@Override
public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
Article article = new Article();
article.setId(rs.getInt("no_article"));
article.setNom(rs.getString("nom_article"));
article.setDesc(rs.getString("description"));
article.setDateDebutEnch(rs.getDate("date_debut_encheres"));
article.setDateFinEnch(rs.getDate("date_fin_encheres"));
article.setPrixInitial(rs.getFloat("prix_initial"));
article.setPrixVente(rs.getFloat("prix_vente"));
article.setNumUtilisateur(rs.getInt("no_utilisateur"));
article.setNumCategorie(rs.getInt("no_categorie"));
return article;
}
}
public ArticleRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.namedJdbcTemplate = namedJdbcTemplate;
}
@Override
public List<Article> findAllArticle() {
String sql = "SELECT * FROM ARTICLES_VENDUS";
List<Article> articles = jdbcTemplate.query(sql, new ArticleRowMapper());
return articles;
}
@Override
public Article findArticleById(int id) {
return null;
}
@Override
public void saveArticle(Article article) {
}
@Override
public void deleteArticle(int id) {
}
@Override
public void updateArticle(int id) {
}
}

View File

@@ -1,4 +1,4 @@
package fr.eni.enchere.dall;
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.User;

View File

@@ -1,10 +1,10 @@
package fr.eni.enchere.dall;
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
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;
@@ -15,10 +15,11 @@ import java.util.List;
public class UserRepositoryImpl implements UserRepository {
private final JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
public UserRepositoryImpl(JdbcTemplate jdbcTemplate) {
public UserRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.namedParameterJdbcTemplate = namedJdbcTemplate;
}
@Override
@@ -34,12 +35,7 @@ public class UserRepositoryImpl implements UserRepository {
@Override
public void save(User utilisateur) {
if (utilisateur.getId() == 0) {
String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone," +
" rue, code_postal, ville, mot_de_passe, credit, administrateur)" +
" values (:pseudo, :nom, :prenom, :email, :telephone," +
" :rue, :code_postal, :ville, :mot_de_passe, 0, false)";
KeyHolder keyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false)";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("pseudo", utilisateur.getPseudo());
parameters.addValue("nom", utilisateur.getNom());
@@ -50,8 +46,12 @@ public class UserRepositoryImpl implements UserRepository {
parameters.addValue("code_postal", utilisateur.getCode_postal());
parameters.addValue("ville", utilisateur.getVille());
parameters.addValue("mot_de_passe", utilisateur.getPassword());
jdbcTemplate.update(sql, parameters, keyHolder, new String[] {"id"});
utilisateur.setId(keyHolder.getKey().intValue());
KeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update(sql, parameters, keyHolder, new String[] {"no_utilisateur"});
if (keyHolder.getKey() != null) {
utilisateur.setId(keyHolder.getKey().intValue());
}
}else {
//Mettre à jour
}

View File

@@ -1,13 +0,0 @@
package fr.eni.enchere.dall;
import fr.eni.enchere.bo.Article;
import java.util.List;
public interface ArticleRepository {
List<Article> findAll();
Article findById(int id);
void save(Article article);
void delete(int id);
void update(int id);
}

View File

@@ -1,4 +0,0 @@
package fr.eni.enchere.dall;
public class ArticleRepositoryImpl {
}

View File

@@ -0,0 +1,5 @@
.v-center {
display: flex;
align-items: center;
height: 100%;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 572 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

View File

@@ -7,8 +7,43 @@
<link href="assets/bootstrap-icons/bootstrap-icons.min.css" rel="stylesheet">
</head>
<body>
<div id="container-main"><h1>hello world !</h1></div>
<div id="container-main">
<div class="container mt-4">
<div class="row mb-4">
<div class="col-12">
<form th:action="@{/search}" method="get" class="input-group">
<input type="text" class="form-control" placeholder="Rechercher un article par nom..." name="searchTerm" th:value="${searchTerm}">
<button type="submit" class="btn btn-primary">Recherche</button>
</form>
</div>
</div>
<div class="row">
<div th:each="article : ${articles}" class="col-md-6 mb-4">
<div class="card shadow-sm h-100">
<div class="row g-0">
<div class="col-md-4 d-flex align-items-center justify-content-center p-3">
<img th:src="${'images/articles/' + article.id + '.jpg'}" alt="Image de l'article" class="img-fluid rounded">
</div>
<div class="col-md-8">
<div class="card-body d-flex flex-column">
<h5 class="card-title" th:text="${article.nom}">Nom de l'article</h5>
<p class="card-text mb-auto" th:text="${article.desc}">Description</p>
<div class="d-flex justify-content-between align-items-center">
<div>
<h6>Prix de vente: <span th:text="${article.prixVente}"></span></h6>
<h6 class="text-muted">Fin de l'enchere: <span th:text="${#dates.format(article.dateFinEnch, 'dd/MM/yyyy')}"></span></h6>
<script src="js/bootstrap/bootstrap.min.js"></script>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="js/bootstrap/bootstrap.min.js"></script>
</body>
</html>