diff --git a/Lambdas/Lists/ItemSearch/src/ItemSearcher.java b/Lambdas/Lists/ItemSearch/src/ItemSearcher.java index a3a7381..e24ebdf 100644 --- a/Lambdas/Lists/ItemSearch/src/ItemSearcher.java +++ b/Lambdas/Lists/ItemSearch/src/ItemSearcher.java @@ -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 body, HashMap queryParams, String s) throws SQLException { + public Object conductAction(Map body, HashMap 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; } } diff --git a/Lambdas/Lists/SearchHistory/src/SearchHistory.java b/Lambdas/Lists/SearchHistory/src/SearchHistory.java new file mode 100644 index 0000000..f319150 --- /dev/null +++ b/Lambdas/Lists/SearchHistory/src/SearchHistory.java @@ -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 searches; + + public SearchHistory(ArrayList 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 getSearches() { + return searches; + } + + public void setSearches(ArrayList searches) { + this.searches = searches; + } + + public void addSearch(String newSearch) { + searches.add(newSearch); + } + + @Override + public String toString() { + return "SearchHistory{" + + "searches=" + searches + + '}'; + } +} diff --git a/Lambdas/Lists/SearchHistory/src/SearchHistoryGET.java b/Lambdas/Lists/SearchHistory/src/SearchHistoryGET.java new file mode 100644 index 0000000..4c696b3 --- /dev/null +++ b/Lambdas/Lists/SearchHistory/src/SearchHistoryGET.java @@ -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, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryGetter.class); + } +} diff --git a/Lambdas/Lists/SearchHistory/src/SearchHistoryGetter.java b/Lambdas/Lists/SearchHistory/src/SearchHistoryGetter.java new file mode 100644 index 0000000..13319cd --- /dev/null +++ b/Lambdas/Lists/SearchHistory/src/SearchHistoryGetter.java @@ -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 bodyMap, HashMap 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); + } +} \ No newline at end of file diff --git a/Lambdas/Lists/SearchHistory/src/SearchHistoryPOST.java b/Lambdas/Lists/SearchHistory/src/SearchHistoryPOST.java new file mode 100644 index 0000000..6b05141 --- /dev/null +++ b/Lambdas/Lists/SearchHistory/src/SearchHistoryPOST.java @@ -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, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, SearchHistoryUpdater.class); + } +} diff --git a/Lambdas/Lists/SearchHistory/src/SearchHistoryUpdater.java b/Lambdas/Lists/SearchHistory/src/SearchHistoryUpdater.java new file mode 100644 index 0000000..625c6db --- /dev/null +++ b/Lambdas/Lists/SearchHistory/src/SearchHistoryUpdater.java @@ -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 bodyMap, HashMap 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; + } +} \ No newline at end of file diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml index b148d2c..4725165 100644 --- a/Lambdas/Lists/pom.xml +++ b/Lambdas/Lists/pom.xml @@ -94,6 +94,11 @@ jackson-dataformat-xml 2.8.5 + + com.fasterxml.jackson.core + jackson-annotations + 2.11.3 + com.fasterxml.jackson.core jackson-databind diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index 881ce02..ec0c89b 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -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 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(); } + */ } //------------------------------------------------------------------------------------------// diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index b2df019..ed149c3 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -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 idReceiver) throws JSONException { + putObject(toPost, idReceiver, null); + } + + public void putObject(Object toPut, Receiver 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)); diff --git a/Listify/app/src/main/java/com/example/listify/data/SearchHistory.java b/Listify/app/src/main/java/com/example/listify/data/SearchHistory.java new file mode 100644 index 0000000..1d29594 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/data/SearchHistory.java @@ -0,0 +1,30 @@ +package com.example.listify.data; + +import java.util.ArrayList; + +public class SearchHistory { + ArrayList searches; + + public SearchHistory(ArrayList searches) { + this.searches = searches; + } + + public ArrayList getSearches() { + return searches; + } + + public void setSearches(ArrayList searches) { + this.searches = searches; + } + + public void addSearch(String newSearch) { + searches.add(newSearch); + } + + @Override + public String toString() { + return "SearchHistory{" + + "searches=" + searches + + '}'; + } +}