Lamdba Integration

Test Lamdbas with full pipeline of client through database
This commit is contained in:
NMerz
2020-10-03 23:00:19 -04:00
parent 1f361cf401
commit 81b4673766
18 changed files with 386 additions and 124 deletions

View File

@@ -3,11 +3,13 @@ import java.sql.SQLException;
import java.time.LocalDateTime;
public class ItemEntry {
Integer listID;
Integer productID;
Integer quantity;
LocalDateTime addedDate;
Boolean purchased;
public ItemEntry(ResultSet listRow) throws SQLException {
public ItemEntry(Integer listID, ResultSet listRow) throws SQLException {
this.listID = listID;
productID = listRow.getInt(1);
quantity = listRow.getInt(2);
addedDate = listRow.getObject(3, LocalDateTime.class);

View File

@@ -1,5 +1,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
@@ -7,14 +8,14 @@ public class List {
Integer itemID;
String name;
String owner;
LocalDateTime lastUpdated;
long lastUpdated;
ArrayList<ItemEntry> entries;
public List(ResultSet listRow) throws SQLException {
itemID = listRow.getInt(1);
name = listRow.getString(2);
owner = listRow.getString(3);
lastUpdated = listRow.getObject(4, LocalDateTime.class);
itemID = listRow.getInt("listID");
name = listRow.getString("name");
owner = listRow.getString("owner");
lastUpdated = listRow.getTimestamp("lastUpdated").toInstant().toEpochMilli();
entries = new ArrayList<>();
}
@@ -22,6 +23,17 @@ public class List {
entries.add(entry);
}
@Override
public String toString() {
return "List{" +
"itemID=" + itemID +
", name='" + name + '\'' +
", owner='" + owner + '\'' +
", lastUpdated=" + lastUpdated +
", entries=" + entries +
'}';
}
public ItemEntry[] getEntries() {
return entries.toArray(new ItemEntry[entries.size()]);
}
@@ -50,11 +62,11 @@ public class List {
this.owner = owner;
}
public LocalDateTime getLastUpdated() {
public long getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(LocalDateTime lastUpdated) {
public void setLastUpdated(long lastUpdated) {
this.lastUpdated = lastUpdated;
}
}

View File

@@ -1,6 +1,4 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.*;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
@@ -20,13 +18,16 @@ public class ListAdder implements CallHandler {
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
Connection connection = connector.getConnection();
PreparedStatement statement = connection.prepareStatement(LIST_CREATE);
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
statement.setString(2, cognitoID);
statement.setObject(3, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime());
statement.setTimestamp(3, Timestamp.from(Instant.now()));
System.out.println(statement);
statement.executeUpdate();
ResultSet newIDRS = statement.getGeneratedKeys();
newIDRS.first();
Integer newID = newIDRS.getInt(1);
connection.commit();
return null;
return newID;
}
}

View File

@@ -23,13 +23,14 @@ public class ListGetter implements CallHandler{
@Override
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryMap, String cognitoID) throws SQLException {
Connection connection = connector.getConnection();
Integer id = Integer.parseInt(queryMap.get("id"));
try {
try (Connection connection = connector.getConnection()) {
Integer id = Integer.parseInt(queryMap.get("id"));
if (id == -1) {
PreparedStatement getLists = connection.prepareStatement(GET_LISTS);
getLists.setString(1, cognitoID);
System.out.println(getLists);
ResultSet getListsResults = getLists.executeQuery();
System.out.println(getListsResults);
ArrayList<Integer> listIds = new ArrayList<>();
while (getListsResults.next()) {
listIds.add(getListsResults.getInt(1));
@@ -48,11 +49,10 @@ public class ListGetter implements CallHandler{
getListEntries.setInt(1, id);
ResultSet getEntryResults = getListEntries.executeQuery();
while (getEntryResults.next()) {
retrievedList.addItemEntry(new ItemEntry(getEntryResults));
retrievedList.addItemEntry(new ItemEntry(id, getEntryResults));
}
System.out.println(retrievedList);
return retrievedList;
} finally {
connection.close();
}
}
}

View File

@@ -6,14 +6,14 @@ import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
public class ItemAdder implements CallHandler {
public class ListEntryAdder implements CallHandler {
private DBConnector connector;
private String cognitoID;
private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)";
public ItemAdder(DBConnector connector, String cognitoID) {
public ListEntryAdder(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}
@@ -22,7 +22,7 @@ public class ItemAdder implements CallHandler {
Connection connection = connector.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST);
statement.setInt(1, (Integer) bodyMap.get("itemID"));
statement.setInt(1, (Integer) bodyMap.get("productID"));
statement.setInt(2, (Integer) bodyMap.get("listID"));
statement.setInt(3, (Integer) bodyMap.get("quantity"));
statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime());

View File

@@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
public class ItemDELETE implements RequestHandler<Map<String,Object>, Object> {
public class ListEntryDELETE implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class);
return BasicHandler.handleRequest(inputMap, unfilled, ListEntryDeleter.class);
}
}

View File

@@ -4,14 +4,14 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ItemDeleter implements CallHandler {
public class ListEntryDeleter 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) {
public ListEntryDeleter(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}

View File

@@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
public class ItemPOST implements RequestHandler<Map<String,Object>, Object> {
public class ListEntryPOST implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class);
return BasicHandler.handleRequest(inputMap, unfilled, ListEntryAdder.class);
}
}

View File

@@ -24,7 +24,7 @@ public class BasicHandler {
}
} catch (IOException |SQLException|ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
throw new RuntimeException(e.getLocalizedMessage());
}
return null;
}