use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class DefaultDataStore method addRandomUsers.
private void addRandomUsers() {
List<String> randomUsernames = getRandomUsernames();
Collections.shuffle(randomUsernames);
for (int i = 0; i < DEFAULT_USER_COUNT; i++) {
User user = new User(UUID.randomUUID(), randomUsernames.get(i), "password", Instant.now());
PersistentStorageAgent.getInstance().writeThrough(user);
users.add(user);
}
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class ChatServlet method doPost.
/**
* This function fires when a user submits the form on the chat page. It gets the logged-in
* username from the session, the conversation title from the URL, and the chat message from the
* submitted form data. It creates a new Message from that data, adds it to the model, and then
* redirects back to the chat page.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
// user is not logged in, don't let them add a message
response.sendRedirect("/login");
return;
}
User user = userStore.getUser(username);
if (user == null) {
// user was not found, don't let them add a message
response.sendRedirect("/login");
return;
}
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
response.sendRedirect("/conversations");
return;
}
String messageContent = request.getParameter("message");
// this removes any HTML from the message content
String cleanedMessageContent = Jsoup.clean(messageContent, Whitelist.none());
Message message = new Message(UUID.randomUUID(), conversation.getId(), user.getId(), cleanedMessageContent, Instant.now());
messageStore.addMessage(message);
// redirect to a GET request
response.sendRedirect("/chat/" + conversationTitle);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class ConversationServlet method doPost.
/**
* This function fires when a user submits the form on the conversations page. It gets the
* logged-in username from the session and the new conversation title from the submitted form
* data. It uses this to create a new Conversation object that it adds to the model.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
// user is not logged in, don't let them create a conversation
response.sendRedirect("/conversations");
return;
}
User user = userStore.getUser(username);
if (user == null) {
// user was not found, don't let them create a conversation
System.out.println("User not found: " + username);
response.sendRedirect("/conversations");
return;
}
String conversationTitle = request.getParameter("conversationTitle");
if (!conversationTitle.matches("[\\w*]*")) {
request.setAttribute("error", "Please enter only letters and numbers.");
request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
return;
}
if (conversationStore.isTitleTaken(conversationTitle)) {
// conversation title is already taken, just go into that conversation instead of creating a
// new one
response.sendRedirect("/chat/" + conversationTitle);
return;
}
Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
conversationStore.addConversation(conversation);
response.sendRedirect("/chat/" + conversationTitle);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.
the class UserStoreTest method testGetUser_byUsername_found.
@Test
public void testGetUser_byUsername_found() {
User resultUser = userStore.getUser(USER_ONE.getName());
assertEquals(USER_ONE, resultUser);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.
the class PersistentStorageAgentTest method testWriteThroughUser.
@Test
public void testWriteThroughUser() {
User user = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
persistentStorageAgent.writeThrough(user);
Mockito.verify(mockPersistentDataStore).writeThrough(user);
}
Aggregations