// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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.EntityNotFoundException;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/user")
public class UserDataServlet extends HttpServlet {

  private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  /** Responsible for adding/updating a User into Datastore */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    // Get the input from the form.
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String university = request.getParameter("university");
    String subscription = request.getParameter("email-notif");
    boolean subscribe = true;

    // Check if user wants to unsubscribe.
    if (subscription.equals("unsubscribe")) {
      subscribe = false;
    }

    // Create an Entity type User.
    Entity userEntity = new Entity("User", email);
    userEntity.setProperty("name", name);
    userEntity.setProperty("university", university);
    userEntity.setProperty("subscribe", subscribe);

    // Store the User in Datastore and Datastore automatically checks for duplicates
    // and will update the user information accordingly to new information based on emails
    datastore.put(userEntity);
    response.sendRedirect("/index.html");
  }
}

@@ -1235,39 +1235,18 @@
 /** 
-Responsible for adding
+Add
 /updat
-ing
+e
  a U
@@ -1793,43 +1793,8 @@
  %7D%0A%0A
-    // Create an Entity type User.%0A
     
@@ -2000,160 +2000,58 @@
  // 
-Store the User in Datastore and Datastore automatically checks for duplicates%0A    // and will update the user information accordingly to new information
+Datastores updates the entity if it existed before
  bas
@@ -2061,17 +2061,20 @@
 on email
-s
+ key
 %0A    dat

update comments to be active voice