소스 검색

return results

master
DemiSel 2 년 전
부모
커밋
6b63a1d07c

+ 5
- 1
backend/src/main/java/api/JsonKeys.java 파일 보기

@@ -9,7 +9,11 @@ public enum JsonKeys implements JsonKey {
9 9
     KEY_ANSWERS("answers"),
10 10
     KEY_DESCRIPTION("description"),
11 11
     KEY_NAME("name"),
12
-    KEY_ID("id");
12
+    KEY_ID("id"),
13
+    KEY_TRAIT("trait"),
14
+    KEY_TRAITS("traits"),
15
+    KEY_WEIGHT("weight"),
16
+    KEY_RESULTS("results");
13 17
 
14 18
 
15 19
     private final Object value;

+ 5
- 0
backend/src/main/java/controller/SessionController.java 파일 보기

@@ -16,6 +16,11 @@ public class SessionController extends ModelController {
16 16
         return new Session().asJsonObject();
17 17
     }
18 18
 
19
+    public JsonObject getEntriesForId(String iId, JsonObject iBody) {
20
+        Session vSession = Session.getSessionForUUID(UUID.fromString(iId));
21
+        return vSession.asJsonObject();
22
+    }
23
+
19 24
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
20 25
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
21 26
         if(null == vSession)

+ 40
- 1
backend/src/main/java/model/personnality/Trait.java 파일 보기

@@ -1,6 +1,17 @@
1 1
 package model.personnality;
2 2
 
3
-public class Trait {
3
+import com.github.cliftonlabs.json_simple.JsonObject;
4
+import com.github.cliftonlabs.json_simple.Jsonable;
5
+
6
+import java.io.IOException;
7
+import java.io.StringWriter;
8
+import java.io.Writer;
9
+import java.nio.charset.StandardCharsets;
10
+
11
+import static api.JsonKeys.KEY_DESCRIPTION;
12
+import static api.JsonKeys.KEY_NAME;
13
+
14
+public class Trait implements Jsonable {
4 15
 
5 16
     private String fName;
6 17
     private String fDescription;
@@ -9,4 +20,32 @@ public class Trait {
9 20
         fName           = iName;
10 21
         fDescription    = iDescription;
11 22
     }
23
+
24
+    // ------------------------------------------------------------------
25
+    // Jsonable Implementation
26
+    // ------------------------------------------------------------------
27
+    public JsonObject asJsonObject(){
28
+        JsonObject vResult = new JsonObject();
29
+        vResult.put(KEY_NAME, fName);
30
+        vResult.put(KEY_DESCRIPTION, fDescription);
31
+        return vResult;
32
+    }
33
+
34
+    @Override
35
+    public String toJson() {
36
+        final StringWriter writable = new StringWriter();
37
+        try{
38
+            this.toJson(writable);
39
+        }catch(final IOException caught)
40
+        {
41
+            caught.printStackTrace();
42
+        }
43
+        return writable.toString();
44
+    }
45
+
46
+    @Override
47
+    public void toJson(Writer writable) throws IOException {
48
+
49
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
50
+    }
12 51
 }

+ 16
- 6
backend/src/main/java/model/questionnaire/Answer.java 파일 보기

@@ -10,8 +10,7 @@ import java.io.Writer;
10 10
 import java.nio.charset.StandardCharsets;
11 11
 import java.util.UUID;
12 12
 
13
-import static api.JsonKeys.KEY_DESCRIPTION;
14
-import static api.JsonKeys.KEY_ID;
13
+import static api.JsonKeys.*;
15 14
 
16 15
 public class Answer implements Jsonable {
17 16
 
@@ -33,6 +32,18 @@ public class Answer implements Jsonable {
33 32
     public float getWeight() { return fWeight; }
34 33
     public UUID getId() { return fId; }
35 34
 
35
+
36
+    // ------------------------------------------------------------------
37
+    // Jsonable Implementation
38
+    // ------------------------------------------------------------------
39
+    public JsonObject asJsonObject(){
40
+        final JsonObject vResult = new JsonObject();
41
+        vResult.put(KEY_DESCRIPTION, fDescription);
42
+        vResult.put(KEY_ID, fId.toString());
43
+        return vResult;
44
+    }
45
+
46
+
36 47
     @Override
37 48
     public String toJson() {
38 49
         final StringWriter writable = new StringWriter();
@@ -45,11 +56,10 @@ public class Answer implements Jsonable {
45 56
         return writable.toString();
46 57
     }
47 58
 
59
+
48 60
     @Override
49 61
     public void toJson(Writer writable) throws IOException {
50
-        final JsonObject vResult = new JsonObject();
51
-        vResult.put(KEY_DESCRIPTION, fDescription);
52
-        vResult.put(KEY_ID, fId.toString());
53
-        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
62
+
63
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
54 64
     }
55 65
 }

+ 9
- 6
backend/src/main/java/model/questionnaire/Question.java 파일 보기

@@ -41,7 +41,14 @@ public class Question implements Jsonable {
41 41
     // ------------------------------------------------------------------
42 42
     // Jsonable Implementation
43 43
     // ------------------------------------------------------------------
44
-
44
+    public JsonObject asJsonObject()
45
+    {
46
+        JsonObject vResult = new JsonObject();
47
+        vResult.put(KEY_NAME, fName);
48
+        vResult.put(KEY_ANSWERS, fAnswers);
49
+        vResult.put(KEY_ID, fId.toString());
50
+        return vResult;
51
+    }
45 52
     @Override
46 53
     public String toJson() {
47 54
         final StringWriter writable = new StringWriter();
@@ -56,10 +63,6 @@ public class Question implements Jsonable {
56 63
 
57 64
     @Override
58 65
     public void toJson(Writer writable) throws IOException {
59
-        JsonObject vResult = new JsonObject();
60
-        vResult.put(KEY_NAME, fName);
61
-        vResult.put(KEY_ANSWERS, fAnswers);
62
-        vResult.put(KEY_ID, fId.toString());
63
-        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
66
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
64 67
     }
65 68
 }

+ 2
- 2
backend/src/main/java/model/questionnaire/Session.java 파일 보기

@@ -11,7 +11,7 @@ import java.nio.charset.StandardCharsets;
11 11
 import java.util.HashMap;
12 12
 import java.util.UUID;
13 13
 
14
-import static api.JsonKeys.KEY_ID;
14
+import static api.JsonKeys.*;
15 15
 
16 16
 public class Session implements Jsonable {
17 17
 
@@ -41,7 +41,7 @@ public class Session implements Jsonable {
41 41
     public JsonObject asJsonObject() {
42 42
         JsonObject vResult = new JsonObject();
43 43
         vResult.put(KEY_ID, fId.toString());
44
-
44
+        vResult.put(KEY_RESULTS, fAnswers.toJson());
45 45
         return vResult;
46 46
     }
47 47
 

+ 42
- 1
backend/src/main/java/model/questionnaire/SessionAnswers.java 파일 보기

@@ -1,14 +1,24 @@
1 1
 package model.questionnaire;
2 2
 
3
+import com.github.cliftonlabs.json_simple.JsonArray;
4
+import com.github.cliftonlabs.json_simple.JsonObject;
5
+import com.github.cliftonlabs.json_simple.Jsonable;
3 6
 import model.personnality.Personality;
4 7
 import model.personnality.Trait;
5 8
 
9
+import java.io.IOException;
10
+import java.io.StringWriter;
11
+import java.io.Writer;
12
+import java.nio.charset.StandardCharsets;
13
+import java.util.ArrayList;
6 14
 import java.util.HashMap;
7 15
 
16
+import static api.JsonKeys.*;
17
+
8 18
 /**
9 19
  * Stores answers provided for a session of answers
10 20
  */
11
-public class SessionAnswers {
21
+public class SessionAnswers implements Jsonable {
12 22
 
13 23
     private HashMap<Question, Answer> fAnswers;
14 24
     private HashMap<Trait, Float> fTraits;
@@ -47,4 +57,35 @@ public class SessionAnswers {
47 57
         fTraits.put(vNewTrait, vNewTraitWeight);
48 58
     }
49 59
 
60
+    // ------------------------------------------------------------------
61
+    // Jsonable Implementation
62
+    // ------------------------------------------------------------------
63
+    @Override
64
+    public String toJson() {
65
+        final StringWriter writable = new StringWriter();
66
+        try{
67
+            this.toJson(writable);
68
+        }catch(final IOException caught)
69
+        {
70
+            caught.printStackTrace();
71
+        }
72
+        return writable.toString();
73
+    }
74
+
75
+    @Override
76
+    public void toJson(Writer writable) throws IOException {
77
+        JsonObject vResult = new JsonObject();
78
+        JsonArray vAnswers = new JsonArray();
79
+        fAnswers.forEach( (question, answer) -> {
80
+            vAnswers.add(answer.asJsonObject().putChain(KEY_QUESTION, question.asJsonObject()));
81
+        });
82
+        vResult.put(KEY_ANSWERS, vAnswers);
83
+        JsonArray vTraits = new JsonArray();
84
+        fTraits.forEach( (trait, weight) -> {
85
+            vTraits.add(trait.asJsonObject().putChain(KEY_WEIGHT, weight));
86
+        });
87
+        vResult.put(KEY_TRAITS, vTraits);
88
+        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
89
+    }
90
+
50 91
 }

+ 1
- 1
frontend/src/component/questionnaire.js 파일 보기

@@ -52,7 +52,7 @@ function Questionnaire(props){
52 52
     )
53 53
     else
54 54
     return(
55
-        <QuestionnaireResult/>
55
+        <QuestionnaireResult sessionId={props.sessionId}/>
56 56
     )
57 57
 }
58 58
 

+ 6
- 1
frontend/src/component/questionnaire_result.js 파일 보기

@@ -6,7 +6,12 @@ function QuestionnaireResult(props){
6 6
 
7 7
     if(null === resultsFetched)
8 8
     {
9
-        
9
+        fetch(url+"/session/"+props.sessionId) 
10
+        .then(response => response.json())
11
+        .then(result => {
12
+            onResultsFetched(result);
13
+        })
14
+        .catch( e => console.error(e));
10 15
     }
11 16
     return(
12 17
         <div>

Loading…
취소
저장