Commit 47bac88

Richard Luby <richluby@gmail.com>
2016-11-01 08:20:53
client now works with ClientTest
added struct to hold all information regarding a client test
1 parent bff7f37
command.go
@@ -87,28 +87,28 @@ func init() {
 
 // walks the user through the test questions
 // this function updates the records with the user responses
-func runTest(recordArray []ClientRecord) []ClientRecord {
+func runTest(clientTest ClientTest) []ClientRecord {
 	reader := bufio.NewReader(os.Stdin)
 	var responseArray []ClientRecord
-	for i, record := range recordArray {
+	for i, record := range clientTest.Records {
 		fmt.Printf("%d) %s\n", i+1, record.Question)
 		input, _ := reader.ReadString('\n')
 		input = strings.TrimSpace(input)
 		if strings.Compare(input, record.Answer) == 0 {
 			fmt.Println("Correct.")
-			recordArray[i].AnsweredCorrectly = true
+			clientTest.Records[i].AnsweredCorrectly = true
 		} else if strings.Compare(input, "exit") == 0 {
 			return responseArray
 		} else {
 			fmt.Printf("Incorrect. Correct answer was: %s\n", record.Answer)
-			recordArray[i].ClientAnswer = input
+			clientTest.Records[i].ClientAnswer = input
 		}
 	}
 	return responseArray
 }
 
 // postRecordsToServer sends the client responses back to the server
-func postRecordsToServer(client *http.Client, recordArray *[]ClientRecord) error {
+func postRecordsToServer(client *http.Client, recordArray *ClientTest) error {
 	data, err := json.Marshal(recordArray)
 	if err != nil {
 		return err
@@ -138,7 +138,7 @@ func getRecordFromServer(client *http.Client, config CLIENT_CONFIG, numQuestions
 // from retrieving the records to returning the answers
 func executeTest(args []string) error {
 	client := &http.Client{}
-	var recordArray []ClientRecord
+	var clientTest ClientTest
 	var err error
 	questions := 20
 	if len(args) > 0 {
@@ -147,10 +147,10 @@ func executeTest(args []string) error {
 			return fmt.Errorf("Error while converting to number: %+v", err)
 		}
 	}
-	recordArray, err = getRecordFromServer(client, clientConfig, questions)
+	clientTest.Records, err = getRecordFromServer(client, clientConfig, questions)
 	if err != nil {
 		return fmt.Errorf("Error while requesting test from server: %+v", err)
 	}
-	runTest(recordArray)
-	return postRecordsToServer(client, &recordArray)
+	runTest(clientTest)
+	return postRecordsToServer(client, &clientTest)
 }
serverHandlers.go
@@ -55,7 +55,7 @@ func handlePostingTest(writer http.ResponseWriter, request *http.Request) error
 		return fmt.Errorf("Incorrectly formatted URL path: %+v", request.URL.Path)
 	}
 	username := request.FormValue("username")
-	var clientResponseRecord []ClientRecord
+	var clientResponseRecord ClientTest
 	data, err := ioutil.ReadAll(request.Body)
 	if err != nil {
 		http.Error(writer, "Could not read the test record.", http.StatusBadRequest)
structures.go
@@ -30,3 +30,10 @@ type ClientRecord struct {
 	ClientAnswer      string
 	AnsweredCorrectly bool
 }
+
+// ClientTest stores a full test and its results for the client
+type ClientTest struct {
+	Records  []ClientRecord
+	Score    int
+	Username string
+}