// 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.sps.data.User;
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 {

  /** Responsible for creating new User. */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

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

    if (subscription.equals("subscribe")) {
      subscribe = true;
    } else if (subscription.equals("unsubscribe")) {
      subscribe = false;
    }

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

    // Store the User in Datastore and refresh page.
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(userEntity);
    response.sendRedirect("/index.html");
  }

  /**
   * @return the request parameter, or the default value if the parameter was not specified by the
   *     client
   */
  private String getParameter(HttpServletRequest request, String name, String defaultValue) {
    String value = request.getParameter(name);
    if (value == null) {
      return defaultValue;
    }
    return value;
  }
}

@@ -815,21 +815,55 @@
 gle.
-sps.data.User
+appengine.api.datastore.EntityNotFoundException
 ;%0Aim
@@ -1138,16 +1138,103 @@
 vlet %7B%0A%0A
+  private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A%0A
   /** Re
@@ -1251,16 +1251,13 @@
 for 
-creating
+add a
  new
@@ -1261,17 +1261,31 @@
 new User
-.
+ into Datastore
  */%0A  @O
@@ -1447,16 +1447,24 @@
  name = 
+request.
 getParam
@@ -1472,27 +1472,14 @@
 ter(
-request, 
 %22name%22
-, %22%22
 );%0A 
@@ -1496,16 +1496,24 @@
 email = 
+request.
 getParam
@@ -1521,28 +1521,15 @@
 ter(
-request, 
 %22email%22
-, %22%22
 );%0A 
@@ -1551,16 +1551,24 @@
 rsity = 
+request.
 getParam
@@ -1572,25 +1572,16 @@
 rameter(
-request, 
 %22univers
@@ -1588,12 +1588,8 @@
 ity%22
-, %22%22
 );%0A 
@@ -1675,89 +1675,64 @@
 ribe
-;%0A%0A    if (subscription.equals(%22subscribe%22)) %7B%0A      subscribe = true;%0A    %7D else
+ = true;%0A%0A    // Check if user wants to unsubscribe.%0A   
  if 
@@ -1878,16 +1878,23 @@
 y(%22User%22
+, email
 );%0A    u
@@ -1962,21 +1962,31 @@
 ty(%22
-email%22, email
+university%22, university
 );%0A 
@@ -2012,39 +2012,37 @@
 operty(%22
-university%22, university
+subscribe%22, subscribe
 );%0A%0A    
@@ -2080,522 +2080,216 @@
 and 
-refresh page.%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    datastore.put(userEntity);%0A    response.sendRedirect(%22/index.html%22);%0A  %7D%0A%0A  /**%0A   * @return the request parameter, or the default value if the parameter was not specified by the%0A   *     client%0A   */%0A  private String getParameter(HttpServletRequest request, String name, String defaultValue) %7B%0A    String value = request.getParameter(name);%0A    if (value == null) %7B%0A      return defaultValue;%0A    %7D%0A    return value
+Datastore automatically checks for duplicates%0A    // and will update the user information accordingly to new information based on emails%0A    datastore.put(userEntity);%0A    response.sendRedirect(%22/index.html%22)
 ;%0A  

update entities to have email as key