diff --git a/.gitignore b/.gitignore index 56cc642..837a882 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ captures/ .idea/assetWizardSettings.xml .idea/dictionaries .idea/libraries +*.idea* # Android Studio 3 in .gitignore file. .idea/caches .idea/modules.xml @@ -83,3 +84,7 @@ lint/generated/ lint/outputs/ lint/tmp/ # lint/reports/ +Lambdas/Lists/src/main/resources/dbProperties.json +Lambdas/Lists/target/classes/dbProperties.json +Lambdas/Lists/target/classes/META-INF/Lists.kotlin_module +Listify/app/src/main/res/raw/auths.json diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml new file mode 100644 index 0000000..8d77579 --- /dev/null +++ b/Lambdas/Lists/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + groupId + Lists + 1.0-SNAPSHOT + + + + com.amazonaws + aws-lambda-java-core + 1.2.1 + + + com.amazonaws + aws-lambda-java-events + 3.1.0 + + + com.amazonaws + aws-lambda-java-log4j2 + 1.2.0 + + + org.json + json + 20200518 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + org.mariadb.jdbc + mariadb-java-client + 2.7.0 + + + + 1.11 + 1.11 + + \ No newline at end of file diff --git a/Lambdas/Lists/src/main/java/DBConnector.java b/Lambdas/Lists/src/main/java/DBConnector.java new file mode 100644 index 0000000..074f4de --- /dev/null +++ b/Lambdas/Lists/src/main/java/DBConnector.java @@ -0,0 +1,53 @@ +import org.json.JSONObject; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class DBConnector { + + Connection connection; + + DBConnector() throws IOException, SQLException, ClassNotFoundException { + this(loadProperties("dbProperties.json")); + } + + DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException { + Class.forName("org.mariadb.jdbc.Driver"); + System.out.println(dbProperties); + System.out.println(DBConnector.buildURL(dbProperties)); + connection = DriverManager.getConnection(dbProperties.get("url").toString(), dbProperties.get("user").toString(), dbProperties.get("password").toString()); + System.out.println(connection); + } + + public void close() { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public Connection getConnection() { + return connection; + } + + public static Properties loadProperties(String path) throws IOException { + Properties toReturn = new Properties(); + String propertiesJSONString = Files.readString(Path.of(path)); + JSONObject propertiesJSON = new JSONObject(propertiesJSONString); + propertiesJSON.keys().forEachRemaining(key -> toReturn.setProperty(key, propertiesJSON.get(key).toString())); + return toReturn; + } + + public static String buildURL(Properties dbProperties) { + String dbURL = dbProperties.get("url").toString(); + dbURL += "?user=" + dbProperties.get("user").toString(); + dbURL += "&password=" + dbProperties.get("password").toString(); + return dbURL; + } +} diff --git a/Lambdas/Lists/src/main/java/InputUtils.java b/Lambdas/Lists/src/main/java/InputUtils.java new file mode 100644 index 0000000..46a0694 --- /dev/null +++ b/Lambdas/Lists/src/main/java/InputUtils.java @@ -0,0 +1,28 @@ +import java.util.Map; + +public class InputUtils { + public static String getCognitoIDFromBody(Map inputMap) { + System.out.println(inputMap.keySet()); + System.out.println(inputMap.entrySet()); + Map contextMap; + if ((inputMap.get("context") != null) && (inputMap.get("context") instanceof Map)) { + contextMap = ((Map) inputMap.get("context")); + } else { + throw new IllegalArgumentException("The key \"context\" must exist and be a map"); + } + System.out.println(inputMap.get("context")); + System.out.println(contextMap.get("sub")); + return contextMap.get("sub").toString(); + } + + public static Map getBody(Map inputMap) { + return getMap(inputMap, "body"); + } + + public static Map getMap(Map parentMap, String childKey) { + if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map)) { + return ((Map) parentMap.get(childKey)); + } + throw new IllegalArgumentException("The key \"" + childKey + "\" must exist and be a map"); + } +} diff --git a/Lambdas/Lists/src/main/java/ListAdder.java b/Lambdas/Lists/src/main/java/ListAdder.java new file mode 100644 index 0000000..e1598b7 --- /dev/null +++ b/Lambdas/Lists/src/main/java/ListAdder.java @@ -0,0 +1,27 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Map; + +public class ListAdder { + + private DBConnector connector; + private String cognitoID; + + private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)"; + + ListAdder(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + public void add(Map bodyMap) 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); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); + } +} diff --git a/Lambdas/Lists/src/main/java/ListPOST.java b/Lambdas/Lists/src/main/java/ListPOST.java new file mode 100644 index 0000000..c2fc63c --- /dev/null +++ b/Lambdas/Lists/src/main/java/ListPOST.java @@ -0,0 +1,28 @@ +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, String>{ + + + public String handleRequest(Map 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; + } +} diff --git a/Listify/.gitignore b/Listify/.gitignore index 603b140..49b1255 100644 --- a/Listify/.gitignore +++ b/Listify/.gitignore @@ -12,3 +12,19 @@ /captures .externalNativeBuild .cxx + +#amplify +amplify/\#current-cloud-backend +amplify/.config/local-* +amplify/mock-data +amplify/backend/amplify-meta.json +amplify/backend/awscloudformation +build/ +dist/ +node_modules/ +aws-exports.js +awsconfiguration.json +amplifyconfiguration.json +amplify-build-config.json +amplify-gradle-config.json +amplifytools.xcconfig \ No newline at end of file diff --git a/Listify/.idea/gradle.xml b/Listify/.idea/gradle.xml index 5cd135a..674414f 100644 --- a/Listify/.idea/gradle.xml +++ b/Listify/.idea/gradle.xml @@ -4,6 +4,7 @@