diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index 02d9723..83b8274 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -19,10 +19,6 @@ public class List { this.shared = shared; } - public void addItemEntry(ItemEntry entry) { - entries.add(entry); - } - @Override public String toString() { return "List{" + @@ -34,10 +30,6 @@ public class List { '}'; } - public ItemEntry[] getEntries() { - return entries.toArray(new ItemEntry[entries.size()]); - } - public Integer getItemID() { return itemID; } @@ -77,4 +69,12 @@ public class List { public void setShared(boolean shared) { this.shared = shared; } + + public ItemEntry[] getEntries() { + return entries.toArray(new ItemEntry[entries.size()]); + } + + public void addItemEntry(ItemEntry entry) { + entries.add(entry); + } } diff --git a/Lambdas/Lists/ListShare/src/ListShare.java b/Lambdas/Lists/ListShare/src/ListShare.java new file mode 100644 index 0000000..151d842 --- /dev/null +++ b/Lambdas/Lists/ListShare/src/ListShare.java @@ -0,0 +1,37 @@ +package com.example.listify.data; + +public class ListShare { + Integer listID; + String shareWithEmail; + ArrayList other; + + public ListShare(ResultSet listRow) throws SQLException { + this.listID = listRow.getInt("listID"); + this.shareWithEmail = listRow.getString("userID"); + other = new ArrayList<>(); + } + + public Integer getListID() { + return listID; + } + + public void setListID(Integer listID) { + this.listID = listID; + } + + public String getShareWithEmail() { + return shareWithEmail; + } + + public void setShareWithEmail(String shareWithEmail) { + this.shareWithEmail = shareWithEmail; + } + + public ListShare[] getEntries() { + return other.toArray(new ListShare[other.size()]); + } + + public void addtoList(ListShare entry) { + other.add(entry); + } +} diff --git a/Lambdas/Lists/ListShare/src/ListShareDELETE.java b/Lambdas/Lists/ListShare/src/ListShareDELETE.java new file mode 100644 index 0000000..784a725 --- /dev/null +++ b/Lambdas/Lists/ListShare/src/ListShareDELETE.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 ListShareDELETE implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListShareDeleter.class); + } + +} diff --git a/Lambdas/Lists/ListShare/src/ListShareDeleter.java b/Lambdas/Lists/ListShare/src/ListShareDeleter.java new file mode 100644 index 0000000..7ec0076 --- /dev/null +++ b/Lambdas/Lists/ListShare/src/ListShareDeleter.java @@ -0,0 +1,65 @@ +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 ListShareDeleter implements CallHandler { + private final Connection connection; + private final String cognitoID; + + private final String GET_LIST_ACCESS = "SELECT * FROM List WHERE (owner = ? AND listID = ?);"; + private final String REMOVE_SHAREE = "DELETE FROM ListSharee WHERE listID = ? AND user = ?;"; + + public ListShareDeleter(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")); + + InvokeRequest invokeRequest = new InvokeRequest(); + invokeRequest.setFunctionName("UserGET"); + invokeRequest.setPayload("{" + + " \"body\": {" + + " \"emailToCheck\": \"" + bodyMap.get("shareWithEmail").toString() + "\"" + + " }," + + " \"params\": {" + + " \"querystring\": {" + + " }" + + " }," + + " \"context\": {" + + " \"sub\": \"not used\"" + + " }" + + "}"); + InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest); + + String shareeID = new String(invokeResult.getPayload().array()).replace("\"", ""); + + //Ensure that the user who is unsharing a list is the owner of that list + PreparedStatement accessCheck = connection.prepareStatement(GET_LIST_ACCESS); + accessCheck.setString(1, cognitoID); + accessCheck.setInt(2, listID); + + ResultSet userLists = accessCheck.executeQuery(); + + //User does not own the list; unshare attempt fails + if (!userLists.next()) { + throw new AccessControlException("User does not have access to list"); + } + + //Unshare the list with the specified sharee + PreparedStatement unshareList = connection.prepareStatement(REMOVE_SHAREE); + unshareList.setInt(1, listID); + unshareList.setInt(2, shareeID); + + cleanAccess.executeUpdate(); + connection.commit(); + + return null; + } +} diff --git a/Lambdas/Lists/ListShare/src/ListShareGET.java b/Lambdas/Lists/ListShare/src/ListShareGET.java new file mode 100644 index 0000000..d59ffcf --- /dev/null +++ b/Lambdas/Lists/ListShare/src/ListShareGET.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ListShareGET implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListShareGetter.class); + } +} diff --git a/Lambdas/Lists/ListShare/src/ListShareGetter.java b/Lambdas/Lists/ListShare/src/ListShareGetter.java new file mode 100644 index 0000000..009f0d6 --- /dev/null +++ b/Lambdas/Lists/ListShare/src/ListShareGetter.java @@ -0,0 +1,40 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class ListShareGetter implements CallHandler{ + private final Connection connection; + private final String cognitoID; + + private final String GET_LISTS = "SELECT * FROM ListSharee WHERE listID = ?;"; + + public ListShareGetter(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 getList = connection.prepareStatement(GET_LIST); + getList.setInt(1, listID); + + ResultSet getListResults = getList.executeQuery(); + getListResults.first(); + + //ListShare object to hold the data values of the first row retrived + ListShare first = new ListShare(getListResults); + + //Insert the ListShare objects to hold the data of the remaining rows into first's ListShare list + while (getListResults.next()) { + first.addtoList(new ListShare(getListResults)); + } + + return first; + } +} diff --git a/Listify/app/src/main/java/com/example/listify/data/List.java b/Listify/app/src/main/java/com/example/listify/data/List.java index e04211f..6a3fab0 100644 --- a/Listify/app/src/main/java/com/example/listify/data/List.java +++ b/Listify/app/src/main/java/com/example/listify/data/List.java @@ -67,10 +67,6 @@ public class List { this.lastUpdated = lastUpdated; } - public ListEntry[] getEntries() { - return entries; - } - public boolean isShared() { return shared; } @@ -78,4 +74,8 @@ public class List { public void setShared(boolean shared) { this.shared = shared; } + + public ListEntry[] getEntries() { + return entries; + } } diff --git a/Listify/app/src/main/java/com/example/listify/data/ListShare.java b/Listify/app/src/main/java/com/example/listify/data/ListShare.java index 406f286..df984ee 100644 --- a/Listify/app/src/main/java/com/example/listify/data/ListShare.java +++ b/Listify/app/src/main/java/com/example/listify/data/ListShare.java @@ -3,10 +3,18 @@ package com.example.listify.data; public class ListShare { Integer listID; String shareWithEmail; + final ListShare[] other; + + public ListShare(Integer listID, String shareWithEmail, ListShare[] other) { + this.listID = listID; + this.shareWithEmail = shareWithEmail; + this.other = other; + } public ListShare(Integer listID, String shareWithEmail) { this.listID = listID; this.shareWithEmail = shareWithEmail; + this.other = null; } public Integer getListID() { @@ -24,4 +32,8 @@ public class ListShare { public void setShareWithEmail(String shareWithEmail) { this.shareWithEmail = shareWithEmail; } + + public ListShare[] getEntries() { + return other; + } }