use of codeu.model.data.Conversation 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.Conversation in project CodeU-Spring-2018 by maksymko.
the class PersistentDataStoreTest method testSaveAndLoadConversations.
@Test
public void testSaveAndLoadConversations() throws PersistentDataStoreException {
UUID idOne = UUID.randomUUID();
UUID ownerOne = UUID.randomUUID();
String titleOne = "Test_Title";
Instant creationOne = Instant.ofEpochMilli(1000);
Conversation inputConversationOne = new Conversation(idOne, ownerOne, titleOne, creationOne);
UUID idTwo = UUID.randomUUID();
UUID ownerTwo = UUID.randomUUID();
String titleTwo = "Test_Title_Two";
Instant creationTwo = Instant.ofEpochMilli(2000);
Conversation inputConversationTwo = new Conversation(idTwo, ownerTwo, titleTwo, creationTwo);
// save
persistentDataStore.writeThrough(inputConversationOne);
persistentDataStore.writeThrough(inputConversationTwo);
// load
List<Conversation> resultConversations = persistentDataStore.loadConversations();
// confirm that what we saved matches what we loaded
Conversation resultConversationOne = resultConversations.get(0);
Assert.assertEquals(idOne, resultConversationOne.getId());
Assert.assertEquals(ownerOne, resultConversationOne.getOwnerId());
Assert.assertEquals(titleOne, resultConversationOne.getTitle());
Assert.assertEquals(creationOne, resultConversationOne.getCreationTime());
Conversation resultConversationTwo = resultConversations.get(1);
Assert.assertEquals(idTwo, resultConversationTwo.getId());
Assert.assertEquals(ownerTwo, resultConversationTwo.getOwnerId());
Assert.assertEquals(titleTwo, resultConversationTwo.getTitle());
Assert.assertEquals(creationTwo, resultConversationTwo.getCreationTime());
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
the class ConversationStoreTest method testGetConversationWithTitle_notFound.
@Test
public void testGetConversationWithTitle_notFound() {
Conversation resultConversation = conversationStore.getConversationWithTitle("unfound_title");
Assert.assertNull(resultConversation);
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
the class ConversationStoreTest method testGetConversationWithTitle_found.
@Test
public void testGetConversationWithTitle_found() {
Conversation resultConversation = conversationStore.getConversationWithTitle(CONVERSATION_ONE.getTitle());
assertEquals(CONVERSATION_ONE, resultConversation);
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
the class PersistentDataStore method loadConversations.
/**
* Loads all Conversation objects from the Datastore service and returns them in a List.
*
* @throws PersistentDataStoreException if an error was detected during the load from the
* Datastore service
*/
public List<Conversation> loadConversations() throws PersistentDataStoreException {
List<Conversation> conversations = new ArrayList<>();
// Retrieve all conversations from the datastore.
Query query = new Query("chat-conversations");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID ownerUuid = UUID.fromString((String) entity.getProperty("owner_uuid"));
String title = (String) entity.getProperty("title");
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
Conversation conversation = new Conversation(uuid, ownerUuid, title, creationTime);
conversations.add(conversation);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return conversations;
}
Aggregations