mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 02:38:47 +00:00
Generify endpoint groups as IntelliJ Modules
This allows them to be built sperately decreasing JAR size and also creates a better structure for generic service functions and business logic.
This commit is contained in:
parent
c0c2d599f8
commit
69572b3a1f
27
Lambdas/Lists/Item/src/ItemAdder.java
Normal file
27
Lambdas/Lists/Item/src/ItemAdder.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemAdder implements CallHandler {
|
||||||
|
|
||||||
|
private DBConnector connector;
|
||||||
|
private String cognitoID;
|
||||||
|
|
||||||
|
private final String LIST_CREATE = "INSERT INTO Items (Name) VALUES (?)";
|
||||||
|
|
||||||
|
public ItemAdder(DBConnector connector, String cognitoID) {
|
||||||
|
this.connector = connector;
|
||||||
|
this.cognitoID = cognitoID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String conductAction(Map<String, Object> bodyMap, String queryString) 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();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Lambdas/Lists/Item/src/ItemGET.java
Normal file
11
Lambdas/Lists/Item/src/ItemGET.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemGET implements RequestHandler<Map<String,Object>, String> {
|
||||||
|
|
||||||
|
public String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||||
|
return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Lambdas/Lists/Item/src/ItemGetter.java
Normal file
9
Lambdas/Lists/Item/src/ItemGetter.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemGetter implements CallHandler{
|
||||||
|
@Override
|
||||||
|
public String conductAction(Map<String, Object> bodyMap, String cognitoID) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Lambdas/Lists/Item/src/ItemPOST.java
Normal file
11
Lambdas/Lists/Item/src/ItemPOST.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemPOST implements RequestHandler<Map<String,Object>, String>{
|
||||||
|
|
||||||
|
public String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||||
|
return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,19 +3,19 @@ import java.sql.PreparedStatement;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ListAdder {
|
public class ListAdder implements CallHandler {
|
||||||
|
|
||||||
private DBConnector connector;
|
private DBConnector connector;
|
||||||
private String cognitoID;
|
private String cognitoID;
|
||||||
|
|
||||||
private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)";
|
private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)";
|
||||||
|
|
||||||
ListAdder(DBConnector connector, String cognitoID) {
|
public ListAdder(DBConnector connector, String cognitoID) {
|
||||||
this.connector = connector;
|
this.connector = connector;
|
||||||
this.cognitoID = cognitoID;
|
this.cognitoID = cognitoID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Map<String, Object> bodyMap) throws SQLException {
|
public String conductAction(Map<String, Object> bodyMap, String queryString) throws SQLException {
|
||||||
Connection connection = connector.getConnection();
|
Connection connection = connector.getConnection();
|
||||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE);
|
PreparedStatement statement = connection.prepareStatement(LIST_CREATE);
|
||||||
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
|
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
|
||||||
@ -23,5 +23,6 @@ public class ListAdder {
|
|||||||
System.out.println(statement);
|
System.out.println(statement);
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
connection.commit();
|
connection.commit();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
Lambdas/Lists/List/src/ListPOST.java
Normal file
11
Lambdas/Lists/List/src/ListPOST.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
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 String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||||
|
return BasicHandler.handleRequest(inputMap, unfilled, ListAdder.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
Lambdas/Lists/src/main/java/BasicHandler.java
Normal file
29
Lambdas/Lists/src/main/java/BasicHandler.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class BasicHandler {
|
||||||
|
public static <T extends CallHandler> String handleRequest(Map<String, Object> inputMap, Context unfilled, Class<T> toCall) {
|
||||||
|
String cognitoID = InputUtils.getCognitoIDFromBody(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), "");
|
||||||
|
} catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
connector.close();
|
||||||
|
}
|
||||||
|
} catch (IOException |SQLException|ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException(e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
Lambdas/Lists/src/main/java/CallHandler.java
Normal file
6
Lambdas/Lists/src/main/java/CallHandler.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface CallHandler {
|
||||||
|
String conductAction(Map<String, Object> bodyMap, String cognitoID) throws SQLException;
|
||||||
|
}
|
||||||
@ -12,11 +12,11 @@ public class DBConnector {
|
|||||||
|
|
||||||
Connection connection;
|
Connection connection;
|
||||||
|
|
||||||
DBConnector() throws IOException, SQLException, ClassNotFoundException {
|
public DBConnector() throws IOException, SQLException, ClassNotFoundException {
|
||||||
this(loadProperties("dbProperties.json"));
|
this(loadProperties("dbProperties.json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
|
public DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
|
||||||
Class.forName("org.mariadb.jdbc.Driver");
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
System.out.println(dbProperties);
|
System.out.println(dbProperties);
|
||||||
System.out.println(DBConnector.buildURL(dbProperties));
|
System.out.println(DBConnector.buildURL(dbProperties));
|
||||||
|
|||||||
@ -19,6 +19,10 @@ public class InputUtils {
|
|||||||
return getMap(inputMap, "body");
|
return getMap(inputMap, "body");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getQueryString(Map<String, Object> inputMap) {
|
||||||
|
return (String) (getMap(inputMap, "params").get("querystring"));
|
||||||
|
}
|
||||||
|
|
||||||
public static Map<String, Object> getMap(Map<String, Object> parentMap, String childKey) {
|
public static Map<String, Object> getMap(Map<String, Object> parentMap, String childKey) {
|
||||||
if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map<?, ?>)) {
|
if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map<?, ?>)) {
|
||||||
return ((Map<String, Object>) parentMap.get(childKey));
|
return ((Map<String, Object>) parentMap.get(childKey));
|
||||||
|
|||||||
@ -1,28 +0,0 @@
|
|||||||
import java.io.IOException;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
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 String handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
|
||||||
String cognitoID = InputUtils.getCognitoIDFromBody(inputMap);
|
|
||||||
try {
|
|
||||||
DBConnector connector = new DBConnector();
|
|
||||||
try {
|
|
||||||
new ListAdder(connector, cognitoID).add(InputUtils.getBody(inputMap));
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
connector.close();
|
|
||||||
}
|
|
||||||
} catch (IOException|SQLException|ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException(e.getMessage());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user