-
Notifications
You must be signed in to change notification settings - Fork 2
/
question.go
145 lines (138 loc) · 4.41 KB
/
question.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package controllers
import (
"encoding/json"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/gubesch/go-quiz/models"
"log"
"net/http"
"strconv"
)
func ShowAllQuestions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
questions, err := models.GetAllQuestions()
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
} else {
NewResponse(true, "Successfully got all questions.").Attr("questions", questions).JSON(w, http.StatusOK)
}
}
func CreateQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
var question models.Question
_ = json.NewDecoder(r.Body).Decode(&question)
err := question.CreateNewQuestion()
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
} else {
NewResponse(true, "Successfully created new question.").JSON(w, http.StatusOK)
}
}
func EditQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
parameters := mux.Vars(r)
log.Println(parameters)
var question models.Question
id, err := strconv.Atoi(parameters["id"])
if err != nil {
NewResponse(false, "Wrong parameters").JSON(w, http.StatusOK)
} else {
_ = json.NewDecoder(r.Body).Decode(&question)
question.ID = id
err = question.EditQuestion()
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
} else {
NewResponse(true, "Successfully edited this question.").JSON(w, http.StatusOK)
}
}
}
func DeleteQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
parameters := mux.Vars(r)
log.Println(parameters)
var question models.Question
id, err := strconv.Atoi(parameters["id"])
if err != nil {
NewResponse(false, "Wrong parameters").JSON(w, http.StatusOK)
} else {
question.ID = id
err = question.DeleteQuestion()
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
} else {
NewResponse(true, "Successfully deleted this question.").JSON(w, http.StatusOK)
}
}
}
func AnswerQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
parameters := mux.Vars(r)
log.Println(parameters)
questionID, err := strconv.Atoi(parameters["id"])
if err != nil {
NewResponse(false, "Wrong parameters").JSON(w, http.StatusOK)
}
answerID, err := strconv.Atoi(parameters["answer_id"])
if err != nil {
NewResponse(false, "Wrong parameters").JSON(w, http.StatusOK)
}
success, err := models.AnswerQuestion(questionID, answerID)
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
} else {
NewResponse(true, "Question answer method successfully.").Attr("correct_answer", success).JSON(w, http.StatusOK)
}
}
func GetRandomQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
//check for timestamp
parameters := mux.Vars(r)
log.Println(parameters)
categoryID, err := strconv.Atoi(parameters["cat_id"])
if err != nil {
NewResponse(false, "Wrong parameters").JSON(w, http.StatusOK)
return
}
question, err := models.GetRandomQuestionPerCategory(categoryID)
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
return
}
NewResponse(true, "Successfully got a random question.").Attr("question", question).JSON(w, http.StatusOK)
}
func GetSingleQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
decoded := context.Get(r, "decoded")
log.Println(decoded)
parameters := mux.Vars(r)
log.Println(parameters)
questionID, err := strconv.Atoi(parameters["id"])
if err != nil {
NewResponse(false, "Question ID must be an int").JSON(w, http.StatusOK)
return
}
question, err := models.GetSpecificQuestion(questionID)
if err != nil {
NewResponse(false, "SQL Error").JSON(w, http.StatusOK)
return
}
NewResponse(true, "Successfully found the question.").Attr("question", question).JSON(w, http.StatusOK)
}