You must login to comment
"); + // response.sendRedirect("/index.html"); + return; + } + + // If user has not set a nickname, redirect to nickname page + String nickname = getUserNickname(userService.getCurrentUser().getUserId()); + if (nickname == null) { + response.sendRedirect("/nickname"); + //in implementation, redirect to index + return; + } + + // User is logged in and has a nickname, so the request can proceed + String logoutUrl = userService.createLogoutURL("/home"); + ListHello " + nickname + "!
"); + // out.println("Logout here.
"); + // out.println("Change your nickname here.
"); + + response.setContentType("application/json;"); + response.getWriter().println(login); + response.sendRedirect("/index.html"); + } + + /** Returns the nickname of the user with id, or null if the user has not set a nickname. */ + private String getUserNickname(String id) { + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); + Query query = + new Query("UserInfo") + .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id)); + PreparedQuery results = datastore.prepare(query); + Entity entity = results.asSingleEntity(); + if (entity == null) { + return null; + } + String nickname = (String) entity.getProperty("nickname"); + return nickname; + } +} diff --git a/portfolio/src/main/java/com/google/sps/servlets/NicknameServlet.java b/portfolio/src/main/java/com/google/sps/servlets/NicknameServlet.java new file mode 100644 index 0000000..07fadbc --- /dev/null +++ b/portfolio/src/main/java/com/google/sps/servlets/NicknameServlet.java @@ -0,0 +1,92 @@ +// 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.PreparedQuery; +import com.google.appengine.api.datastore.Query; +import com.google.appengine.api.users.UserService; +import com.google.appengine.api.users.UserServiceFactory; +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/nickname") +public class NicknameServlet extends HttpServlet { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println("Set your nickname here:
"); + out.println(""); + } else { + String loginUrl = userService.createLoginURL("/nickname"); + out.println("Login here.
"); + } + } + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + UserService userService = UserServiceFactory.getUserService(); + if (!userService.isUserLoggedIn()) { + response.sendRedirect("/nickname"); + return; + } + + String nickname = request.getParameter("nickname"); + String id = userService.getCurrentUser().getUserId(); + + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); + Entity entity = new Entity("UserInfo", id); + entity.setProperty("id", id); + entity.setProperty("nickname", nickname); + // The put() function automatically inserts new data or updates existing data based on ID + datastore.put(entity); + + response.sendRedirect("/home"); + } + + /** + * Returns the nickname of the user with id, or empty String if the user has not set a nickname. + */ + private String getUserNickname(String id) { + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); + Query query = + new Query("UserInfo") + .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id)); + PreparedQuery results = datastore.prepare(query); + Entity entity = results.asSingleEntity(); + if (entity == null) { + return ""; + } + String nickname = (String) entity.getProperty("nickname"); + return nickname; + } +} diff --git a/portfolio/src/main/webapp/index.html b/portfolio/src/main/webapp/index.html index d896219..aadfeb0 100644 --- a/portfolio/src/main/webapp/index.html +++ b/portfolio/src/main/webapp/index.html @@ -56,11 +56,12 @@