mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-15 18:28:47 +00:00
Try storing serialized search object
Does not seem to be the best solution (https://stackoverflow.com/questions/17662236/java-write-object-to-mysql-each-field-or-serialized-byte-array) and does not entirely work, but saving it for posterity because there are some interesting changes here
This commit is contained in:
parent
2d3e985ded
commit
8653b9a035
@ -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,22 @@ 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("SearchHistoryUpdate");
|
||||
invokeRequest.setPayload("{" +
|
||||
" \"newSearch\": \"" + queryParams.get("id") + "\"," +
|
||||
" \"cognitoID\": \""+ cognitoID + "\"" +
|
||||
"}");
|
||||
invokeRequest.setInvocationType("Event");
|
||||
System.out.println(invokeRequest);
|
||||
AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
|
||||
return searchResultsObject;
|
||||
}
|
||||
}
|
||||
|
||||
39
Lambdas/Lists/SearchHistory/src/SearchHistory.java
Normal file
39
Lambdas/Lists/SearchHistory/src/SearchHistory.java
Normal file
@ -0,0 +1,39 @@
|
||||
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 = (ArrayList<String>) row.getObject("historyObject");
|
||||
}
|
||||
|
||||
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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
11
Lambdas/Lists/SearchHistory/src/SearchHistoryGET.java
Normal file
11
Lambdas/Lists/SearchHistory/src/SearchHistoryGET.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 SearchHistoryGET implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryGetter.class);
|
||||
}
|
||||
}
|
||||
31
Lambdas/Lists/SearchHistory/src/SearchHistoryGetter.java
Normal file
31
Lambdas/Lists/SearchHistory/src/SearchHistoryGetter.java
Normal 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);
|
||||
}
|
||||
}
|
||||
11
Lambdas/Lists/SearchHistory/src/SearchHistoryPUT.java
Normal file
11
Lambdas/Lists/SearchHistory/src/SearchHistoryPUT.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 SearchHistoryPUT implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryPutter.class);
|
||||
}
|
||||
}
|
||||
34
Lambdas/Lists/SearchHistory/src/SearchHistoryPutter.java
Normal file
34
Lambdas/Lists/SearchHistory/src/SearchHistoryPutter.java
Normal file
@ -0,0 +1,34 @@
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SearchHistoryPutter implements CallHandler {
|
||||
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
public SearchHistoryPutter(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
final private String STORE_HISTORY = "REPLACE INTO SearchHistory(userID, historyObject) VALUES(?, ?);";
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
final String searchHistoryJson = new Gson().toJson(bodyMap);
|
||||
System.out.println(searchHistoryJson);
|
||||
SearchHistory toStore = new Gson().fromJson(searchHistoryJson, SearchHistory.class);
|
||||
System.out.println(toStore);
|
||||
PreparedStatement store_history = connection.prepareStatement(STORE_HISTORY);
|
||||
store_history.setString(1, cognitoID);
|
||||
store_history.setObject(2, toStore.searches);
|
||||
System.out.println(store_history);
|
||||
store_history.executeUpdate();
|
||||
connection.commit();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
56
Lambdas/Lists/SearchHistory/src/SearchHistoryUpdate.java
Normal file
56
Lambdas/Lists/SearchHistory/src/SearchHistoryUpdate.java
Normal file
@ -0,0 +1,56 @@
|
||||
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
|
||||
import com.amazonaws.services.lambda.model.InvokeRequest;
|
||||
import com.amazonaws.services.lambda.model.InvokeResult;
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Map;
|
||||
|
||||
public class SearchHistoryUpdate implements RequestHandler<Map<String,Object>, Object> {
|
||||
|
||||
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
|
||||
String cognitoID = (String) inputMap.get("cognitoID");
|
||||
System.out.println(cognitoID);
|
||||
String newSearch = (String) inputMap.get("newSearch");
|
||||
System.out.println(newSearch);
|
||||
InvokeRequest invokeRequest = new InvokeRequest();
|
||||
invokeRequest.setFunctionName("SearchHistoryGET");
|
||||
invokeRequest.setPayload("{" +
|
||||
" \"body\": {" +
|
||||
" }," +
|
||||
" \"params\": {" +
|
||||
" \"querystring\": {" +
|
||||
" }" +
|
||||
" }," +
|
||||
" \"context\": {" +
|
||||
" \"sub\": \""+ cognitoID + "\"" +
|
||||
" }" +
|
||||
"}");
|
||||
System.out.println(invokeRequest);
|
||||
InvokeResult searchHistoryResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
|
||||
System.out.println(searchHistoryResult);
|
||||
if (searchHistoryResult.getStatusCode() != 200) {
|
||||
throw new InputMismatchException("Could not find a search history for the specified usr");
|
||||
}
|
||||
System.out.println(new String(searchHistoryResult.getPayload().array()));
|
||||
SearchHistory priorSearchHistory = new Gson().fromJson(new String(searchHistoryResult.getPayload().array()), SearchHistory.class);
|
||||
priorSearchHistory.addSearch(newSearch);
|
||||
invokeRequest.setFunctionName("SearchHistoryPUT");
|
||||
System.out.println("New search history: " + new Gson().toJson(priorSearchHistory));
|
||||
invokeRequest.setPayload("{" +
|
||||
" \"body\":" + new Gson().toJson(priorSearchHistory) + "," +
|
||||
" \"params\": {" +
|
||||
" \"querystring\": {" +
|
||||
" }" +
|
||||
" }," +
|
||||
" \"context\": {" +
|
||||
" \"sub\": \""+ cognitoID + "\"" +
|
||||
" }" +
|
||||
"}");
|
||||
invokeRequest.setInvocationType("Event");
|
||||
System.out.println(invokeRequest);
|
||||
AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest);
|
||||
return null; }
|
||||
}
|
||||
@ -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>
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user