Search in sources :

Example 61 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class ProfileServlet method doGet.

/**
 * This function fires when a user navigates to a user's profile page. It gets the username from
 * the URL, finds the corresponding User, and fetches the messages posted by that user. It then
 * forwards to profile.jsp for rendering.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    String name = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(name);
    List<Message> messages = null;
    if (user != null) {
        messages = messageStore.getMessagesByAuthorSorted(user.getId());
    }
    request.setAttribute("messages", messages);
    request.setAttribute("user", user);
    request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message)

Example 62 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class RegisterServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
    String confirmPassword = request.getParameter("confirmPassword");
    if (!username.matches("[\\w*\\s*]*")) {
        request.setAttribute("error", "Please enter only letters, numbers, and spaces.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (password == null || password.length() < 8) {
        request.setAttribute("error", "Please enter a password that is at least 8 characters.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (confirmPassword == null) {
        request.setAttribute("error", "Please enter a confirmation password.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (password != null && !password.equals(confirmPassword)) {
        request.setAttribute("error", "Your password and confirmation password do not match.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    if (userStore.isUserRegistered(username)) {
        request.setAttribute("error", "That username is already taken.");
        request.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(request, response);
        return;
    }
    User user = new User(UUID.randomUUID(), username, passwordHash, null, Instant.now());
    userStore.addUser(user);
    String message = "<a href=\"/profile/" + username + "\">" + username + "</a>" + " created an account!";
    UUID userId = user.getId();
    UUID conversationId = new UUID(0L, 0L);
    Activity activity = new Activity(UUID.randomUUID(), userId, conversationId, Instant.now(), "joinedApp", message);
    activityStore.addActivity(activity);
    response.sendRedirect("/login");
}
Also used : User(codeu.model.data.User) Activity(codeu.model.data.Activity) UUID(java.util.UUID)

Example 63 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class UserStore method searchUsers.

/**
 * Finds all User objects whose username contains the given string
 *
 * @return the list of user objects.
 */
public List<User> searchUsers(String search) {
    Comparator<User> userComparator = new Comparator<User>() {

        public int compare(User u1, User u2) {
            return StringUtils.getLevenshteinDistance(search, u1.getName()) - StringUtils.getLevenshteinDistance(search, u2.getName());
        }
    };
    ArrayList<User> result = new ArrayList<User>();
    for (User user : users) {
        if (user.getName().contains(search)) {
            result.add(user);
        }
    }
    return result;
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator)

Example 64 with User

use of codeu.model.data.User in project codeu-2018-team12 by codeu-2018-team12.

the class UserStore method searchUsersSorted.

/**
 * Finds all User objects whose username contains the given string
 *
 * @return the list of user objects sorted with users whose name is closer to the given string
 *     first.
 */
public List<User> searchUsersSorted(String search) {
    Comparator<User> userComparator = new Comparator<User>() {

        public int compare(User u1, User u2) {
            return StringUtils.getLevenshteinDistance(search, u1.getName()) - StringUtils.getLevenshteinDistance(search, u2.getName());
        }
    };
    ArrayList<User> result = new ArrayList<User>();
    for (User user : users) {
        if (user.getName().contains(search)) {
            result.add(user);
        }
    }
    result.sort(userComparator);
    return result;
}
Also used : User(codeu.model.data.User) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator)

Example 65 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class PersistentDataStoreTest method testSaveAndLoadUsers.

@Test
public void testSaveAndLoadUsers() throws PersistentDataStoreException {
    UUID idOne = UUID.randomUUID();
    String nameOne = "test_username_one";
    String passwordOne = "passwordOne";
    Instant creationOne = Instant.ofEpochMilli(1000);
    User inputUserOne = new User(idOne, nameOne, passwordOne, creationOne);
    UUID idTwo = UUID.randomUUID();
    String nameTwo = "test_username_two";
    String passwordTwo = "passwordTwo";
    Instant creationTwo = Instant.ofEpochMilli(2000);
    User inputUserTwo = new User(idTwo, nameTwo, passwordTwo, creationTwo);
    // save
    persistentDataStore.writeThrough(inputUserOne);
    persistentDataStore.writeThrough(inputUserTwo);
    // load
    List<User> resultUsers = persistentDataStore.loadUsers();
    // confirm that what we saved matches what we loaded
    User resultUserOne = resultUsers.get(0);
    Assert.assertEquals(idOne, resultUserOne.getId());
    Assert.assertEquals(nameOne, resultUserOne.getName());
    Assert.assertEquals(creationOne, resultUserOne.getCreationTime());
    Assert.assertEquals(passwordOne, resultUserOne.getPassword());
    User resultUserTwo = resultUsers.get(1);
    Assert.assertEquals(idTwo, resultUserTwo.getId());
    Assert.assertEquals(nameTwo, resultUserTwo.getName());
    Assert.assertEquals(creationTwo, resultUserTwo.getCreationTime());
    Assert.assertEquals(passwordTwo, resultUserTwo.getPassword());
}
Also used : User(codeu.model.data.User) Instant(java.time.Instant) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

User (codeu.model.data.User)91 Test (org.junit.Test)60 Conversation (codeu.model.data.Conversation)36 Message (codeu.model.data.Message)23 UserStore (codeu.model.store.basic.UserStore)9 UUID (java.util.UUID)9 HttpSession (javax.servlet.http.HttpSession)8 Activity (codeu.model.data.Activity)7 Instant (java.time.Instant)7 ArrayList (java.util.ArrayList)7 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Comparator (java.util.Comparator)2 ActivityStore (codeu.model.store.basic.ActivityStore)1 OutputSettings (org.jsoup.nodes.Document.OutputSettings)1