Anthony Jordan 6 anni fa
parent
commit
b75d71b899

+ 29
- 2
Client/src/main/java/SimpleShell.java Vedi File

@@ -71,11 +71,38 @@ public class SimpleShell {
71 71
 
72 72
                 // messages
73 73
                 if (list.get(0).equals("messages")) {
74
-                    String results = webber.get_messages();
75
-                    SimpleShell.prettyPrint(results);
74
+                    if (list.size() >1) {
75
+                        String results = webber.get_messagesForId(list.get(1));
76
+                        SimpleShell.prettyPrint(results);
77
+                    } else {
78
+                        String results = webber.get_messages();
79
+                        SimpleShell.prettyPrint(results);
80
+                    }
76 81
                     continue;
77 82
                 }
83
+
78 84
                 // you need to add a bunch more.
85
+                if (list.get(0).equals("send") && list.get(list.size()-2).equals("to")) {
86
+                    StringBuilder message = new StringBuilder();
87
+                    System.out.println("Test");
88
+                    for (int i = 2; i < list.size()-2; i++){
89
+                        message.append(list.get(i) + " ");
90
+                    }
91
+                    String results = webber.sendMessageToId(list.get(1), message.toString(), list.get(list.size()-1));
92
+                    SimpleShell.prettyPrint(results);
93
+                    continue;
94
+                }else if (list.get(0).equals("send")) {
95
+                    System.out.println("test2");
96
+                    if (list.size() >1) {
97
+                        StringBuilder message = new StringBuilder();
98
+                        for (int i = 2; i < list.size(); i++){
99
+                            message.append(list.get(i) + " ");
100
+                        }
101
+                        String results = webber.sendMessage(list.get(1), message.toString());
102
+                        SimpleShell.prettyPrint(results);
103
+                    }
104
+                    continue;
105
+                }
79 106
 
80 107
                 //!! command returns the last command in history
81 108
                 if (list.get(list.size() - 1).equals("!!")) {

+ 43
- 5
Client/src/main/java/YouAreEll.java Vedi File

@@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
3 3
 import com.mashape.unirest.http.Unirest;
4 4
 import com.mashape.unirest.http.exceptions.UnirestException;
5 5
 import java.io.IOException;
6
+import java.util.ArrayList;
6 7
 import java.util.Arrays;
7 8
 
8 9
 public class YouAreEll {
@@ -30,23 +31,50 @@ public class YouAreEll {
30 31
 
31 32
     public void postID(String name, String gitHubID){
32 33
         String jsonPackage = "{ \"userid\": \"-\", \"name\": \"" + name + "\", \"github\": \"" + gitHubID + "\"}";
33
-
34
-        System.out.println(MakeURLCall("/ids", "PUT", jsonPackage));
34
+        MakeURLCall("/ids", "PUT", jsonPackage);
35 35
     }
36 36
 
37 37
     public String get_messages() {
38 38
         ObjectMapper jsonMapper = new ObjectMapper();
39 39
         String jsonString = MakeURLCall("/messages", "GET", "");
40
+        String last20 = convertMessagesToString(jsonMapper, jsonString);
41
+        if (last20 != null) return last20;
42
+        return null;
43
+    }
44
+
45
+    private String convertMessagesToString(ObjectMapper jsonMapper, String jsonString) {
40 46
         try {
41 47
             Messages[] messagesList = jsonMapper.readValue(jsonString, Messages[].class);
42
-            Messages[] last20 = Arrays.copyOf(messagesList, 20);
43
-            return Arrays.toString(last20);
48
+            if (messagesList!=null) {
49
+                Messages[] last20 = Arrays.copyOf(messagesList, 20);
50
+                return getRidOfNulls(last20);
51
+            }
44 52
         } catch (IOException e) {
45 53
             e.printStackTrace();
46 54
         }
47 55
         return null;
48 56
     }
49 57
 
58
+    public String get_messagesForId(String id) {
59
+        ObjectMapper jsonMapper = new ObjectMapper();
60
+        String jsonString = MakeURLCall("/ids/"+id+"/messages", "GET", "");
61
+        String last20 = convertMessagesToString(jsonMapper, jsonString);
62
+        if (last20 != null) return last20;
63
+        return null;
64
+    }
65
+
66
+    public String sendMessage(String id, String message){
67
+        String jsonPackage = "{ \"sequence\": \"-\", \"timestamp\": \"2018-03-21T01:00:00.0Z\", \"fromid\": \"" +id + "\"," +
68
+                "\"toid\": \"\", \"message\": \"" + message + "\"}";
69
+        return MakeURLCall("/ids/" + id + "/messages", "PUT", jsonPackage);
70
+    }
71
+
72
+    public String sendMessageToId(String fromId, String message, String toId){
73
+        String jsonPackage = "{ \"sequence\": \"-\", \"timestamp\": \"2018-03-21T01:00:00.0Z\", \"fromid\": \"" + fromId + "\"," +
74
+                "\"toid\": \""+toId+ "\", \"message\": \"" + message + "\"}";
75
+        return MakeURLCall("/ids/" + fromId + "/messages", "PUT", jsonPackage);
76
+    }
77
+
50 78
     public String MakeURLCall(String mainurl, String method, String jpayload) {
51 79
 
52 80
         try {
@@ -54,11 +82,21 @@ public class YouAreEll {
54 82
                 return Unirest.get("http://zipcode.rocks:8085" + mainurl).asString().getBody();
55 83
             }
56 84
             if (method.equals("PUT")){
57
-                return Unirest.post("http://zipcode.rocks:8085" + mainurl).body(jpayload).asString().getStatusText();
85
+                return Unirest.post("http://zipcode.rocks:8085" + mainurl).body(jpayload).asString().getBody();
58 86
             }
59 87
         }catch (UnirestException e){
60 88
             e.printStackTrace();
61 89
         }
62 90
         return null;
63 91
     }
92
+
93
+    private String getRidOfNulls(Messages[] messages){
94
+        ArrayList<Messages> returnArrayList = new ArrayList<>();
95
+        for (Messages message: messages) {
96
+            if (message!=null){
97
+                returnArrayList.add(message);
98
+            }
99
+        }
100
+        return returnArrayList.toString();
101
+    }
64 102
 }