From 8233a989efed3f89723af65d70313f940bc627bf Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 31 Oct 2020 14:54:42 -0400 Subject: [PATCH] ListDELETE lambda List deletion now working Integration tested independently. Still needs tests and user deletion should now use this. --- Lambdas/Lists/List/src/ListDELETE.java | 12 ++++++ Lambdas/Lists/List/src/ListDeleter.java | 50 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Lambdas/Lists/List/src/ListDELETE.java create mode 100644 Lambdas/Lists/List/src/ListDeleter.java diff --git a/Lambdas/Lists/List/src/ListDELETE.java b/Lambdas/Lists/List/src/ListDELETE.java new file mode 100644 index 0000000..d202b8d --- /dev/null +++ b/Lambdas/Lists/List/src/ListDELETE.java @@ -0,0 +1,12 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ListDELETE implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListDeleter.class); + } + +} diff --git a/Lambdas/Lists/List/src/ListDeleter.java b/Lambdas/Lists/List/src/ListDeleter.java new file mode 100644 index 0000000..0b71411 --- /dev/null +++ b/Lambdas/Lists/List/src/ListDeleter.java @@ -0,0 +1,50 @@ +import java.security.AccessControlException; +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 ListDeleter implements CallHandler { + private final Connection connection; + private final String cognitoID; + + private final String GET_LISTS = "SELECT * FROM List WHERE (owner = ? AND listID = ?);"; + private final String DELETE_LIST = "DELETE FROM List WHERE listID = ?;"; + private final String DELETE_LIST_ACCESS = "DELETE FROM ListSharee where listID = ?;"; + private final String DELETE_LIST_ENTRIES = "DELETE FROM ListProduct where listID = ?;"; + + public ListDeleter(Connection connection, String cognitoID) { + this.connection = connection; + this.cognitoID = cognitoID; + } + + @Override + public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { + Integer listID = Integer.parseInt(queryMap.get("id")); + PreparedStatement accessCheck = connection.prepareStatement(GET_LISTS); + accessCheck.setString(1, cognitoID); + accessCheck.setInt(2, listID); + System.out.println(accessCheck); + ResultSet userLists = accessCheck.executeQuery(); + + if (!userLists.next()) { + throw new AccessControlException("User does not have access to list"); + } + PreparedStatement cleanAccess = connection.prepareStatement(DELETE_LIST_ACCESS); + cleanAccess.setInt(1, listID); + System.out.println(cleanAccess); + cleanAccess.executeUpdate(); + PreparedStatement deleteEntries = connection.prepareStatement(DELETE_LIST_ENTRIES); + deleteEntries.setInt(1, listID); + System.out.println(deleteEntries); + deleteEntries.executeUpdate(); + PreparedStatement cleanList = connection.prepareStatement(DELETE_LIST); + cleanList.setInt(1, listID); + System.out.println(cleanList); + cleanList.executeUpdate(); + connection.commit(); + return null; + } +}