mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-13 09:48:47 +00:00
Add list duplication lambdas
This commit is contained in:
parent
1fe04e038b
commit
27f0d74422
11
Lambdas/Lists/ListDuplicate/src/ListDuplicatePOST.java
Normal file
11
Lambdas/Lists/ListDuplicate/src/ListDuplicatePOST.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 ListDuplicatePOST implements RequestHandler<Map<String,Object>, Object>{
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, ListDuplicater.class);
|
||||
}
|
||||
}
|
||||
73
Lambdas/Lists/ListDuplicate/src/ListDuplicater.java
Normal file
73
Lambdas/Lists/ListDuplicate/src/ListDuplicater.java
Normal file
@ -0,0 +1,73 @@
|
||||
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
|
||||
import com.amazonaws.services.lambda.model.InvokeRequest;
|
||||
import com.amazonaws.services.lambda.model.InvokeResult;
|
||||
|
||||
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.InputMismatchException;
|
||||
import java.util.Map;
|
||||
|
||||
public class ListDuplicater implements CallHandler {
|
||||
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
private final String LIST_POPULATE = "INSERT INTO ListProduct(listID, productID, quantity, addedDate, purchased) SELECT ?, productID, quantity, addedDate, purchased FROM ListProduct WHERE listID = ?;";
|
||||
private final String ACCESS_CHECK = "SELECT * from ListSharee WHERE userID = ? and listID = ?;";
|
||||
|
||||
public ListDuplicater(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
String listName = bodyMap.get("name").toString();//Needs safe checking
|
||||
Integer copyListID = (Integer) bodyMap.get("listID");
|
||||
|
||||
PreparedStatement accessCheck = connection.prepareStatement(ACCESS_CHECK);
|
||||
accessCheck.setString(1, cognitoID);
|
||||
accessCheck.setInt(2, copyListID);
|
||||
ResultSet access = accessCheck.executeQuery();
|
||||
if (access.next()) {
|
||||
if (!ListPermissions.hasPermission(access.getInt("permissionLevel"), "Read")) {
|
||||
throw new AccessControlException("User " + cognitoID + " does not have write permissions for list " + copyListID);
|
||||
}
|
||||
} else {
|
||||
throw new AccessControlException("User " + cognitoID + " does not have any permissions to access list " + copyListID);
|
||||
}
|
||||
|
||||
|
||||
InvokeRequest invokeRequest = new InvokeRequest();
|
||||
invokeRequest.setFunctionName("ListPOST");
|
||||
invokeRequest.setPayload("{" +
|
||||
" \"body\": {" +
|
||||
" \"name\": \"" + listName + "\"" +
|
||||
" }," +
|
||||
" \"params\": {" +
|
||||
" \"querystring\": {" +
|
||||
" }" +
|
||||
" }," +
|
||||
" \"context\": {" +
|
||||
" \"sub\": \"" + cognitoID + "\"" +
|
||||
" }" +
|
||||
"}");
|
||||
System.out.println(invokeRequest);
|
||||
InvokeResult listCreateResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
|
||||
if (listCreateResult.getStatusCode() != 200) {
|
||||
throw new InputMismatchException("Could not find specified user to share with");
|
||||
}
|
||||
Integer newListID = Integer.parseInt(new String(listCreateResult.getPayload().array()).replace("\"", ""));
|
||||
|
||||
PreparedStatement populateList = connection.prepareStatement(LIST_POPULATE);
|
||||
populateList.setInt(1, newListID);
|
||||
populateList.setInt(2, copyListID);
|
||||
System.out.println(populateList);
|
||||
populateList.executeUpdate();
|
||||
connection.commit();
|
||||
return newListID;
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,7 @@ import androidx.navigation.ui.AppBarConfiguration;
|
||||
import androidx.navigation.ui.NavigationUI;
|
||||
import com.amplifyframework.auth.AuthException;
|
||||
import com.example.listify.data.List;
|
||||
import com.example.listify.data.ListDuplicate;
|
||||
import com.example.listify.data.ListReposition;
|
||||
import com.example.listify.data.SearchHistory;
|
||||
import com.example.listify.ui.LoginPage;
|
||||
@ -110,6 +111,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
|
||||
try {
|
||||
System.out.println(historyReceiver.await());
|
||||
requestor.putObject(new ListReposition(291, 1));
|
||||
requestor.postObject(new ListDuplicate(290, "yet another list"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
package com.example.listify.data;
|
||||
|
||||
// Info on List to copy
|
||||
public class ListDuplicate {
|
||||
Integer listID;
|
||||
String name;
|
||||
|
||||
public ListDuplicate(Integer listID, String name) {
|
||||
this.listID = listID;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListDuplicate{" +
|
||||
"listID=" + listID +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Integer getListID() {
|
||||
return listID;
|
||||
}
|
||||
|
||||
public void setListID(Integer listID) {
|
||||
this.listID = listID;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user