Merge identical ListEntries

Each productID should have a single list entry
This commit is contained in:
NMerz 2020-10-09 00:11:44 -04:00
parent 09735f5ce3
commit 86975532bc

View File

@ -1,9 +1,5 @@
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -12,6 +8,9 @@ public class ListEntryAdder implements CallHandler {
private Connection connection; private Connection connection;
private String cognitoID; private String cognitoID;
private final String CHECK_ITEM_IN_LIST = "SELECT quantity from ListProduct WHERE productID = ? AND listID = ?;";
private final String CLEAR_PAIRING = "DELETE from ListProduct WHERE productID = ? AND listID = ?;";
private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)"; private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)";
public ListEntryAdder(Connection connection, String cognitoID) { public ListEntryAdder(Connection connection, String cognitoID) {
@ -20,10 +19,24 @@ public class ListEntryAdder implements CallHandler {
} }
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException { public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
PreparedStatement quantitiyStatement = connection.prepareStatement(CHECK_ITEM_IN_LIST);
Integer productID = (Integer) bodyMap.get("productID");
Integer listID = (Integer) bodyMap.get("listID");
quantitiyStatement.setInt(1, productID);
quantitiyStatement.setInt(2, listID);
ResultSet quanitityRS = quantitiyStatement.executeQuery();
int priorQuanity = 0;
if (quanitityRS.next()) {
priorQuanity = quanitityRS.getInt(1);
}
PreparedStatement clearStatement = connection.prepareStatement(CLEAR_PAIRING);
clearStatement.setInt(1, productID);
clearStatement.setInt(2, listID);
clearStatement.executeUpdate();
PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST);
statement.setInt(1, (Integer) bodyMap.get("productID")); statement.setInt(1, productID);
statement.setInt(2, (Integer) bodyMap.get("listID")); statement.setInt(2, listID);
statement.setInt(3, (Integer) bodyMap.get("quantity")); statement.setInt(3, (Integer) bodyMap.get("quantity") + priorQuanity);
statement.setTimestamp(4, Timestamp.from(Instant.now())); statement.setTimestamp(4, Timestamp.from(Instant.now()));
statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); statement.setBoolean(5, (Boolean) bodyMap.get("purchased"));
System.out.println(statement); System.out.println(statement);