Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.1 kB
1
Indexable
Never
package DAOs;

import Models.Detail;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DetailDAO {

    private Connection conn;

    public DetailDAO() {
        conn = DB.DbConnection.GetConnection();
    }

    public ResultSet GetAll() {
        ResultSet rs = null;
        String sql = "SELECT * FROM Product";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public Detail getProductByID(int id) {
        String sql = "SELECT * FROM Product WHERE ID = ?";
        Detail product = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                product = new Detail(
                        rs.getInt("ID"),
                        rs.getString("FlowerName"),
                        rs.getInt("Price"),
                        rs.getInt("Quantity"),
                        rs.getString("Linkimage"),
                        rs.getString("Description")
                );
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return product;
    }

    public boolean addToCart(int productId) {
        // Check if the product is already in the cart
        if (isProductInCart(productId)) {
            // Increment the quantity of the existing product in the cart
            updateCartItemQuantity(productId, getCartItemQuantity(productId) + 1);
        } else {
            // Add the product to the cart with quantity 1
            String sql = "INSERT INTO Cart (ProductID, Quantity) VALUES (?, 1)";
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setInt(1, productId);
                ps.executeUpdate();
                return true;
            } catch (SQLException ex) {
                Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
    private boolean isProductInCart(int productId) {
        String sql = "SELECT COUNT(*) FROM Cart WHERE ProductID = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, productId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    private int getCartItemQuantity(int productId) {
        String sql = "SELECT Quantity FROM Cart WHERE ProductID = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, productId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt("Quantity");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    private void updateCartItemQuantity(int productId, int quantity) {
        String sql = "UPDATE Cart SET Quantity = ? WHERE ProductID = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, quantity);
            ps.setInt(2, productId);
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DetailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}