This repository was archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Adds the following feature. #93
Open
Rmoneygit
wants to merge
1
commit into
master
Choose a base branch
from
user-following
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 shef.servlets; | ||
|
|
||
| import shef.data.User; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.LinkedList; | ||
| 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.appengine.api.users.UserService; | ||
| import com.google.appengine.api.users.UserServiceFactory; | ||
| 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.datastore.Key; | ||
| import com.google.appengine.api.datastore.KeyFactory; | ||
| import com.google.appengine.api.datastore.EntityNotFoundException; | ||
|
|
||
| /** Servlet that retrieves follower information. */ | ||
| @WebServlet("/followers") | ||
| public class FollowersServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| UserService userService = UserServiceFactory.getUserService(); | ||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
|
|
||
| // Get the key from the query string. | ||
| String userKeyString = request.getParameter("key"); | ||
| Key userKey; | ||
| if(userKeyString == null && userService.isUserLoggedIn()) { | ||
| userKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId()); | ||
| } else { | ||
| userKey = KeyFactory.stringToKey(userKeyString); | ||
| } | ||
|
|
||
| List<String> followers; | ||
| try { | ||
| Entity userEntity = datastore.get(userKey); | ||
| followers = (List<String>) userEntity.getProperty("followers"); | ||
| } catch(EntityNotFoundException e) { | ||
| // User doesn't exist. | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| LinkedList<User> followerUsers = new LinkedList<User>(); | ||
|
|
||
| if(followers != null) { | ||
| for(String keyString : followers) { | ||
| try { | ||
| Key key = KeyFactory.stringToKey(keyString); | ||
| Entity userEntity = datastore.get(key); | ||
| String name = (String) userEntity.getProperty("username"); | ||
| String profilePicKey = (String) userEntity.getProperty("profile-pic"); | ||
|
|
||
| User user = new User(keyString, name, profilePicKey); | ||
| followerUsers.add(user); | ||
| } catch(EntityNotFoundException e) { | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert to JSON and send it as the response. | ||
| Gson gson = new Gson(); | ||
|
|
||
| response.setContentType("application/json"); | ||
| response.getWriter().println(gson.toJson(followerUsers)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // 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 shef.servlets; | ||
|
|
||
| import shef.data.User; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.LinkedList; | ||
| 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.appengine.api.users.UserService; | ||
| import com.google.appengine.api.users.UserServiceFactory; | ||
| 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.datastore.Key; | ||
| import com.google.appengine.api.datastore.KeyFactory; | ||
| import com.google.appengine.api.datastore.EntityNotFoundException; | ||
|
|
||
| /** Servlet that updates and retrieves following information. */ | ||
| @WebServlet("/following") | ||
| public class FollowingServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| UserService userService = UserServiceFactory.getUserService(); | ||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
|
|
||
| // Get the key from the query string. | ||
| String userKeyString = request.getParameter("key"); | ||
| Key userKey; | ||
| if(userKeyString == null && userService.isUserLoggedIn()) { | ||
| userKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId()); | ||
| } else { | ||
| userKey = KeyFactory.stringToKey(userKeyString); | ||
| } | ||
|
|
||
| List<String> following; | ||
| try { | ||
| Entity userEntity = datastore.get(userKey); | ||
| following = (List<String>) userEntity.getProperty("following"); | ||
| } catch(EntityNotFoundException e) { | ||
| // User doesn't exist. | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
|
|
||
| LinkedList<User> usersFollowed = new LinkedList<User>(); | ||
|
|
||
| if(following != null) { | ||
| for(String keyString : following) { | ||
| try { | ||
| Key key = KeyFactory.stringToKey(keyString); | ||
| Entity userEntity = datastore.get(key); | ||
| String name = (String) userEntity.getProperty("username"); | ||
| String profilePicKey = (String) userEntity.getProperty("profile-pic"); | ||
|
|
||
| User user = new User(keyString, name, profilePicKey); | ||
| usersFollowed.add(user); | ||
| } catch(EntityNotFoundException e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if possible, please auto-reformat before submitting. There are a lot of small formatting inconsistencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to ignore. |
||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert to JSON and send it as the response. | ||
| Gson gson = new Gson(); | ||
|
|
||
| response.setContentType("application/json"); | ||
| response.getWriter().println(gson.toJson(usersFollowed)); | ||
| } | ||
|
|
||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| UserService userService = UserServiceFactory.getUserService(); | ||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
|
|
||
| // Retrieve the key of the user to follow. | ||
| String userToFollowKeyString = request.getParameter("user"); | ||
| Key userToFollowKey = KeyFactory.stringToKey(userToFollowKeyString); | ||
| Entity userToFollow; | ||
|
|
||
| // Get the current user's key. | ||
| Key currentUserKey = KeyFactory.createKey("User", userService.getCurrentUser().getUserId()); | ||
| String currentUserKeyString = KeyFactory.keyToString(currentUserKey); | ||
| Entity currentUser; | ||
|
|
||
| boolean unfollow = Boolean.parseBoolean(request.getParameter("unfollow")); | ||
|
|
||
| try { | ||
| // Retrieve the current user from Datastore and add to their following list. | ||
| currentUser = datastore.get(currentUserKey); | ||
| ArrayList<String> following = (ArrayList<String>) currentUser.getProperty("following"); | ||
| // Retrieve the user to follow, and add the current user as a follow | ||
| userToFollow = datastore.get(userToFollowKey); | ||
| ArrayList<String> followers = (ArrayList<String>) userToFollow.getProperty("followers"); | ||
|
|
||
| if(unfollow) { | ||
| if(following != null) { | ||
| following.remove(userToFollowKeyString); | ||
| } | ||
| if(followers != null) { | ||
| followers.remove(currentUserKeyString); | ||
| } | ||
| } else { | ||
| if(following == null) { | ||
| following = new ArrayList<String>(); | ||
| } | ||
| if(followers == null) { | ||
| followers = new ArrayList<String>(); | ||
| } | ||
| following.add(userToFollowKeyString); | ||
| followers.add(currentUserKeyString); | ||
| currentUser.setProperty("following", following); | ||
| userToFollow.setProperty("followers", followers); | ||
| } | ||
|
|
||
| } catch(EntityNotFoundException e) { | ||
| /** This means the current user, for whom we're trying to set following for, doesn't exist. | ||
| * This should never happen. If it does, multiple things have gone seriously wrong. | ||
| */ | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
|
|
||
| // Store the user in Datastore. | ||
| datastore.put(currentUser); | ||
| datastore.put(userToFollow); | ||
| response.sendRedirect("/profile-page.html?key=" + userToFollowKeyString); | ||
| } | ||
| } | ||
60 changes: 0 additions & 60 deletions
60
src/main/java/shef/servlets/TestDisplayRecipesServlet.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a user's key isn't found (maybe the user's account was deleted or something), then this try-catch will completely interrupt the servlet and return no followers to the user. I think this behavior can be improved with a continue statement, so the problematic user is skipped, but the loop still gets all the other value users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it also might be better to run a query on Datastore users as opposed to iterating through a loop of keys and getting user data (I think individual calls to separate APIs are costly).