Listify/Lambdas/Lists/ItemSearch/src/ItemSearcher.java
NMerz 3afaad89ea 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?
2020-11-14 12:05:50 -05:00

51 lines
1.9 KiB
Java

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;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ItemSearcher implements CallHandler {
Connection connection;
String cognitoID;
private final String GET_ITEM_MATCHES = "SELECT * FROM Product WHERE description LIKE ? LIMIT 100;";
public ItemSearcher(Connection connection, String cognitoID) {
this.connection = connection;
this.cognitoID = cognitoID;
}
@Override
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;
}
}