// Copyright 2021 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 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.gson.Gson;
import com.google.sps.data.*;  

@WebServlet("/authentication")
public class AuthServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    User userObj = User.create(1896,"Phoebe Khokgawe","phoebek@");

    Gson gson = new Gson();
    
    response.setContentType("application/json");

    response.getWriter().println(gson.toJson(userObj));
  }
}

@@ -886,266 +886,736 @@
   %0A%0A
-@WebServlet(%22/authentication%22)%0Apublic class AuthServlet extends HttpServlet %7B%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    User userObj = User.create(1896,%22Phoebe Khokgawe%22,%22phoebek@%22
+import java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;%0Aimport javax.sql.DataSource;%0A%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A@WebServlet(%22/authentication%22)%0Apublic class AuthServlet extends HttpServlet %7B%0A%0A  private static final Logger LOGGER = Logger.getLogger(AuthServlet.class.getName());%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    DataSource pool = (DataSource) request.getServletContext().getAttribute(%22my-pool%22);%0A%0A    try (Connection conn = pool.getConnection()) %7B%0A      String email = request.getParameter(%22email%22);%0A      User user = getUser(conn, email
 );%0A%0A
+  
     
@@ -1642,19 +1642,16 @@
 ();%0A    
-%0A  
   respon
@@ -1689,21 +1689,22 @@
 json%22);%0A
-%0A
+ 
     
+ 
 response
@@ -1744,17 +1744,917 @@
 user
-Obj));%0A  %7D%0A%7D%0A
+));%0A      //response.setContentType(%22text/html;%22);%0A      //response.getWriter().println(%22%3Ch1%3E%22 + user + %22%3C/h1%3E%22);%0A    %7D catch (SQLException ex) %7B%0A      LOGGER.log(Level.WARNING, %22Error while attempting to find user.%22, ex);%0A      response.setStatus(500);%0A      response.getWriter().write(%22Unable to successfully fetch user%22);%0A    %7D%0A  %7D%0A%0A  private User getUser(Connection conn, String email) throws SQLException %7B%0A    String stmt = %22SELECT id, name FROM users WHERE email='%22 + email + %22';%22;%0A    try (PreparedStatement userStmt = conn.prepareStatement(stmt);) %7B%0A      // Execute the statement%0A      ResultSet userRs = userStmt.executeQuery();%0A      // Convert a result into User object%0A      int id;%0A      String name;%0A      User user;%0A      while (userRs.next()) %7B%0A        id = userRs.getInt(1);%0A        name = userRs.getString(2);%0A        return User.create(id, name, email);%0A      %7D%0A      return null;%0A    %7D%0A  %7D%0A%7D

Update auth servlet to query db and respond to user email