src/main/java/codeu/controller/FollowingServlet.java
/**
     * This function fires when a user submits the form on the following page. It gets the
     * logged-in username from the session and the user to follow from the submitted form
     * data. It uses this to edit the following user in the model.
     */
This function fires when a user submits the form on the following page. It gets the
logged-in username from the session and the user to follow from the submitted form
data. It uses this to edit the following user in the model.
This function fires when a user submits the form on the following page . It gets the logged - in username from the session and the user to follow from the submitted form data . It uses this to edit the following user in the model .
@Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
    	String username = (String) request.getSession().getAttribute("user");
			List<String> following = new ArrayList<>();
			if(username!=null && !username.isEmpty()) {
				following = userStore.getFollowing(username);
			}
			request.setAttribute("following", following);
			if (username == null) {
				response.sendRedirect("/login");
				return;
			}
			User user = userStore.getUser(username);
			if (user == null) {
				response.sendRedirect("/login");
				return;
			}
			String followUser = request.getParameter("followUserName");
      if(!userStore.isUserRegistered(followUser)) {
				request.setAttribute("error", "That username was not found.");
				request.getRequestDispatcher("/WEB-INF/view/following.jsp").forward(request, response);
				return;
      }
			if(following.contains(followUser)) {
				request.setAttribute("error", "User already followed.");
				request.getRequestDispatcher("/WEB-INF/view/following.jsp").forward(request, response);
				return;
			}
			String newFollowingList;
      if(user.getFollowingUsersString().equals("") || user.getFollowingUsersString() == null) {
				newFollowingList = followUser;
			}
			else {
      	newFollowingList = user.getFollowingUsersString() + "," + followUser;
      }
      User newUser = new User(user.getId(), user.getName(), user.getPassword(), user.getCreationTime(), newFollowingList);
      userStore.addUser(newUser);
      response.sendRedirect("/following");
    }
@ Override public void doPost ( HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException { String username = ( String ) request . getSession ( ) . getAttribute ( "user" ) ; List < String > following = new ArrayList < > ( ) ; if ( username != null && ! username . isEmpty ( ) ) { following = userStore . getFollowing ( username ) ; } request . setAttribute ( "following" , following ) ; if ( username == null ) { response . sendRedirect ( "/login" ) ; return ; } User user = userStore . getUser ( username ) ; if ( user == null ) { response . sendRedirect ( "/login" ) ; return ; } String followUser = request . getParameter ( "followUserName" ) ; if ( ! userStore . isUserRegistered ( followUser ) ) { request . setAttribute ( "error" , "That username was not found." ) ; request . getRequestDispatcher ( "/WEB-INF/view/following.jsp" ) . forward ( request , response ) ; return ; } if ( following . contains ( followUser ) ) { request . setAttribute ( "error" , "User already followed." ) ; request . getRequestDispatcher ( "/WEB-INF/view/following.jsp" ) . forward ( request , response ) ; return ; } String newFollowingList ; if ( user . getFollowingUsersString ( ) . equals ( "" ) || user . getFollowingUsersString ( ) == null ) { newFollowingList = followUser ; } else { newFollowingList = user . getFollowingUsersString ( ) + "," + followUser ; } User newUser = new User ( user . getId ( ) , user . getName ( ) , user . getPassword ( ) , user . getCreationTime ( ) , newFollowingList ) ; userStore . addUser ( newUser ) ; response . sendRedirect ( "/following" ) ; }
This function fires when a user submits the form on the following page.
This function fires when a user submits the form on the following page .
// user is not logged in, don't let them follow anybody // user was not found, don't let them follow anybody //if the target user is not registered, send error message //if the target user is already followed, send error message