This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Add CartDAOMySQLImpl class, implement CartDAO interface"
This reverts commit 3bb7705.
- Loading branch information
1 parent
3bb7705
commit b5701bd
Showing
2 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/io/hardingadonis/r7/dao/impl/mysql/CartDAOMySQLImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters