From 86975532bc99f03c03f8ab0ee9ea11a319d94464 Mon Sep 17 00:00:00 2001 From: NMerz Date: Fri, 9 Oct 2020 00:11:44 -0400 Subject: [PATCH] Merge identical ListEntries Each productID should have a single list entry --- .../Lists/ListEntry/src/ListEntryAdder.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Lambdas/Lists/ListEntry/src/ListEntryAdder.java b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java index 043ef67..6f5dd33 100644 --- a/Lambdas/Lists/ListEntry/src/ListEntryAdder.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java @@ -1,9 +1,5 @@ -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Timestamp; +import java.sql.*; import java.time.Instant; -import java.time.ZoneOffset; import java.util.HashMap; import java.util.Map; @@ -12,6 +8,9 @@ public class ListEntryAdder implements CallHandler { private Connection connection; 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 (?, ?, ?, ?, ?)"; public ListEntryAdder(Connection connection, String cognitoID) { @@ -20,10 +19,24 @@ public class ListEntryAdder implements CallHandler { } public Object conductAction(Map bodyMap, HashMap 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); - statement.setInt(1, (Integer) bodyMap.get("productID")); - statement.setInt(2, (Integer) bodyMap.get("listID")); - statement.setInt(3, (Integer) bodyMap.get("quantity")); + statement.setInt(1, productID); + statement.setInt(2, listID); + statement.setInt(3, (Integer) bodyMap.get("quantity") + priorQuanity); statement.setTimestamp(4, Timestamp.from(Instant.now())); statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); System.out.println(statement);