Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Revert "Add CartDAOMySQLImpl class, implement CartDAO interface"
Browse files Browse the repository at this point in the history
This reverts commit 3bb7705.
  • Loading branch information
hardingadonis committed Nov 9, 2023
1 parent 3bb7705 commit b5701bd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
100 changes: 100 additions & 0 deletions src/main/java/io/hardingadonis/r7/dao/impl/mysql/CartDAOMySQLImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.hardingadonis.r7.dao.impl.mysql;

import io.hardingadonis.r7.dao.*;
import io.hardingadonis.r7.model.*;
import io.hardingadonis.r7.services.*;
import java.sql.*;
import java.util.*;

public class CartDAOMySQLImpl implements CartDAO {

@Override
public List<Cart> getAll(int userID) {
List<Cart> list = new ArrayList<>();

try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("SELECT * FROM cart WHERE user_id = ?");
smt.setInt(1, userID);

ResultSet rs = smt.executeQuery();

while (rs.next()) {
int productID = rs.getInt("product_id");
int amount = rs.getInt("amount");

list.add(new Cart(userID, productID, amount));
}

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

return list;
}

@Override
public void insert(Cart obj) {
try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("INSERT INTO cart(user_id, product_id, amount) VALUES (?, ?, ?)");
smt.setInt(1, obj.getUserID());
smt.setInt(2, obj.getProductID());
smt.setInt(3, obj.getAmount());

smt.executeUpdate();

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}

@Override
public void update(Cart obj) {
try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("UPDATE cart SET amount WHERE user_id = ? AND product_id = ?");
smt.setInt(1, obj.getAmount());
smt.setInt(2, obj.getUserID());
smt.setInt(3, obj.getProductID());

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}

@Override
public void delete(int userID, int productID) {
try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("DELETE FROM cart WHERE user_id = ? AND product_id = ?");
smt.setInt(1, userID);
smt.setInt(2, productID);

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}

@Override
public void deleteAll(int userID) {
try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("DELETE FROM cart WHERE user_id = ?");
smt.setInt(1, userID);

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
4 changes: 0 additions & 4 deletions src/main/java/io/hardingadonis/r7/services/Singleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public class Singleton {
public static DBContext dbContext;

public static AdminDAO adminDAO;

public static CartDAO cartDAO;

public static CategoryDAO categoryDAO;

Expand All @@ -22,8 +20,6 @@ public class Singleton {
dbContext = new DBContextMySQLImpl();

adminDAO = new AdminDAOMySQLImpl();

cartDAO = new CartDAOMySQLImpl();

categoryDAO = new CategoryDAOMySQLImpl();

Expand Down

0 comments on commit b5701bd

Please sign in to comment.