GET strcuture

Build out infrastructure for get requests (along with the first couple).

This includes changing the request engine from Volley to OKHTTP due to issues get Volley's callbacks to call back.
This commit is contained in:
NMerz
2020-10-03 17:37:46 -04:00
parent 69572b3a1f
commit f107ab023d
21 changed files with 398 additions and 52 deletions

View File

@@ -0,0 +1,52 @@
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
public class Item {
Integer productID;
Integer chainID;
String upc;
String description;
BigDecimal price;
String imageURL;
String department;
LocalDateTime retrievedDate;
Integer fetchCounts;
Item(ResultSet itemRow) throws SQLException {
this.productID = itemRow.getInt(1);
System.out.println(this.productID);
this.chainID = itemRow.getInt(2);
System.out.println(this.chainID);
this.upc = itemRow.getString(3);
System.out.println(this.upc);
this.description = itemRow.getString(4);
System.out.println(this.description);
this.price = itemRow.getBigDecimal(5);
System.out.println(this.price);
this.imageURL = itemRow.getString(6);
System.out.println(imageURL);
this.department = itemRow.getString(7);
System.out.println(department);
this.retrievedDate = itemRow.getObject(8, LocalDateTime.class);
System.out.println(retrievedDate);
this.fetchCounts = itemRow.getInt(9);
System.out.println(fetchCounts);
}
@Override
public String toString() {
return "Item{" +
"productID=" + productID +
", chainID=" + chainID +
", upc='" + upc + '\'' +
", description='" + description + '\'' +
", price=" + price +
", imageURL='" + imageURL + '\'' +
", department='" + department + '\'' +
", retrievedDate=" + retrievedDate +
", fetchCounts=" + fetchCounts +
'}';
}
}

View File

@@ -1,6 +1,9 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
public class ItemAdder implements CallHandler {
@@ -8,20 +11,28 @@ public class ItemAdder implements CallHandler {
private DBConnector connector;
private String cognitoID;
private final String LIST_CREATE = "INSERT INTO Items (Name) VALUES (?)";
private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)";
public ItemAdder(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}
public String conductAction(Map<String, Object> bodyMap, String queryString) throws SQLException {
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);
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
System.out.println(statement);
statement.executeUpdate();
connection.commit();
try {
PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST);
statement.setInt(1, (Integer) bodyMap.get("itemID"));
statement.setInt(2, (Integer) bodyMap.get("listID"));
statement.setInt(3, (Integer) bodyMap.get("quantity"));
statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime());
statement.setBoolean(5, (Boolean) bodyMap.get("purchased"));
System.out.println(statement);
statement.executeUpdate();
connection.commit();
} finally {
connection.close();
}
return null;
}
}

View File

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

View File

@@ -1,9 +1,36 @@
import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ItemGetter implements CallHandler{
private final DBConnector connector;
private final String GET_ITEM = "SELECT * FROM Product WHERE productID = ?;";
public ItemGetter(DBConnector connector, String cognitoID) {
this.connector = connector;
}
@Override
public String conductAction(Map<String, Object> bodyMap, String cognitoID) throws SQLException {
return null;
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryMap, String cognitoID) throws SQLException {
Connection connection = connector.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(GET_ITEM);
statement.setInt(1, Integer.parseInt(queryMap.get("id")));
System.out.println(statement);
ResultSet queryResults = statement.executeQuery();
queryResults.first();
System.out.println(queryResults);
Item retrievedItem = new Item(queryResults);
System.out.println(retrievedItem);
return retrievedItem;
} finally {
connection.close();
}
}
}

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>, String>{
public class ItemPOST implements RequestHandler<Map<String,Object>, Object> {
public String handleRequest(Map<String, Object> inputMap, Context unfilled) {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class);
}
}

View File

@@ -0,0 +1,16 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
public class ItemEntry {
Integer productID;
Integer quantity;
LocalDateTime addedDate;
Boolean purchased;
public ItemEntry(ResultSet listRow) throws SQLException {
productID = listRow.getInt(1);
quantity = listRow.getInt(2);
addedDate = listRow.getObject(3, LocalDateTime.class);
purchased = listRow.getBoolean(4);
}
}

View File

@@ -0,0 +1,18 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
public class List {
Integer itemID;
String name;
String owner;
LocalDateTime lastUpdated;
public List(ResultSet listRow) throws SQLException {
itemID = listRow.getInt(1);
name = listRow.getString(2);
owner = listRow.getString(3);
lastUpdated = listRow.getObject(8, LocalDateTime.class);
}
}

View File

@@ -1,6 +1,9 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
public class ListAdder implements CallHandler {
@@ -8,18 +11,19 @@ public class ListAdder implements CallHandler {
private DBConnector connector;
private String cognitoID;
private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)";
private final String LIST_CREATE = "INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)";
public ListAdder(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}
public String conductAction(Map<String, Object> bodyMap, String queryString) throws SQLException {
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);
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
statement.setString(2, cognitoID);
statement.setObject(3, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime());
System.out.println(statement);
statement.executeUpdate();
connection.commit();

View File

@@ -0,0 +1,12 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
public class ListGET implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, ListGetter.class);
}
}

View File

@@ -0,0 +1,39 @@
import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ListGetter implements CallHandler{
private final DBConnector connector;
private final String cognitoID;
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
public ListGetter(DBConnector connector, String cognitoID) {
this.connector = connector;
this.cognitoID = cognitoID;
}
@Override
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryMap, String cognitoID) throws SQLException {
Connection connection = connector.getConnection();
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;
} finally {
connection.close();
}
}
}

View File

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

View File

@@ -39,6 +39,16 @@
<artifactId>mariadb-java-client</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.11</maven.compiler.source>

View File

@@ -4,17 +4,19 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class BasicHandler {
public static <T extends CallHandler> String handleRequest(Map<String, Object> inputMap, Context unfilled, Class<T> toCall) {
public static <T extends CallHandler> Object handleRequest(Map<String, Object> inputMap, Context unfilled, Class<T> toCall) {
String cognitoID = InputUtils.getCognitoIDFromBody(inputMap);
HashMap<String, String> queryMap = InputUtils.getQueryParams(inputMap);
try {
DBConnector connector = new DBConnector();
try {
Constructor<T> constructor = toCall.getConstructor(DBConnector.class, String.class);
CallHandler callHandler = constructor.newInstance(connector, cognitoID);
callHandler.conductAction(InputUtils.getBody(inputMap), "");
return callHandler.conductAction(InputUtils.getBody(inputMap), queryMap, cognitoID);
} catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} finally {

View File

@@ -1,6 +1,7 @@
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public interface CallHandler {
String conductAction(Map<String, Object> bodyMap, String cognitoID) throws SQLException;
Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException;
}

View File

@@ -1,3 +1,9 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class InputUtils {
@@ -19,10 +25,24 @@ public class InputUtils {
return getMap(inputMap, "body");
}
public static String getQueryString(Map<String, Object> inputMap) {
private static String getQueryString(Map<String, Object> inputMap) {
return (String) (getMap(inputMap, "params").get("querystring"));
}
public static HashMap<String, String> getQueryParams(Map<String, Object> inputMap) {
return (HashMap<String, String>) (getMap(inputMap, "params").get("querystring"));
// String queryString = getQueryString(inputMap);
// List<NameValuePair> queryparams = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8);
// HashMap<String, String> mappedParams = new HashMap<>();
// for (NameValuePair param : queryparams) {
// mappedParams.put(param.getName(), param.getValue());
// }
// return mappedParams;
}
public static Map<String, Object> getMap(Map<String, Object> parentMap, String childKey) {
if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map<?, ?>)) {
return ((Map<String, Object>) parentMap.get(childKey));