package com.google.sps.servlets;
import java.io.IOException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
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.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Key;
import java.util.List;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.sps.data.User;
import com.google.sps.data.Room;
import com.google.sps.service.DatabaseService;
import com.google.sps.data.RequestParser;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/dashboard")
public class DashboardHandlerServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Key userKey = ((User) request.getAttribute(User.USER_ENTITY_NAME)).getUserKey();
        Filter roomFilter = new FilterPredicate(Room.HOST_PROPERTY_KEY, FilterOperator.EQUAL, userKey);
        Query query = new Query(Room.ROOM_ENTITY_NAME).setFilter(roomFilter);
        PreparedQuery results = datastore.prepare(query);
    
        ArrayList<Room> rooms = new ArrayList<>();
        for(Entity entity : results.asIterable()){
            rooms.add(new Room(entity));
        }

        response.setContentType("application/json");
        response.getWriter().println(new Gson().toJson(rooms));
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        JsonObject jobject = JsonParser.parseString(RequestParser.parseStringFromRequest(request)).getAsJsonObject();

        Room room = new Room((User) request.getAttribute(User.USER_ENTITY_NAME), 
            jobject.get(Room.TITLE_PROPERTY_KEY).getAsString(), jobject.get(Room.DESCRIPTION_PROPERTY_KEY).getAsString());
        DatabaseService.save(room.getRoomEntity());

        response.setStatus(HttpServletResponse.SC_OK);
    }
}
@@ -26,16 +26,17 @@
 rvlets;%0A
+%0A
 import j
@@ -334,262 +334,14 @@
 ore.
-Query.SortDirection;%0Aimport com.google.appengine.api.datastore.Entity;%0Aimport com.google.appengine.api.datastore.Query.Filter;%0Aimport com.google.appengine.api.datastore.Query.FilterOperator;%0Aimport com.google.appengine.api.datastore.Query.FilterPredicate
+Entity
 ;%0Aim
@@ -676,32 +676,77 @@
 atabaseService;%0A
+import com.google.sps.service.FilterService;%0A
 import com.googl
@@ -1153,92 +1153,8 @@
 n %7B%0A
-        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A
     
@@ -1250,236 +1250,106 @@
     
-Filt
+PreparedQu
 er
+y
  r
-oomFilter = new FilterPredicate(Room.HOST_PROPERTY_KEY, FilterOperator.EQUAL, userKey);%0A        Query query = new Query(Room.ROOM_ENTITY_NAME).setFilter(roomFilter);%0A        PreparedQuery results = datastore.prepare(quer
+esults = FilterService.getAll(Room.ROOM_ENTITY_NAME, Room.HOST_PROPERTY_KEY, userKe
 y);%0A
@@ -1747,17 +1747,16 @@
 ption %7B%0A
-%0A
         
@@ -1865,17 +1865,16 @@
 ject();%0A
-%0A
         
@@ -2066,24 +2066,25 @@
 sString());%0A
+%0A
         Data
@@ -2119,25 +2119,24 @@
 mEntity());%0A
-%0A
         resp

Update dashboard handler