package dao;

import model.Product;
import utils.MysqlConnect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO {

    public List<Product> getProductList() throws SQLException {
        List<Product> result = new ArrayList<>();

        String sql = "SELECT * FROM products";

        Connection c = MysqlConnect.getConnecton();

        PreparedStatement pt = c.prepareStatement(sql);

        ResultSet rs = pt.executeQuery();

        while (rs.next()) {

            Product p = new Product(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getDouble("price"),
                    rs.getString("title"),
                    rs.getDate("created"),
                    rs.getString("catalog"),
                    rs.getBoolean("status")
            );

            result.add(p);

        }

        return result;
    }
}
