Use plain table search history

This is easier, clearer, faster for inters, and ultimately not really much less space efficient than trying to store this in a serialized nor unreasonably slow for retrievals.

Food for thought: Would this might be well-suited to a nosql database?
This commit is contained in:
NMerz 2020-11-14 12:05:50 -05:00
parent 8653b9a035
commit 3afaad89ea
7 changed files with 40 additions and 79 deletions

View File

@ -29,10 +29,18 @@ public class ItemSearcher implements CallHandler {
ItemSearch searchResultsObject = new ItemSearch(searchResults);
System.out.println(searchResultsObject);
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName("SearchHistoryUpdate");
invokeRequest.setFunctionName("SearchHistoryPOST");
invokeRequest.setPayload("{" +
" \"newSearch\": \"" + queryParams.get("id") + "\"," +
" \"cognitoID\": \""+ cognitoID + "\"" +
" \"body\": {" +
" \"searchTerm\": \"" + queryParams.get("id") + "\"" +
" }," +
" \"params\": {" +
" \"querystring\": {" +
" }" +
" }," +
" \"context\": {" +
" \"sub\": \"" + cognitoID + "\"" +
" }" +
"}");
invokeRequest.setInvocationType("Event");
System.out.println(invokeRequest);

View File

@ -15,7 +15,11 @@ public class SearchHistory implements Serializable {
}
public SearchHistory(ResultSet row) throws SQLException {
this.searches = (ArrayList<String>) row.getObject("historyObject");
this.searches = new ArrayList<>();
row.beforeFirst();
while (row.next()) {
this.searches.add(row.getString("search"));
}
}
public ArrayList<String> getSearches() {

View File

@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Map;
public class SearchHistoryPUT implements RequestHandler<Map<String,Object>, Object> {
public class SearchHistoryPOST implements RequestHandler<Map<String,Object>, Object> {
public Object handleRequest(Map<String, Object> inputMap, Context unfilled) {
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryPutter.class);
return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryUpdater.class);
}
}

View File

@ -1,56 +0,0 @@
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; }
}

View File

@ -1,31 +1,25 @@
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 {
public class SearchHistoryUpdater implements CallHandler {
private Connection connection;
private String cognitoID;
public SearchHistoryPutter(Connection connection, String cognitoID) {
public SearchHistoryUpdater(Connection connection, String cognitoID) {
this.connection = connection;
this.cognitoID = cognitoID;
}
final private String STORE_HISTORY = "REPLACE INTO SearchHistory(userID, historyObject) VALUES(?, ?);";
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 {
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);
PreparedStatement store_history = connection.prepareStatement(UPDATE_HISTORY);
store_history.setString(1, cognitoID);
store_history.setObject(2, toStore.searches);
store_history.setObject(2, bodyMap.get("searchTerm"));
System.out.println(store_history);
store_history.executeUpdate();
connection.commit();

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

@ -20,4 +20,11 @@ public class SearchHistory {
public void addSearch(String newSearch) {
searches.add(newSearch);
}
@Override
public String toString() {
return "SearchHistory{" +
"searches=" + searches +
'}';
}
}