Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/main/java/shef/data/ForYou.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,56 @@

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import javax.servlet.http.HttpServletRequest;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilter;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;

/** Defines the For You algorithm, which shows users recipes unique to their preferences. */
public class ForYou implements RecipeFilter {

private DatastoreService datastore;
private static final List<String> TEMP_PREFERENCES = new ArrayList<>(Arrays.asList("SPICY", "CHICKEN", "CHOCOLATE"));
private Set<Filter> filters;
private UserService userService;
private List<String> preferences;

public ForYou() {
datastore = DatastoreServiceFactory.getDatastoreService();
filters = new HashSet<>();
userService = UserServiceFactory.getUserService();
}

/**
* Returns recipes that match the responses to the user's preference quiz.
* For now, returns recipes that match SPICY, CHICKEN, and CHOCOLATE, or that have more than 50 likes.
* This is just a hard-coded example, and will change. */
public PreparedQuery getResults(Query query) {
filters.add(new FilterPredicate("search-strings", FilterOperator.IN, TEMP_PREFERENCES));
filters.add(new FilterPredicate("likes", FilterOperator.GREATER_THAN_OR_EQUAL, 50));
query.setFilter(new CompositeFilter(CompositeFilterOperator.OR, filters));
preferences = getUserData();
if (preferences == null) {
// If the user cannot be found, return null.
return null;
}
query.setFilter(new FilterPredicate("search-strings", FilterOperator.IN, preferences));
return datastore.prepare(query);
}

/** Helper method that adds a filter to the composite filter. */
public Filter addFilter(CompositeFilter filters) {
throw new UnsupportedOperationException();
}

/** Retrieves additional data from Datastore to be used in the filter. */
public PreparedQuery getData(Query query) {
throw new UnsupportedOperationException();
public List<String> getUserData() {
Entity user;
try {
user = datastore.get(KeyFactory.createKey("User", userService.getCurrentUser().getUserId()));
} catch(Exception e) {
// User could not be found. In this case, return null, which will cause the servlet to return trending recipes.
e.printStackTrace();
return null;
}
List<String> userPreferences = (List<String>) user.getProperty("preferences");
return userPreferences;
}
}
8 changes: 3 additions & 5 deletions src/main/java/shef/data/RecipeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import com.google.appengine.api.datastore.Query.CompositeFilter;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.PreparedQuery;
import java.util.List;

public interface RecipeFilter {

/** Returns a PreparedQuery of filtered Recipe entities. */
public PreparedQuery getResults(Query query);

/** Helper method that adds a filter to the composite filter. */
public Filter addFilter(CompositeFilter filters);

/** Retrieves additional data from Datastore to be used in the filter. */
public PreparedQuery getData(Query query);
/** Retrieves user data from Datastore to be used in the filter. */
public List<String> getUserData();

}
9 changes: 2 additions & 7 deletions src/main/java/shef/data/Trending.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package shef.data;

import javax.servlet.http.HttpServletRequest;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Query;
Expand All @@ -23,6 +22,7 @@
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.PreparedQuery;
import java.util.List;

/**
* Returns recipes that are currently popular.
Expand All @@ -43,13 +43,8 @@ public PreparedQuery getResults(Query query) {
return datastore.prepare(query);
}

/** Helper method that adds a filter to the composite filter. */
public Filter addFilter(CompositeFilter filters) {
throw new UnsupportedOperationException();
}

/** Retrieves additional data from Datastore to be used in the filter. */
public PreparedQuery getData(Query query) {
public List<String> getUserData() {
throw new UnsupportedOperationException();
}
}
4 changes: 4 additions & 0 deletions src/main/java/shef/servlets/BrowseRecipesServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro

Query query = new Query("Recipe");
PreparedQuery recipeEntities = filter.getResults(query);

// If the filter returns null, an error has occurred with UserService.
// In this case, show trending recipes.
recipeEntities = recipeEntities != null ? recipeEntities : new Trending().getResults(query);
List<Recipe> recipes = new LinkedList<>();

for (Entity recipeEntity : recipeEntities.asIterable()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shef/servlets/NewRecipeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
recipe.setProperty("likes", likes);
datastore.put(recipe);

response.sendRedirect("/recipe.html");
response.sendRedirect("/recipe.html?key=" + KeyFactory.keyToString(recipe.getKey()));
}

/**
Expand Down
60 changes: 0 additions & 60 deletions src/main/java/shef/servlets/TestDisplayRecipesServlet.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/webapp/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,10 @@ function recipePageInit() {

function getRecipeInfo() {
var url = window.location.href;
var key = url.split('?')[1];
var key = url.split('=')[1];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would caution against this!! Maybe just use getURLParams

console.log(key);

fetch('/new-recipe?' + key).then(response => response.json()).then(recipe => {
fetch('/new-recipe?key=' + key).then(response => response.json()).then(recipe => {
document.getElementById('recipe-title').innerHTML = recipe.name;
document.getElementById('recipe-description').innerHTML = recipe.description;
displayTags(recipe.tags);
Expand Down