-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.go
52 lines (45 loc) · 1.37 KB
/
quiz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package server
import (
"errors"
"net/url"
"strconv"
"strings"
)
type Quiz struct {
QuizId int `json:"quizId"`
Type string `json:"type"`
TimeLimit float64 `json:"timeLimit"`
Question string `json:"question"`
Answers []string `json:"answers"`
Solutions []bool `json:"solutions"`
}
type QuizProperties struct {
Type string
Difficulty string
NumVars int
TimeLimit int
}
func QuizPropertiesFromUrlQuery(queryParams url.Values) (QuizProperties, error) {
qp := QuizProperties{}
qp.Type = strings.ToUpper(queryParams.Get("type"))
if qp.Type != "SAT" && qp.Type != "TAUT" && qp.Type != "EQUIV" {
return qp, errors.New("Invalid quiz type.")
}
qp.Difficulty = strings.ToLower(queryParams.Get("difficulty"))
if qp.Difficulty != "easy" && qp.Difficulty != "medium" && qp.Difficulty != "hard" {
return qp, errors.New("Invalid quiz difficulty.")
}
// parse numVars query parameter to int
numVars, err := strconv.ParseInt(queryParams.Get("numVars"), 10, 64)
if err != nil || numVars < 1 || numVars > 10 {
return qp, errors.New("Invalid number of variables.")
}
qp.NumVars = int(numVars)
// parse timeLimit query parameter to float64
timeLimit, err := strconv.ParseInt(queryParams.Get("timeLimit"), 10, 64)
if err != nil || timeLimit < 0 {
return qp, errors.New("Invalid time limit.")
}
qp.TimeLimit = int(timeLimit)
return qp, nil
}