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.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.sps.data.PropertyNames;
import com.google.sps.data.Video;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// Servlet for testing queries.
@WebServlet("/data")
public class DataServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String id = request.getParameter(PropertyNames.VIDEO_ID);
        if(id == null){
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query query = new Query("Video")
            .setFilter(new FilterPredicate("videoId", FilterOperator.EQUAL, id));
        PreparedQuery preparedQuery = datastore.prepare(query);
        Entity video = preparedQuery.asSingleEntity();
        String videoObjectJson = video.getProperty(PropertyNames.VIDEO_OBJECT_AS_JSON).toString();
        response.setContentType("application/json");
        response.getWriter().println(videoObjectJson);
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Video video = Video.httpRequestToVideo(request);
        Entity videoEntity = Video.videoToDatastoreEntity(video);
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(videoEntity);
        response.sendRedirect("/?videoId=" + video.getVideoId());
    }
}
@@ -772,16 +772,107 @@
 /data%22)%0A
+private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A
 public c
@@ -1024,16 +1024,75 @@
 eption%7B%0A
+        //Check if we get a valid video ID as a parameter %0A
         
@@ -1170,16 +1170,32 @@
  == null
+ %7C%7C id.isEmpty()
 )%7B%0A     
@@ -1269,93 +1269,131 @@
     
-%7D%0A        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
+    return;%0A        %7D%0A        //if our ID is valid we go ahead and query our database for the video object pertaining to it
 %0A   
@@ -1977,54 +1977,273 @@
     
-Video video = Video.httpRequestToVideo(
+//Run a query to see if we get any results from the video ID of the POST request%0A        Query query = new Query(%22Video%22)%0A            .setFilter(new FilterPredicate(%22videoId%22, FilterOperator.EQUAL, id));%0A        PreparedQuery preparedQuery = datastore.prepa
 re
+(
 que
-st
+ry
 );%0A 
@@ -2252,25 +2252,30 @@
      Entity 
-v
+queryV
 ideoEntity =
@@ -2279,37 +2279,37 @@
 y = 
-Video.videoToDatastor
+preparedQuery.asSingl
 eEntity(
 vide
@@ -2304,21 +2304,16 @@
 eEntity(
-video
 );%0A     
@@ -2319,84 +2319,304 @@
     
-DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A
+//if the video Id doesnt exist in our database, we convert the request to a video entity, add it to the database, and redirect. %0A        if(queryVideoEntity == null)%7B%0A            video = Video.httpRequestToVideo(request);%0A            Entity videoEntity = Video.videoToDatastoreEntity(video);%0A    
     
@@ -2647,32 +2647,36 @@
 ntity);%0A        
+ 
+   
 response.sendRed
@@ -2713,19 +2713,268 @@
 VideoId());%0A
+        %7D%0A%0A        //if we have the video in our database already we query for the ID property and redirect using that.%0A        response.sendRedirect(%22/?videoId=%22 + queryVideoEntity.getProperty(PropertyNames.VIDEO_ID).toString());%0A        %0A        %0A
     %7D%0A%7D

add comments and unique video funcitonality