Merge branch 'main' into Olivier

This commit is contained in:
Parpaillax
2024-04-23 16:00:21 +02:00
15 changed files with 279 additions and 46 deletions

View File

@@ -1,11 +1,13 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.SearchArticleCritere;
import java.util.List;
public interface ArticleRepository {
List<Article> findAllArticle();
List<Article> searchArticle(SearchArticleCritere critere);
Article findArticleById(int id);
List<Article> findArticleByTitle(String title);
void saveArticle(Article article);

View File

@@ -1,6 +1,9 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.SearchArticleCritere;
import fr.eni.enchere.bo.UserProfil;
import fr.eni.enchere.controllers.AccueilController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -12,6 +15,7 @@ import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
@@ -21,6 +25,7 @@ public class ArticleRepositoryImpl implements ArticleRepository {
private static final Logger logger = LoggerFactory.getLogger(ArticleRepositoryImpl.class);
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
private UserService userService;
private class ArticleRowMapper implements RowMapper<Article> {
@Override
@@ -33,15 +38,23 @@ public class ArticleRepositoryImpl implements ArticleRepository {
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"));
UserProfil user = userService.utilisateurById(rs.getInt("no_utilisateur"));
if (user != null) {
article.setUtilisateur(user);
} else {
logger.error("erreur de l'utilisateur");
}
article.setNumCategorie(rs.getInt("no_categorie"));
return article;
}
}
public ArticleRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
public ArticleRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, UserService userService) {
this.jdbcTemplate = jdbcTemplate;
this.namedJdbcTemplate = namedJdbcTemplate;
this.userService = userService;
}
@Override
@@ -51,6 +64,24 @@ public class ArticleRepositoryImpl implements ArticleRepository {
return articles;
}
@Override
public List<Article> searchArticle(SearchArticleCritere critere) {
StringBuilder sql = new StringBuilder("SELECT * FROM ARTICLES_VENDUS WHERE 1 = 1");
List<Object> params = new ArrayList<>();
if (critere.getNoCategorie() != null) {
sql.append(" AND no_categorie = ?");
params.add(critere.getNoCategorie());
}
if (critere.getTitle() != null && !critere.getTitle().isEmpty()) {
sql.append(" AND nom_article LIKE ?");
params.add( '%' + critere.getTitle() + "%");
}
return jdbcTemplate.query(sql.toString(), new ArticleRowMapper(), params.toArray());
}
@Override
public Article findArticleById(int id) {
return null;
@@ -58,8 +89,8 @@ public class ArticleRepositoryImpl implements ArticleRepository {
@Override
public List<Article> findArticleByTitle(String title) {
String sql = "SELECT * FROM ARTICLES_VENDUS WHERE nom_article LIKE '?'";
List<Article> articles = jdbcTemplate.query(sql, new ArticleRowMapper(), title);
String sql = "SELECT * FROM ARTICLES_VENDUS WHERE nom_article LIKE ?";
List<Article> articles = jdbcTemplate.query(sql, new ArticleRowMapper(), "%" + title + "%");
return articles;
}

View File

@@ -0,0 +1,10 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Categorie;
import java.util.List;
public interface CategorieRepository {
List<Categorie> findAllCategories();
Categorie findCategorieById(int id);
}

View File

@@ -0,0 +1,50 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.Categorie;
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
public class CategorieRepositoryImpl implements CategorieRepository {
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedJdbcTemplate;
private class CategorieRowMapper implements RowMapper<Categorie> {
@Override
public Categorie mapRow(ResultSet rs, int rowNum) throws SQLException {
Categorie categorie = new Categorie();
categorie.setId(rs.getInt("no_categorie"));
categorie.setLibelle(rs.getString("libelle"));
return categorie;
}
}
public CategorieRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.namedJdbcTemplate = namedJdbcTemplate;
}
@Override
public List<Categorie> findAllCategories() {
String sql = "SELECT * FROM CATEGORIES";
List<Categorie> categories = jdbcTemplate.query(sql, new CategorieRowMapper());
System.out.println(categories);
return categories;
}
@Override
public Categorie findCategorieById(int id) {
String sql = "SELECT * FROM CATEGORIES WHERE no_categorie = :id";
Categorie categorie = jdbcTemplate.queryForObject(sql, new CategorieRowMapper(), id);
return categorie;
}
}

View File

@@ -1,9 +1,12 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.UserProfil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
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;
@@ -11,6 +14,8 @@ import org.springframework.jdbc.support.KeyHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -30,14 +35,25 @@ public class UserRepositoryImpl implements UserRepository {
this.passwordEncoder = passwordEncoder;
}
@Override
public List<UserProfil> findAll() {
return List.of();
}
public class UserRowMapper implements RowMapper<UserProfil> {
@Override
public UserProfil findById(int id) {
return null;
@Override
public UserProfil mapRow(ResultSet rs, int rowNum) throws SQLException {
UserProfil userProfile = new UserProfil();
userProfile.setId(rs.getInt("no_utilisateur"));
userProfile.setPrenom(rs.getString("prenom"));
userProfile.setNom(rs.getString("nom"));
userProfile.setPseudo(rs.getString("pseudo"));
userProfile.setEmail(rs.getString("email"));
userProfile.setTelephone(rs.getString("telephone"));
userProfile.setRue(rs.getString("rue"));
userProfile.setCode_postal(rs.getString("code_postal"));
userProfile.setVille(rs.getString("ville"));
userProfile.setPassword(rs.getString("mot_de_passe"));
userProfile.setCredit(rs.getFloat("credit"));
userProfile.setAdmin(rs.getBoolean("administrateur"));
return userProfile;
}
}
@Override
@@ -45,22 +61,20 @@ public class UserRepositoryImpl implements UserRepository {
String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username AND isDelete = 0";
Map<String, Object> params = new HashMap<>();
params.put("username", username);
UserProfil user = namedParameterJdbcTemplate.queryForObject(sql, params, (rs, rowNum) -> {
UserProfil userProfile = new UserProfil();
userProfile.setId(rs.getInt("no_utilisateur"));
userProfile.setPrenom(rs.getString("prenom"));
userProfile.setNom(rs.getString("nom"));
userProfile.setPseudo(rs.getString("pseudo"));
userProfile.setEmail(rs.getString("email"));
userProfile.setTelephone(rs.getString("telephone"));
userProfile.setRue(rs.getString("rue"));
userProfile.setCode_postal(rs.getString("code_postal"));
userProfile.setVille(rs.getString("ville"));
userProfile.setPassword(rs.getString("mot_de_passe"));
userProfile.setCredit(rs.getFloat("credit"));
userProfile.setAdmin(rs.getBoolean("administrateur"));
return userProfile;
});
UserProfil user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), params);
return user;
}
@Override
public List<UserProfil> findAll() {
return List.of();
}
@Override
public UserProfil findById(int id) {
String sql = "SELECT * FROM UTILISATEURS WHERE no_utilisateur = ?";
UserProfil user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), id);
return user;
}