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.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.UserRecord;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Once class owner submits a TA email, retrieve that user and add them as a TA to the class
@WebServlet("/add-ta")
public class AddTA extends HttpServlet {
  private FirebaseAuth authInstance;

  // Get the current session
  @Override
  public void init(ServletConfig config) throws ServletException {
    try {
      authInstance = FirebaseAuth.getInstance(FirebaseAppManager.getApp());
    } catch (IOException e) {
      throw new ServletException(e);
    }
  }

  // Add a TA to the datastore
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    try {
      // Obtain the teaching assistant email and search for the user
      String teachingAssistantEmail = request.getParameter("taEmail").trim();
      UserRecord userRecord = authInstance.getUserByEmail(teachingAssistantEmail);

      String classCode = request.getParameter("classCode").trim();

      // Create a TA entity with both a user ID and a class ID
      Entity taEntity = new Entity("TA");
      taEntity.setProperty("userKey", userRecord.getUid());
      taEntity.setProperty("classKey", classCode);

      datastore.put(taEntity); // Store the new TA with their class code

    } catch (FirebaseAuthException e) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } catch (IllegalArgumentException e) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
  }
}

@@ -1,38 +1,4 @@
-package com.google.sps.servlets;%0A%0A
 impo
@@ -170,16 +170,184 @@
 Entity;%0A
+import com.google.appengine.api.datastore.EntityNotFoundException;%0Aimport com.google.appengine.api.datastore.Key;%0Aimport com.google.appengine.api.datastore.KeyFactory;%0A
 import c
@@ -554,32 +554,60 @@
 io.IOException;%0A
+import java.util.ArrayList;%0A
 import javax.ser
@@ -1005,16 +1005,8 @@
 %7B%0A  
-private 
 Fire
@@ -1028,17 +1028,16 @@
 stance;%0A
-%0A
   // Get
@@ -1299,17 +1299,16 @@
   %7D%0A  %7D%0A
-%0A
   // Add
@@ -1442,17 +1442,16 @@
 ption %7B%0A
-%0A
     Data
@@ -1518,25 +1518,24 @@
 eService();%0A
-%0A
     try %7B%0A  
@@ -1759,24 +1759,69 @@
 antEmail);%0A%0A
+      // Find the corresponding Class Entity%0A
       String
@@ -1874,16 +1874,124 @@
 .trim();
+%0A      Key classKey = KeyFactory.stringToKey(classCode);%0A      Entity classEntity = datastore.get(classKey);
 %0A%0A      
@@ -2204,81 +2204,515 @@
 e);%0A
-%0A      datastore.put(taEntity); // Store the new TA with their class code
+      // Get the list of TAs%0A      ArrayList%3CString%3E listOfClassTAs = (ArrayList) classEntity.getProperty(%22taList%22);%0A%0A      datastore.put(taEntity); // Store the new TA with their class code%0A      // Update the class's TA list to include the new TA%0A      listOfClassTAs.add(teachingAssistantEmail);%0A      classEntity.setProperty(%22taList%22, listOfClassTAs);%0A      datastore.put(classEntity);%0A%0A      // Redirect to the class dashboard page%0A      response.sendRedirect(%22/dashboard.html?classCode=%22 + classCode);
 %0A%0A  
@@ -2914,16 +2914,118 @@
 QUEST);%0A
+    %7D catch (EntityNotFoundException e) %7B%0A      response.sendError(HttpServletResponse.SC_NOT_FOUND);%0A
     %7D%0A  
@@ -3027,9 +3027,8 @@
  %7D%0A  %7D%0A%7D
-%0A

Add additions to ta servlet.