mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2026-03-10 18:55:03 +00:00
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:
52
Lambdas/Lists/Item/src/Item.java
Normal file
52
Lambdas/Lists/Item/src/Item.java
Normal 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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user