mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2026-03-10 10:45:04 +00:00
Merge branch 'master' into list-renaming
This commit is contained in:
@@ -9,18 +9,16 @@ public class List {
|
||||
long lastUpdated;
|
||||
ArrayList<ItemEntry> entries;
|
||||
boolean shared;
|
||||
Integer uiPosition;
|
||||
|
||||
public List(ResultSet listRow, boolean shared) throws SQLException {
|
||||
public List(ResultSet listRow, boolean shared, Integer uiPosition) throws SQLException {
|
||||
listID = listRow.getInt("listID");
|
||||
name = listRow.getString("name");
|
||||
owner = listRow.getString("owner");
|
||||
lastUpdated = listRow.getTimestamp("lastUpdated").toInstant().toEpochMilli();
|
||||
entries = new ArrayList<>();
|
||||
this.shared = shared;
|
||||
}
|
||||
|
||||
public void addItemEntry(ItemEntry entry) {
|
||||
entries.add(entry);
|
||||
this.uiPosition = uiPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,9 +29,12 @@ public class List {
|
||||
", owner='" + owner + '\'' +
|
||||
", lastUpdated=" + lastUpdated +
|
||||
", entries=" + entries +
|
||||
", shared=" + shared +
|
||||
", uiPosition=" + uiPosition +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
public ItemEntry[] getEntries() {
|
||||
return entries.toArray(new ItemEntry[entries.size()]);
|
||||
}
|
||||
@@ -77,4 +78,19 @@ public class List {
|
||||
public void setShared(boolean shared) {
|
||||
this.shared = shared;
|
||||
}
|
||||
|
||||
public Integer getUiPosition() {
|
||||
return uiPosition;
|
||||
}
|
||||
|
||||
public void setUiPosition(Integer uiPosition) {
|
||||
this.uiPosition = uiPosition;
|
||||
|
||||
public ItemEntry[] getEntries() {
|
||||
return entries.toArray(new ItemEntry[entries.size()]);
|
||||
}
|
||||
|
||||
public void addItemEntry(ItemEntry entry) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ public class ListAdder implements CallHandler {
|
||||
private String cognitoID;
|
||||
|
||||
private final String LIST_CREATE = "INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?);";
|
||||
private final String LIST_ACCESS_GRANT = "INSERT INTO ListSharee(listID, userID, permissionLevel) VALUES(?, ?, ?);";
|
||||
private final String LIST_ACCESS_GRANT = "INSERT INTO ListSharee(listID, userID, permissionLevel, uiPosition) VALUES(?, ?, ?, ?);";
|
||||
private final String UI_POSITION_CHECK = "SELECT Max(uiPosition) as maxUIPosition FROM ListSharee WHERE userID = ?;";
|
||||
|
||||
public ListAdder(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
@@ -17,9 +18,17 @@ public class ListAdder implements CallHandler {
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
|
||||
|
||||
String listName = bodyMap.get("name").toString();//Needs safe checking
|
||||
|
||||
PreparedStatement uiPositionCheck = connection.prepareStatement(UI_POSITION_CHECK);
|
||||
uiPositionCheck.setString(1, cognitoID);
|
||||
ResultSet uiPositionCheckRS = uiPositionCheck.executeQuery();
|
||||
int nextPosition = 1;
|
||||
if (uiPositionCheckRS.next()) {
|
||||
nextPosition = uiPositionCheckRS.getInt("maxUIPosition") + 1;
|
||||
}
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
|
||||
statement.setString(1, listName);
|
||||
statement.setString(2, cognitoID);
|
||||
statement.setTimestamp(3, Timestamp.from(Instant.now()));
|
||||
@@ -32,6 +41,7 @@ public class ListAdder implements CallHandler {
|
||||
accessGrant.setInt(1, newID);
|
||||
accessGrant.setString(2, cognitoID);
|
||||
accessGrant.setInt(3, ListPermissions.getAll());
|
||||
accessGrant.setInt(4, nextPosition);
|
||||
System.out.println(accessGrant);
|
||||
accessGrant.executeUpdate();
|
||||
connection.commit();
|
||||
|
||||
@@ -12,7 +12,7 @@ public class ListGetter implements CallHandler{
|
||||
private final String cognitoID;
|
||||
|
||||
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
|
||||
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ?;";
|
||||
private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ? ORDER BY uiPosition;";
|
||||
private final String SHARE_CHECK = "SELECT * FROM ListSharee WHERE listID = ?;";
|
||||
private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;";
|
||||
|
||||
@@ -42,6 +42,7 @@ public class ListGetter implements CallHandler{
|
||||
ResultSet accessResults = checkAccess.executeQuery();
|
||||
int sharees = 0;
|
||||
boolean verifiedAccess = false;
|
||||
int uiPosition = 1;
|
||||
while ((sharees < 2 && accessResults.next()) || !verifiedAccess) {
|
||||
int permissionLevel = accessResults.getInt("permissionLevel");
|
||||
if (accessResults.getString("userID").equals(cognitoID)) {
|
||||
@@ -49,6 +50,7 @@ public class ListGetter implements CallHandler{
|
||||
if (!ListPermissions.hasPermission(permissionLevel, "Read")) {
|
||||
throw new AccessControlException("User " + cognitoID + " does not have permission to read list " + id);
|
||||
}
|
||||
uiPosition = accessResults.getInt("uiPosition");
|
||||
}
|
||||
if (permissionLevel > 0) {
|
||||
sharees++;
|
||||
@@ -64,7 +66,7 @@ public class ListGetter implements CallHandler{
|
||||
ResultSet getListResults = getList.executeQuery();
|
||||
getListResults.first();
|
||||
System.out.println(getListResults);
|
||||
List retrievedList = new List(getListResults, shared);
|
||||
List retrievedList = new List(getListResults, shared, uiPosition);
|
||||
System.out.println(retrievedList);
|
||||
PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES);
|
||||
getListEntries.setInt(1, id);
|
||||
|
||||
70
Lambdas/Lists/ListReposition/src/ListRepositionActor.java
Normal file
70
Lambdas/Lists/ListReposition/src/ListRepositionActor.java
Normal file
@@ -0,0 +1,70 @@
|
||||
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 ListRepositionActor implements CallHandler {
|
||||
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
public ListRepositionActor(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
final private String GET_PRIOR_POSITION = "SELECT uiPosition FROM ListSharee WHERE userID = ? AND listID = ?;";
|
||||
final private String SET_NEW_POSITION = "UPDATE ListSharee SET uiPosition = ? WHERE userID = ? AND listID = ?;";
|
||||
final private String DECREMENT_HIGHER_POSITIONS = "UPDATE ListSharee SET uiPosition = uiPosition - 1 WHERE uiPosition > ? AND userID = ?;";
|
||||
final private String INCREMENT_GEQ_POSITIONS = "UPDATE ListSharee SET uiPosition = uiPosition + 1 WHERE uiPosition >= ? AND userID = ?;";
|
||||
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
Integer listID = (Integer) bodyMap.get("listID");
|
||||
Integer newPosition = (Integer) bodyMap.get("newPosition");
|
||||
|
||||
PreparedStatement getPriorPosition = connection.prepareStatement(GET_PRIOR_POSITION);
|
||||
getPriorPosition.setString(1, cognitoID);
|
||||
getPriorPosition.setInt(2, listID);
|
||||
ResultSet priorPositionRS = getPriorPosition.executeQuery();
|
||||
if (!priorPositionRS.next()) {
|
||||
throw new IllegalArgumentException("Bad listID for user");
|
||||
}
|
||||
Integer priorPosition = priorPositionRS.getInt("uiPosition");
|
||||
|
||||
PreparedStatement openNewPosition = connection.prepareStatement(INCREMENT_GEQ_POSITIONS);
|
||||
|
||||
if (newPosition.equals(priorPosition)) {
|
||||
return null;
|
||||
}
|
||||
if (newPosition < priorPosition) {
|
||||
openNewPosition.setInt(1, newPosition);
|
||||
} else {
|
||||
openNewPosition.setInt(1, newPosition + 1);
|
||||
}
|
||||
openNewPosition.setString(2, cognitoID);
|
||||
System.out.println(openNewPosition);
|
||||
openNewPosition.executeUpdate();
|
||||
|
||||
PreparedStatement fillPriorPosition = connection.prepareStatement(DECREMENT_HIGHER_POSITIONS);
|
||||
fillPriorPosition.setInt(1, priorPosition);
|
||||
fillPriorPosition.setString(2, cognitoID);
|
||||
System.out.println(fillPriorPosition);
|
||||
fillPriorPosition.executeUpdate();
|
||||
|
||||
|
||||
|
||||
PreparedStatement setNewPosition = connection.prepareStatement(SET_NEW_POSITION);
|
||||
setNewPosition.setInt(1, newPosition);
|
||||
setNewPosition.setString(2, cognitoID);
|
||||
setNewPosition.setInt(3, listID);
|
||||
System.out.println(setNewPosition);
|
||||
setNewPosition.executeUpdate();
|
||||
|
||||
connection.commit();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
11
Lambdas/Lists/ListReposition/src/ListRepositionPUT.java
Normal file
11
Lambdas/Lists/ListReposition/src/ListRepositionPUT.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 ListRepositionPUT implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, ListRepositionActor.class);
|
||||
}
|
||||
}
|
||||
37
Lambdas/Lists/ListShare/src/ListShare.java
Normal file
37
Lambdas/Lists/ListShare/src/ListShare.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.example.listify.data;
|
||||
|
||||
public class ListShare {
|
||||
Integer listID;
|
||||
String shareWithEmail;
|
||||
ArrayList<ListShare> 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);
|
||||
}
|
||||
}
|
||||
12
Lambdas/Lists/ListShare/src/ListShareDELETE.java
Normal file
12
Lambdas/Lists/ListShare/src/ListShareDELETE.java
Normal file
@@ -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<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, ListShareDeleter.class);
|
||||
}
|
||||
|
||||
}
|
||||
65
Lambdas/Lists/ListShare/src/ListShareDeleter.java
Normal file
65
Lambdas/Lists/ListShare/src/ListShareDeleter.java
Normal file
@@ -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<String, Object> bodyMap, HashMap<String, String> 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;
|
||||
}
|
||||
}
|
||||
11
Lambdas/Lists/ListShare/src/ListShareGET.java
Normal file
11
Lambdas/Lists/ListShare/src/ListShareGET.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 ListShareGET implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, ListShareGetter.class);
|
||||
}
|
||||
}
|
||||
40
Lambdas/Lists/ListShare/src/ListShareGetter.java
Normal file
40
Lambdas/Lists/ListShare/src/ListShareGetter.java
Normal file
@@ -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<String, Object> bodyMap, HashMap<String, String> 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;
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,9 @@ public class ListSharer implements CallHandler {
|
||||
}
|
||||
|
||||
final private String CHECK_ACCESS = "SELECT * from ListSharee WHERE listID = ? AND userID = ?;";
|
||||
private final String UI_POSITION_CHECK = "SELECT Max(uiPosition) as maxUIPosition FROM ListSharee WHERE userID = ?;";
|
||||
final private String SHARE_LIST = "INSERT INTO ListSharee(listID, userID, permissionLevel, uiPosition) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE permissionLevel = ?;";
|
||||
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
PreparedStatement checkAccess = connection.prepareStatement(CHECK_ACCESS);
|
||||
Integer listID = Integer.parseInt(bodyMap.get("listID").toString());
|
||||
@@ -63,12 +63,23 @@ public class ListSharer implements CallHandler {
|
||||
// throw new InputMismatchException("The specified user already has access");
|
||||
// }
|
||||
|
||||
PreparedStatement uiPositionCheck = connection.prepareStatement(UI_POSITION_CHECK);
|
||||
uiPositionCheck.setString(1, shareWithSub);
|
||||
System.out.println(uiPositionCheck);
|
||||
ResultSet uiPositionCheckRS = uiPositionCheck.executeQuery();
|
||||
int nextPosition = 1;
|
||||
if (uiPositionCheckRS.next()) {
|
||||
nextPosition = uiPositionCheckRS.getInt("maxUIPosition") + 1;
|
||||
}
|
||||
|
||||
PreparedStatement shareList = connection.prepareStatement(SHARE_LIST);
|
||||
shareList.setInt(1, listID);
|
||||
shareList.setString(2, shareWithSub);
|
||||
Integer permissionLevel = Integer.parseInt(bodyMap.get("permissionLevel").toString());
|
||||
shareList.setInt(3, permissionLevel);
|
||||
shareList.setInt(4, permissionLevel);
|
||||
shareList.setInt(4, nextPosition);
|
||||
shareList.setInt(5, permissionLevel);
|
||||
System.out.println(shareList);
|
||||
shareList.executeUpdate();
|
||||
connection.commit();
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user