Merge branch 'search-history' into sharing-permissions

This commit is contained in:
NMerz 2020-11-14 14:32:27 -05:00
commit 9de8f425c7
10 changed files with 210 additions and 8 deletions

View File

@ -1,3 +1,6 @@
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvokeRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -18,13 +21,30 @@ public class ItemSearcher implements CallHandler {
}
@Override
public Object conductAction(Map<String, Object> body, HashMap<String, String> queryParams, String s) throws SQLException {
public Object conductAction(Map<String, Object> body, HashMap<String, String> queryParams, String cognitoID) throws SQLException {
PreparedStatement getItemMatches = connection.prepareStatement(GET_ITEM_MATCHES);
getItemMatches.setString(1, "%" + queryParams.get("id") + "%");
System.out.println(getItemMatches);
ResultSet searchResults = getItemMatches.executeQuery();
ItemSearch searchResultsObject = new ItemSearch(searchResults);
System.out.println(searchResultsObject);
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("SearchHistoryPOST");
invokeRequest.setPayload("{" +
" \"body\": {" +
" \"searchTerm\": \"" + queryParams.get("id") + "\"" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"" + cognitoID + "\"" +
" }" +
"}");
invokeRequest.setInvocationType("Event");
System.out.println(invokeRequest);
AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
return searchResultsObject;
}
}

View File

@ -0,0 +1,43 @@
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class SearchHistory implements Serializable {
ArrayList<String> searches;
public SearchHistory(ArrayList<String> searches) {
this.searches = searches;
}
public SearchHistory() {
this.searches = new ArrayList<>();
}
public SearchHistory(ResultSet row) throws SQLException {
this.searches = new ArrayList<>();
row.beforeFirst();
while (row.next()) {
this.searches.add(row.getString("search"));
}
}
public ArrayList<String> getSearches() {
return searches;
}
public void setSearches(ArrayList<String> searches) {
this.searches = searches;
}
public void addSearch(String newSearch) {
searches.add(newSearch);
}
@Override
public String toString() {
return "SearchHistory{" +
"searches=" + searches +
'}';
}
}

View 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 SearchHistoryGET implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryGetter.class);
}
}

View File

@ -0,0 +1,31 @@
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 SearchHistoryGetter implements CallHandler {
private Connection connection;
private String cognitoID;
public SearchHistoryGetter(Connection connection, String cognitoID) {
this.connection = connection;
this.cognitoID = cognitoID;
}
final private String SELECT_HISTORY = "SELECT * from SearchHistory WHERE userID = ?;";
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
PreparedStatement select_history = connection.prepareStatement(SELECT_HISTORY);
select_history.setString(1, cognitoID);
System.out.println(select_history);
ResultSet searchHistory = select_history.executeQuery();
if (!searchHistory.first()) {
return new SearchHistory();
}
System.out.println(new SearchHistory(searchHistory));
return new SearchHistory(searchHistory);
}
}

View 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 SearchHistoryPOST implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryUpdater.class);
}
}

View File

@ -0,0 +1,28 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class SearchHistoryUpdater implements CallHandler {
private Connection connection;
private String cognitoID;
public SearchHistoryUpdater(Connection connection, String cognitoID) {
this.connection = connection;
this.cognitoID = cognitoID;
}
final private String UPDATE_HISTORY = "INSERT INTO SearchHistory(userID, search) VALUES(?, ?);";
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
PreparedStatement store_history = connection.prepareStatement(UPDATE_HISTORY);
store_history.setString(1, cognitoID);
store_history.setObject(2, bodyMap.get("searchTerm"));
System.out.println(store_history);
store_history.executeUpdate();
connection.commit();
return null;
}
}

View File

@ -94,6 +94,11 @@
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

View File

@ -16,19 +16,15 @@ import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.amplifyframework.auth.AuthException;
import com.example.listify.data.Item;
import com.example.listify.data.ItemSearch;
import com.example.listify.data.List;
import com.example.listify.data.ListEntry;
import com.example.listify.data.SearchHistory;
import com.example.listify.ui.LoginPage;
import com.google.android.material.navigation.NavigationView;
import org.json.JSONException;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import static com.example.listify.SplashActivity.showSplash;
@ -108,7 +104,14 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
}
Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey"));
SynchronousReceiver<SearchHistory> historyReceiver = new SynchronousReceiver<>();
requestor.getObject("N/A", SearchHistory.class, historyReceiver, historyReceiver);
try {
System.out.println(historyReceiver.await());
} catch (Exception e) {
e.printStackTrace();
}
/*
List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli());
ListEntry entry = new ListEntry(1, 4, Math.abs(new Random().nextInt()), Instant.now().toEpochMilli(),false);
@ -139,6 +142,7 @@ public class MainActivity extends AppCompatActivity implements CreateListDialogF
} catch (Exception receiverError) {
receiverError.printStackTrace();
}
*/
}
//------------------------------------------------------------------------------------------//

View File

@ -60,6 +60,25 @@ public class Requestor {
launchCall(deleteRequest, null, classType, failureHandler);
}
public void putObject(Object toPost) throws JSONException {
putObject(toPost, (RequestErrorHandler) null);
}
public void putObject(Object toPost, RequestErrorHandler failureHandler) throws JSONException {
putObject(toPost, null, failureHandler);
}
public void putObject(Object toPost, Receiver<Integer> idReceiver) throws JSONException {
putObject(toPost, idReceiver, null);
}
public void putObject(Object toPut, Receiver<Integer> idReceiver, RequestErrorHandler failureHandler) throws JSONException {
String putURL = DEV_BASEURL + "/" + toPut.getClass().getSimpleName();
Request putRequest = buildBaseRequest(putURL, "PUT", new Gson().toJson(toPut));
launchCall(putRequest, idReceiver, Integer.class, failureHandler);
}
public void postObject(Object toPost) throws JSONException {
postObject(toPost, (RequestErrorHandler) null);
}
@ -85,7 +104,7 @@ public class Requestor {
String responseString = response.body().string();
if (receiver != null) {
if (classType == null) {
Log.e("Requestor Contract Error", "classType while receiver populated");
Log.e("Requestor Contract Error", "no/null classType while receiver populated");
}
try {
receiver.acceptDelivery(new Gson().fromJson(responseString, classType));

View File

@ -0,0 +1,30 @@
package com.example.listify.data;
import java.util.ArrayList;
public class SearchHistory {
ArrayList<String> searches;
public SearchHistory(ArrayList<String> searches) {
this.searches = searches;
}
public ArrayList<String> getSearches() {
return searches;
}
public void setSearches(ArrayList<String> searches) {
this.searches = searches;
}
public void addSearch(String newSearch) {
searches.add(newSearch);
}
@Override
public String toString() {
return "SearchHistory{" +
"searches=" + searches +
'}';
}
}