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;
    }
}
