-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionsRepo.go
130 lines (106 loc) · 2.42 KB
/
questionsRepo.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import "errors"
type QuestionsRepo struct {
Questions map[int]*Question
}
//var questions = make(map[int]*Question)
func (s *QuestionsRepo) GetQuestions() (map[int]*Question, error) {
return s.Questions, nil
}
func (s *QuestionsRepo) FindQuestionByID(ID int) (*Question, error) {
var question = s.Questions[ID]
return question, nil
}
func (s *QuestionsRepo) GetTotalQuestionsNumber() int {
return len(s.Questions)
}
func (s *QuestionsRepo) FindAnswerByQuestionId(ID int) (*Answer, error) {
var question, questionFound = s.Questions[ID]
if !questionFound {
return nil, errors.New("question not found")
}
var correctOptions []string
for lbl, opt := range question.Options {
if opt.Correct {
correctOptions = append(correctOptions, lbl)
}
}
var answer = &Answer{
QuestionID: question.ID,
CorrectOptions: correctOptions,
}
return answer, nil
}
func (s *QuestionsRepo) Init() {
s.Questions = make(map[int]*Question)
optionsQ1 := make(map[string]Option)
optionsQ1["A"] = Option{
ID: 11,
QuestionID: 1,
Label: "A",
Text: "Question 1 - Answer 1 (correct)",
Correct: true,
}
optionsQ1["B"] = Option{
ID: 12,
QuestionID: 1,
Label: "B",
Text: "Question 1 - Answer 2",
Correct: false,
}
optionsQ1["C"] = Option{
ID: 13,
QuestionID: 1,
Label: "C",
Text: "Question 1 - Answer 3",
Correct: false,
}
optionsQ1["D"] = Option{
ID: 14,
QuestionID: 1,
Label: "D",
Text: "Question 1 - Answer 4",
Correct: false,
}
question1 := &Question{
ID: 1,
Text: "Question 1",
Options: optionsQ1,
}
s.Questions[question1.ID] = question1
optionsQ2 := make(map[string]Option)
optionsQ2["A"] = Option{
ID: 21,
QuestionID: 2,
Label: "A",
Text: "Question 2 - Answer 1",
Correct: false,
}
optionsQ2["B"] = Option{
ID: 22,
QuestionID: 2,
Label: "B",
Text: "Question 2 - Answer 2 (correct)",
Correct: true,
}
optionsQ2["C"] = Option{
ID: 23,
QuestionID: 2,
Label: "C",
Text: "Question 2 - Answer 3",
Correct: false,
}
optionsQ2["D"] = Option{
ID: 24,
QuestionID: 2,
Label: "D",
Text: "Question 2 - Answer 4",
Correct: false,
}
question2 := &Question{
ID: 2,
Text: "Question 2",
Options: optionsQ2,
}
s.Questions[question2.ID] = question2
}