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:
NMerz 2020-10-03 11:22:35 -04:00
parent c0c2d599f8
commit 69572b3a1f
12 changed files with 115 additions and 34 deletions

View 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;
}
}

View 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);
}
}

View 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;
}
}

View 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);
}
}

View File

@ -3,19 +3,19 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
public class ListAdder {
public class ListAdder implements CallHandler {
private DBConnector connector;
private String cognitoID;
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.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();
PreparedStatement statement = connection.prepareStatement(LIST_CREATE);
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
@ -23,5 +23,6 @@ public class ListAdder {
System.out.println(statement);
statement.executeUpdate();
connection.commit();
return null;
}
}

View 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);
}
}

View 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;
}
}

View 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;
}

View File

@ -12,11 +12,11 @@ public class DBConnector {
Connection connection;
DBConnector() throws IOException, SQLException, ClassNotFoundException {
public DBConnector() throws IOException, SQLException, ClassNotFoundException {
this(loadProperties("dbProperties.json"));
}
DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
public DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println(dbProperties);
System.out.println(DBConnector.buildURL(dbProperties));

View File

@ -19,6 +19,10 @@ public class InputUtils {
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) {
if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map<?, ?>)) {
return ((Map<String, Object>) parentMap.get(childKey));

View File

@ -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;
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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" />
</component>
<component name="ProjectType">