Browse Source

Post had worked...

Lauren Green 6 years ago
parent
commit
5abb87e591

+ 4
- 0
Client/src/main/java/Id.java View File

@@ -40,4 +40,8 @@ public class Id {
40 40
             "github: " +  github);
41 41
     }
42 42
 
43
+    public String toJSON() {
44
+        return "{\"userid\":\"-\", \"github\":\""+ github +"\", \"name\":\"" + name + "\"}";
45
+    }
46
+
43 47
 }

+ 28
- 6
Client/src/main/java/SimpleShell.java View File

@@ -14,13 +14,13 @@ import java.util.List;
14 14
 public class SimpleShell {
15 15
 
16 16
 
17
-    public static void prettyPrint(JsonNode jsonNode) {
17
+    public static String prettyPrint(JsonNode jsonNode) {
18 18
         try {
19 19
             ObjectMapper mapper = new ObjectMapper();
20 20
             Object json = mapper.readValue(jsonNode.toString(), Object.class);
21
-            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
21
+            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
22 22
         } catch (Exception e) {
23
-            System.out.println("Sorry, pretty print didn't work");
23
+            return "Sorry, pretty print didn't work";
24 24
         }
25 25
     }
26 26
 
@@ -71,14 +71,36 @@ public class SimpleShell {
71 71
                 // Specific Commands.
72 72
 
73 73
                 // ids
74
-                if (list.contains("ids")) {
75
-                    webber.get_ids();
74
+                if (list.contains("ids") && list.size() == 1) {
75
+                    System.out.println(webber.get_ids());
76 76
                     continue;
77 77
                 }
78 78
 
79 79
                 // messages
80 80
                 if (list.contains("messages")) {
81
-                    webber.get_messages();
81
+                    System.out.println(webber.get_messages());
82
+                    continue;
83
+                }
84
+
85
+                if (list.contains("ids") && list.size() > 1 && !webber.githubIdExists(list.get(2))) {
86
+                    Id id = new Id(list.get(1), list.get(2));
87
+                    webber.postNewUser(id);
88
+                    if(webber.githubIdExists(list.get(2))) {
89
+                        System.out.println("User successfully added.");
90
+                    } else {
91
+                        System.out.println("Error adding new user.");
92
+                    }
93
+                    continue;
94
+                }
95
+
96
+                if (list.contains("ids") && list.size() > 1 && webber.githubIdExists(list.get(2))) {
97
+                    Id id = new Id(list.get(1), list.get(2));
98
+                    webber.putNewName(id);
99
+                    if(webber.githubIdExists(list.get(1))) {
100
+                        System.out.println("User name successfully updated.");
101
+                    } else {
102
+                        System.out.println("Error updating name.");
103
+                    }
82 104
                     continue;
83 105
                 }
84 106
                 // you need to add a bunch more.

+ 26
- 24
Client/src/main/java/YouAreEll.java View File

@@ -18,60 +18,62 @@ public class YouAreEll {
18 18
 
19 19
     }
20 20
 
21
-    public static void main(String[] args) {
22
-        YouAreEll urlhandler = new YouAreEll();
23
-//        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
24
-//        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
25
-    }
26
-
27
-    public void get_ids(){
21
+    public String get_ids(){
28 22
 
29 23
         try {
30 24
             HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/ids")
31 25
                     .header("accept", "application/json")
32 26
                     .asJson();
33 27
 
34
-            SimpleShell.prettyPrint(jsonResponse.getBody());
28
+            return SimpleShell.prettyPrint(jsonResponse.getBody());
35 29
 
36 30
         } catch (UnirestException e) {
37
-            System.out.println("Error");
31
+            return "Error";
38 32
         }
39 33
 
40 34
     }
41 35
 
42
-    public void get_messages() {
36
+    public String get_messages() {
43 37
         try {
44 38
             HttpResponse<JsonNode> jsonResponse = Unirest.get(underARock + "/messages")
45 39
                     .header("accept", "application/json")
46 40
                     .asJson();
47 41
 
48
-            SimpleShell.prettyPrint(jsonResponse.getBody());
42
+            return SimpleShell.prettyPrint(jsonResponse.getBody());
49 43
 
50 44
         } catch (UnirestException e) {
51
-            System.out.println("Error");
45
+            return "Error";
52 46
         }
53 47
     }
54 48
 
55
-    public String MakeURLCall(String mainurl, String method, String jpayload) throws UnirestException {
56
-
57
-        String url = underARock + mainurl;
58
-        method = method.toUpperCase();
59
-        HttpResponse<com.mashape.unirest.http.JsonNode> jsonResponse;
49
+    public void postNewUser(Id id) {
50
+        try {
51
+            Unirest.post(underARock + "/ids")
52
+                    .body(id.toJSON())
53
+                    .asJson();
60 54
 
61
-        if(method.equals("GET")) {
62
-            jsonResponse = Unirest.get("http://zipcode.rocks:8085/GET").routeParam("method", url).asJson();
55
+        } catch (UnirestException e) {
56
+            System.out.println("Error");
63 57
         }
64 58
 
65
-        if(method.equals("POST")) {
66
-
67
-        }
59
+    }
68 60
 
69
-        if(method.equals("PUT")) {
61
+    public void putNewName(Id id) {
62
+        try {
63
+            Unirest.put(underARock + "/ids")
64
+                    .body(id.toJSON())
65
+                    .asJson();
70 66
 
67
+        } catch (UnirestException e) {
68
+            System.out.println("Error");
71 69
         }
72 70
 
73
-            return "nada";
71
+    }
74 72
 
73
+    public boolean githubIdExists(String githubId) {
74
+        String ids = get_ids();
75
+        return ids.contains(githubId);
75 76
     }
76 77
 
78
+
77 79
 }