Browse Source

Part 1 finished

Anthony Jordan 6 years ago
parent
commit
b75d71b899
2 changed files with 72 additions and 7 deletions
  1. 29
    2
      Client/src/main/java/SimpleShell.java
  2. 43
    5
      Client/src/main/java/YouAreEll.java

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

71
 
71
 
72
                 // messages
72
                 // messages
73
                 if (list.get(0).equals("messages")) {
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
                     continue;
81
                     continue;
77
                 }
82
                 }
83
+
78
                 // you need to add a bunch more.
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
                 //!! command returns the last command in history
107
                 //!! command returns the last command in history
81
                 if (list.get(list.size() - 1).equals("!!")) {
108
                 if (list.get(list.size() - 1).equals("!!")) {

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

3
 import com.mashape.unirest.http.Unirest;
3
 import com.mashape.unirest.http.Unirest;
4
 import com.mashape.unirest.http.exceptions.UnirestException;
4
 import com.mashape.unirest.http.exceptions.UnirestException;
5
 import java.io.IOException;
5
 import java.io.IOException;
6
+import java.util.ArrayList;
6
 import java.util.Arrays;
7
 import java.util.Arrays;
7
 
8
 
8
 public class YouAreEll {
9
 public class YouAreEll {
30
 
31
 
31
     public void postID(String name, String gitHubID){
32
     public void postID(String name, String gitHubID){
32
         String jsonPackage = "{ \"userid\": \"-\", \"name\": \"" + name + "\", \"github\": \"" + gitHubID + "\"}";
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
     public String get_messages() {
37
     public String get_messages() {
38
         ObjectMapper jsonMapper = new ObjectMapper();
38
         ObjectMapper jsonMapper = new ObjectMapper();
39
         String jsonString = MakeURLCall("/messages", "GET", "");
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
         try {
46
         try {
41
             Messages[] messagesList = jsonMapper.readValue(jsonString, Messages[].class);
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
         } catch (IOException e) {
52
         } catch (IOException e) {
45
             e.printStackTrace();
53
             e.printStackTrace();
46
         }
54
         }
47
         return null;
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
     public String MakeURLCall(String mainurl, String method, String jpayload) {
78
     public String MakeURLCall(String mainurl, String method, String jpayload) {
51
 
79
 
52
         try {
80
         try {
54
                 return Unirest.get("http://zipcode.rocks:8085" + mainurl).asString().getBody();
82
                 return Unirest.get("http://zipcode.rocks:8085" + mainurl).asString().getBody();
55
             }
83
             }
56
             if (method.equals("PUT")){
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
         }catch (UnirestException e){
87
         }catch (UnirestException e){
60
             e.printStackTrace();
88
             e.printStackTrace();
61
         }
89
         }
62
         return null;
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
 }