From 1f361cf401a2e5c58c82d13b1a92f3d8eb2bcd73 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 3 Oct 2020 18:35:27 -0400 Subject: [PATCH] Finish V1 of List and Item Lambdas List and Item Lambdas and client calls should be roughly finished (pending full testing once databse config is done). --- Lambdas/Lists/Item/src/Item.java | 72 +++++++++++++++++++ Lambdas/Lists/Item/src/ItemDELETE.java | 11 +++ Lambdas/Lists/Item/src/ItemDeleter.java | 33 +++++++++ Lambdas/Lists/List/src/ItemEntry.java | 32 +++++++++ Lambdas/Lists/List/src/List.java | 44 +++++++++++- Lambdas/Lists/List/src/ListGetter.java | 39 +++++++--- .../java/com/example/listify/Requestor.java | 6 ++ .../example/listify/SynchronousReceiver.java | 2 +- 8 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 Lambdas/Lists/Item/src/ItemDELETE.java create mode 100644 Lambdas/Lists/Item/src/ItemDeleter.java diff --git a/Lambdas/Lists/Item/src/Item.java b/Lambdas/Lists/Item/src/Item.java index 53ef341..dcdbdd6 100644 --- a/Lambdas/Lists/Item/src/Item.java +++ b/Lambdas/Lists/Item/src/Item.java @@ -49,4 +49,76 @@ public class Item { ", fetchCounts=" + fetchCounts + '}'; } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getChainID() { + return chainID; + } + + public void setChainID(Integer chainID) { + this.chainID = chainID; + } + + public String getUpc() { + return upc; + } + + public void setUpc(String upc) { + this.upc = upc; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getImageURL() { + return imageURL; + } + + public void setImageURL(String imageURL) { + this.imageURL = imageURL; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public LocalDateTime getRetrievedDate() { + return retrievedDate; + } + + public void setRetrievedDate(LocalDateTime retrievedDate) { + this.retrievedDate = retrievedDate; + } + + public Integer getFetchCounts() { + return fetchCounts; + } + + public void setFetchCounts(Integer fetchCounts) { + this.fetchCounts = fetchCounts; + } } diff --git a/Lambdas/Lists/Item/src/ItemDELETE.java b/Lambdas/Lists/Item/src/ItemDELETE.java new file mode 100644 index 0000000..7d0f1ee --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemDELETE.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ItemDELETE implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class); + } +} \ No newline at end of file diff --git a/Lambdas/Lists/Item/src/ItemDeleter.java b/Lambdas/Lists/Item/src/ItemDeleter.java new file mode 100644 index 0000000..ad03a91 --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemDeleter.java @@ -0,0 +1,33 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class ItemDeleter implements CallHandler { + + private DBConnector connector; + private String cognitoID; + + private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);"; + + public ItemDeleter(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { + Connection connection = connector.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); + statement.setInt(1, (Integer) bodyMap.get("ProductID")); + statement.setInt(2, (Integer) bodyMap.get("ListID")); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); + } finally { + connection.close(); + } + return null; + } +} diff --git a/Lambdas/Lists/List/src/ItemEntry.java b/Lambdas/Lists/List/src/ItemEntry.java index b05ddc7..513d4ae 100644 --- a/Lambdas/Lists/List/src/ItemEntry.java +++ b/Lambdas/Lists/List/src/ItemEntry.java @@ -13,4 +13,36 @@ public class ItemEntry { addedDate = listRow.getObject(3, LocalDateTime.class); purchased = listRow.getBoolean(4); } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public LocalDateTime getAddedDate() { + return addedDate; + } + + public void setAddedDate(LocalDateTime addedDate) { + this.addedDate = addedDate; + } + + public Boolean getPurchased() { + return purchased; + } + + public void setPurchased(Boolean purchased) { + this.purchased = purchased; + } } diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index c2ac9c6..5f9c4f5 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -1,18 +1,60 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; +import java.util.ArrayList; public class List { Integer itemID; String name; String owner; LocalDateTime lastUpdated; + ArrayList entries; public List(ResultSet listRow) throws SQLException { itemID = listRow.getInt(1); name = listRow.getString(2); owner = listRow.getString(3); - lastUpdated = listRow.getObject(8, LocalDateTime.class); + lastUpdated = listRow.getObject(4, LocalDateTime.class); + entries = new ArrayList<>(); } + public void addItemEntry(ItemEntry entry) { + entries.add(entry); + } + + public ItemEntry[] getEntries() { + return entries.toArray(new ItemEntry[entries.size()]); + } + + public Integer getItemID() { + return itemID; + } + + public void setItemID(Integer itemID) { + this.itemID = itemID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public LocalDateTime getLastUpdated() { + return lastUpdated; + } + + public void setLastUpdated(LocalDateTime lastUpdated) { + this.lastUpdated = lastUpdated; + } } diff --git a/Lambdas/Lists/List/src/ListGetter.java b/Lambdas/Lists/List/src/ListGetter.java index 9e8596a..3e4ff1e 100644 --- a/Lambdas/Lists/List/src/ListGetter.java +++ b/Lambdas/Lists/List/src/ListGetter.java @@ -4,6 +4,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -12,6 +13,8 @@ public class ListGetter implements CallHandler{ private final String cognitoID; private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;"; + private final String GET_LISTS = "SELECT listID FROM List WHERE owner = ?;"; + private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;"; public ListGetter(DBConnector connector, String cognitoID) { this.connector = connector; @@ -21,17 +24,33 @@ public class ListGetter implements CallHandler{ @Override public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { Connection connection = connector.getConnection(); + Integer id = Integer.parseInt(queryMap.get("id")); try { - PreparedStatement statement = connection.prepareStatement(GET_LIST); - statement.setInt(1, Integer.parseInt(queryMap.get("id"))); - System.out.println(statement); - ResultSet queryResults = statement.executeQuery(); - queryResults.first(); - System.out.println(queryResults); - String returnValue; - List retrievedItem = new List(queryResults); - System.out.println(retrievedItem); - return retrievedItem; + if (id == -1) { + PreparedStatement getLists = connection.prepareStatement(GET_LISTS); + getLists.setString(1, cognitoID); + ResultSet getListsResults = getLists.executeQuery(); + ArrayList listIds = new ArrayList<>(); + while (getListsResults.next()) { + listIds.add(getListsResults.getInt(1)); + } + return listIds; + } + PreparedStatement getList = connection.prepareStatement(GET_LIST); + getList.setInt(1, id); + System.out.println(getList); + ResultSet getListResults = getList.executeQuery(); + getListResults.first(); + System.out.println(getListResults); + List retrievedList = new List(getListResults); + System.out.println(retrievedList); + PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES); + getListEntries.setInt(1, id); + ResultSet getEntryResults = getListEntries.executeQuery(); + while (getEntryResults.next()) { + retrievedList.addItemEntry(new ItemEntry(getEntryResults)); + } + return retrievedList; } finally { connection.close(); } diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 3032c0d..2134ac8 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -28,6 +28,12 @@ public class Requestor { getObject(id, classType, receiver, null); } + public void getListOfIds(Class ofType, Receiver successHandler, RequestErrorHandler failureHandler) { + String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?list=-1"; + Request postRequest = buildBaseRequest(getURL, "GET", null); + launchCall(postRequest, successHandler, Integer[].class, failureHandler); + } + public void getObject(String id, Class classType, Receiver successHandler, RequestErrorHandler failureHandler) { String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; Request postRequest = buildBaseRequest(getURL, "GET", null); diff --git a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java index 7272a3e..74ca055 100644 --- a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java +++ b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java @@ -34,6 +34,6 @@ public class SynchronousReceiver implements Requestor.Receiver, Requestor. @Override public void acceptError(IOException error) { waiting = false; - this.error = error + this.error = error; } }