Commit 3e170eb

Richard Luby <richluby@gmail.com>
2017-01-26 08:15:29
improved logic flow
greater degree of error message available for problems
1 parent b19956f
Changed files (1)
serverHandlers.go
@@ -19,26 +19,14 @@ import (
 var userTests map[string][]ClientTest
 var userTestsLock = sync.RWMutex{}
 
-// getRecordForCategory returns a record for the specified category
-// it moves the now used record to the usedRecordMap, and updates
-// recordMap to reflect the change
-func getRecordForCategory(categoryIndex int) Record {
-	mapLock.Lock()
-	defer mapLock.Unlock()
-	return Record{}
-}
-
 // buildRecordArray builds an array of Records based on the parameter criteria
-func buildRecordArray(numQuestions int, categoryIndices []int) []Record {
+func buildRecordArray(numQuestions int, selectedCategories []string) ([]Record, error) {
 	recordArray := []Record{}
-	numCats := len(categoryIndices)
-	totalCats := len(categories)
-	for len(recordArray) < numQuestions {
-		var catChosen int
-		if numCats > 0 {
-			catChosen = categoryIndices[rand.Intn(numCats)]
-		} else {
-			catChosen = rand.Intn(totalCats)
+	var err error
+	if len(selectedCategories) > 0 {
+		recordArray, err = returnForQuery(selectedCategories, numQuestions)
+	} else {
+		recordArray, err = returnForQuery(categories, numQuestions)
 	}
 	return recordArray, err
 }
@@ -64,28 +52,29 @@ func parseBluePrint(blueprint string) ([]string, error) {
 func handleRequestForTest(writer http.ResponseWriter, request *http.Request) error {
 	var giveRecords []Record
 	var err error
-	var chosenCategories []int
+	var chosenCategories []string
 	questions := request.FormValue("questions")
 	blueprint := request.FormValue("blueprint")
 	if blueprint != "" {
-		splits := strings.Split(blueprint, "-")
-		chosenCategories = make([]int, len(splits))
-		for i, key := range splits {
-			chosenCategories[i], err = strconv.Atoi(strings.TrimSpace(key))
-			if err != nil {
-				return fmt.Errorf("Error parsing numbers: %+v", err)
-			}
+		chosenCategories, err = parseBluePrint(blueprint)
+		if err != nil {
+			http.Error(writer, "Improperly formatted blueprint parameter.", http.StatusBadRequest)
+			return fmt.Errorf("Error building blueprint: %+v", err)
 		}
 	}
 	numQuestions, err := strconv.Atoi(questions)
 	if err != nil {
-		http.Error(writer, "Improperly formatted number.", http.StatusBadRequest)
+		http.Error(writer, "Improperly formatted question parameter.", http.StatusBadRequest)
 		return fmt.Errorf("Error building questions: %+v", err)
 	}
-	giveRecords = buildRecordArray(numQuestions, chosenCategories)
+	giveRecords, err = buildRecordArray(numQuestions, chosenCategories)
+	if err != nil {
+		http.Error(writer, "Failed to generate record array.", http.StatusInternalServerError)
+		return fmt.Errorf("Error while retrieving records from DB: %+v", err)
+	}
 	data, err := json.Marshal(giveRecords)
 	if err != nil {
-		http.Error(writer, "Improperly formatted data.", http.StatusBadRequest)
+		http.Error(writer, "Failed to generate record array.", http.StatusInternalServerError)
 		return fmt.Errorf("Error building questions: %+v", err)
 	}
 	writer.Write(data)