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);
}
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");
}
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;
}
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;
}
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());
}
Aggregations