Commit d65572a

Richard Luby <richluby@gmail.com>
2016-11-08 13:55:54
server accepts blueprints
server accepts blueprints and returns tests built according to the blueprint specified in the query
1 parent 929cf9d
Changed files (1)
serverHandlers.go
@@ -10,14 +10,19 @@ import (
 	"os"
 	"path/filepath"
 	"strconv"
+	"strings"
 )
 
 // buildRecordArray builds an array of Records based on the parameter criteria
-func buildRecordArray(numQuestions int) []Record {
+func buildRecordArray(numQuestions int, categories []int) []Record {
 	var keyIndex, valueIndex int
 	var recordArray = []Record{}
 	for i := 0; i < numQuestions; i++ {
-		keyIndex = rand.Intn(len(categoryKeys))
+		if categories == nil {
+			keyIndex = rand.Intn(len(categoryKeys))
+		} else {
+			keyIndex = categories[rand.Intn(len(categories))]
+		}
 		valueIndex = rand.Intn(len(recordMap[categoryKeys[keyIndex]]))
 		recordArray = append(recordArray, recordMap[categoryKeys[keyIndex]][valueIndex])
 	}
@@ -28,9 +33,24 @@ func buildRecordArray(numQuestions int) []Record {
 func handleRequestForTest(writer http.ResponseWriter, request *http.Request) error {
 	var giveRecords []Record
 	var err error
+	var categories []int
 	questions := request.FormValue("questions")
+	blueprint := request.FormValue("blueprint")
+	if blueprint != "" {
+		splits := strings.Split(blueprint, "-")
+		categories = make([]int, len(splits))
+		for i, key := range splits {
+			categories[i], err = strconv.Atoi(strings.TrimSpace(key))
+			if err != nil {
+				return fmt.Errorf("Error parsing numbers: %+v", err)
+			}
+		}
+	}
 	numQuestions, err := strconv.Atoi(questions)
-	giveRecords = buildRecordArray(numQuestions)
+	if err != nil {
+		return fmt.Errorf("Error building questions: %+v", err)
+	}
+	giveRecords = buildRecordArray(numQuestions, categories)
 	data, err := json.Marshal(giveRecords)
 	if err != nil {
 		return fmt.Errorf("Error building questions: %+v", err)