@Override
 public synchronized boolean removeMessage(String messageId) {
   for (int i = 0; i < messages.size(); i++) {
     if (messages.get(i).getId().compareTo(Long.parseLong(messageId)) == 0) {
       Message newMessage = messages.get(i);
       newMessage.setText("");
       newMessage.setDeleted(true);
       newMessage.setEdited(false);
       newMessage.setWasEdited(false);
       messages.set(i, newMessage);
       rewriteHistory();
       return true;
     }
   }
   return false;
 }
Beispiel #2
0
  // ++++++
  private Response doPut(HttpExchange httpExchange) {
    try {
      JSONObject jsonObject =
          stringToJsonObject(inputStreamToString(httpExchange.getRequestBody()));

      Message message = new Message();
      message.setId(((String) jsonObject.get(Constants.Message.FIELD_ID)));
      message.setText(((String) jsonObject.get(Constants.Message.FIELD_TEXT)));

      // System.out.println(((long)jsonObject.get(Constants.Message.FIELD_TIMESTAMP_MODIFIED)));
      message.setTimestampModified(
          ((long) jsonObject.get(Constants.Message.FIELD_TIMESTAMP_MODIFIED)));

      logger.info(String.format("Received updated message from user: %s", message));
      if (messageStorage.updateMessage(message)) {
        return Response.ok();
      }
      return Response.badRequest("This message does not exist");
    } catch (ParseException e) {
      logger.error("Could not parse message.", e);
      return new Response(Constants.RESPONSE_CODE_BAD_REQUEST, "Incorrect request body");
    }
  }
 @Override
 public boolean updateMessage(Message message) {
   for (int i = 0; i < messages.size(); i++) {
     if (messages.get(i).getId().compareTo(message.getId()) == 0) {
       Message newMessage = messages.get(i);
       if (newMessage.isDeleted()) return false;
       if (newMessage.isEdited()) {
         newMessage.setWasEdited(true);
       }
       newMessage.setText(message.getText());
       newMessage.setEdited(true);
       messages.set(i, newMessage);
       rewriteHistory();
       return true;
     }
   }
   return false;
 }