Commit 47dd318

Richard Luby <richluby@gmail.com>
2016-11-09 09:41:09
client pulls scores from server
client pulls test array from server for display to user
1 parent 666d3c9
Changed files (1)
command.go
@@ -25,10 +25,7 @@ var commandArray = []Command{Command{Command: "exit",
 	}},
 	Command{Command: "score",
 		Description: "Display the score for the user.",
-		Run: func(command Command) error {
-			fmt.Print("score was run.")
-			return nil
-		}},
+		Run:         getScoreFromServer},
 	Command{Command: "test",
 		Usage: "test [flags] [n]",
 		Flags: map[string]Flag{"blueprint": Flag{
@@ -97,6 +94,25 @@ func init() {
 	commandArray = append(commandArray, helpCommand)
 }
 
+// getScoreFromServer retrieves the previous tests for this user
+// from the server
+func getScoreFromServer(command Command) error {
+	client := &http.Client{}
+	resp, err := client.Get(clientConfig.SERVER_URL + API_ROOT + "/test/score?username=" + clientConfig.USER)
+	defer resp.Body.Close()
+	if err != nil {
+		return fmt.Errorf("Error while making request: %+v", err)
+	}
+	clientTests := []ClientTest{}
+	decoder := json.NewDecoder(resp.Body)
+	err = decoder.Decode(&clientTests)
+	if err != nil {
+		return fmt.Errorf("Error while parsing request: %+v", err)
+	}
+	fmt.Printf("Pulled %d tests from server.", len(clientTests))
+	return nil
+}
+
 // calculateScore returns the client score as a float32
 func calculateScore(numCorrect int, numQuestions int) float32 {
 	return float32(numCorrect) / float32(numQuestions) * 100