package com.google.sps.servlets; 
 
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.gson.Gson;
import com.google.sps.data.Recipe;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/** Servlet responsible for listing private recipes. */
@WebServlet("/list-private-recipes")
public class ListPrivateRecipesServlet extends HttpServlet {
   
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // this Query seems to need the addSort method to work so rn it's arbitrarily by the recipeName
    // it can be changed
    Query query = new Query("PrivateRecipe").addSort("recipeName", SortDirection.DESCENDING);
 
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery results = datastore.prepare(query);
 
    List<Recipe> recipes = new ArrayList<>();
    for (Entity entity : results.asIterable()) {
    //   TODO: check if logged in user's id == recipe's authorID
    //   String authorID = (String) entity.getProperty("authorID");
      long id = entity.getKey().getId();
      String recipeName = (String) entity.getProperty("recipeName");
      String description = (String) entity.getProperty("description");
      String blobkey = (String) entity.getProperty("imageBlobKey");
      ArrayList<String> tags = (ArrayList<String>) entity.getProperty("tags");
      ArrayList<String> ingredients = (ArrayList<String>) entity.getProperty("ingredients");
      ArrayList<String> steps = (ArrayList<String>) entity.getProperty("steps");

      Recipe recipe = new Recipe(id, recipeName, blobkey, description, tags, ingredients, steps, 0);
      recipes.add(recipe);
    }
 
    Gson gson = new Gson();

    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(recipes));
  }
}
 


@@ -1090,16 +1090,55 @@
 changed%0A
+    // TODO: filter query by author id%0A
     Quer
@@ -1460,73 +1460,8 @@
 ) %7B%0A
-    //   TODO: check if logged in user's id == recipe's authorID%0A
     
@@ -1776,261 +1776,8 @@
 y%22);
-%0A      ArrayList%3CString%3E tags = (ArrayList%3CString%3E) entity.getProperty(%22tags%22);%0A      ArrayList%3CString%3E ingredients = (ArrayList%3CString%3E) entity.getProperty(%22ingredients%22);%0A      ArrayList%3CString%3E steps = (ArrayList%3CString%3E) entity.getProperty(%22steps%22);
 %0A%0A  
@@ -1810,16 +1810,17 @@
 cipe(id,
+ 
  recipeN
@@ -1823,16 +1823,17 @@
 ipeName,
+ 
  blobkey
@@ -1833,16 +1833,17 @@
 blobkey,
+ 
  descrip
@@ -1850,37 +1850,8 @@
 tion
-, tags, ingredients, steps, 0
 );%0A 

use new Recipe constructor for recipe cards in list servlet