-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMessageController.java
More file actions
60 lines (51 loc) · 2.13 KB
/
MessageController.java
File metadata and controls
60 lines (51 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package controllers;
import models.Id;
import models.Message;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;
public class MessageController {
private HashSet<Message> messagesSeen;
// why a HashSet??
public ArrayList<Message> getMessages() {
ArrayList<Message> messagesList = new ArrayList<Message>();
String json = TransactionController.MakeURLCall("/messages", "GET", "");
try {
ObjectMapper mapper = new ObjectMapper();
Message[] messages = mapper.readValue(json, Message[].class);
for (int i = 0; i < messages.length; i++) {
messagesList.add(messages[i]);
}
return messagesList;
} catch (IOException i) {
i.printStackTrace();
return null;
}
}
public ArrayList<Message> getMessagesForId(Id id) {
return (ArrayList<Message>) getMessages().stream().filter(m -> m.getfromid().equals(id.getGithub())).collect(Collectors.toList());
}
public Message getMessageForSequence(String seq) {
return getMessages().stream().filter(m -> m.getSequence().equals(seq)).collect(Collectors.toList()).get(0);
}
public ArrayList<Message> getMessagesFromFriend(Id myId, Id friendId) {
return (ArrayList<Message>) getMessages().stream()
.filter(m ->
(m.getfromid().equals(myId.getGithub())) && m.gettoid().equals(friendId.getGithub()))
.collect(Collectors.toList());
}
public Message postMessage(String myId, String toId, String msg) {
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(new Message(msg, myId, toId));
String mainUrl = String.format("/ids/%s/messages", myId);
String response = TransactionController.MakeURLCall(mainUrl, "POST", json);
return mapper.readValue(response, Message.class);
}catch (IOException i){
i.printStackTrace();
return null;
}
}
}