package com.google.sps.servlets;

import com.google.gson.Gson;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.*;
import java.io.PrintWriter;

@WebServlet("/deleteTopic")
public class DeleteTopicServlet extends HttpServlet{

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

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserService userService = UserServiceFactory.getUserService(); 
        User user = userService.getCurrentUser();
        String userId = user.getUserId(); 
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query query = 
        new Query("UserInfo")
        .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, userId));
        PreparedQuery results = datastore.prepare(query); 
        Entity entity = results.asSingleEntity(); 
        String topics = (String) entity.getProperty("topics"); 
        String [] listedTopics = topics.split(",");

        String removedTopic = request.getParameter("topic");
        String editedTopics = "";
        for (int i = 0; i < listedTopics.length; i++){
          if (!listedTopics[i].equals(removedTopic)){
            editedTopics += listedTopics[i]; 
          }
          if(i+1 < listedTopics.length){
            editedTopics+=",";
          }
        }

        entity.setProperty("topics", editedTopics);
        datastore.put(entity); 
        response.sendRedirect("/search.html");
    }

}
@@ -1677,26 +1677,16 @@
 %5Bi%5D; %0A  
-        %7D%0A
         
@@ -1689,16 +1689,17 @@
       if
+ 
 (i+1 %3C l
@@ -1723,32 +1723,34 @@
 h)%7B%0A            
+ 
+ 
 editedTopics+=%22,
@@ -1762,18 +1762,22 @@
         
-%7D%0A
+  %7D%0A  
         
@@ -1774,24 +1774,194 @@
           %7D%0A
+        %7D%0A        if (editedTopics.substring(editedTopics.length()-1).equals(%22,%22))%7B%0A          editedTopics = editedTopics.substring(0, editedTopics.length()-1);%0A        %7D
 %0A        ent

Fix extra items in topics list