8
8
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
9
9
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
10
10
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
11
+ "github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
11
12
"github.com/go-chi/chi/v5"
12
13
"github.com/google/uuid"
13
14
"github.com/jackc/pgx/v5"
@@ -25,6 +26,17 @@ type Question struct {
25
26
OutputFormat * string `json:"output_format"`
26
27
}
27
28
29
+ type QuestionRequest struct {
30
+ ID uuid.UUID `json:"id" validate:"required"`
31
+ Description * string `json:"description"`
32
+ Title * string `json:"title"`
33
+ InputFormat * string `json:"input_format"`
34
+ Points pgtype.Int4 `json:"points"`
35
+ Round int32 `json:"round"`
36
+ Constraints * string `json:"constraints"`
37
+ OutputFormat * string `json:"output_format"`
38
+ }
39
+
28
40
func GetAllQuestion (w http.ResponseWriter , r * http.Request ) {
29
41
ctx := r .Context ()
30
42
fetchedQuestions , err := database .Queries .GetQuestions (ctx )
@@ -79,6 +91,11 @@ func CreateQuestion(w http.ResponseWriter, r *http.Request) {
79
91
return
80
92
}
81
93
94
+ if err := validator .ValidatePayload (w , question ); err != nil {
95
+ httphelpers .WriteError (w , http .StatusNotAcceptable , "Please provide values for all required fields." )
96
+ return
97
+ }
98
+
82
99
questions , err := database .Queries .CreateQuestion (ctx , db.CreateQuestionParams {
83
100
ID : uuid .New (),
84
101
Description : question .Description ,
@@ -116,12 +133,17 @@ func DeleteQuestion(w http.ResponseWriter, r *http.Request) {
116
133
117
134
func UpdateQuestion (w http.ResponseWriter , r * http.Request ) {
118
135
ctx := r .Context ()
119
- var updateQuestion Question
136
+ var updateQuestion QuestionRequest
120
137
if err := httphelpers .ParseJSON (r , & updateQuestion ); err != nil {
121
138
httphelpers .WriteError (w , http .StatusInternalServerError , err .Error ())
122
139
return
123
140
}
124
141
142
+ if err := validator .ValidatePayload (w , updateQuestion ); err != nil {
143
+ httphelpers .WriteError (w , http .StatusNotAcceptable , "Please provide values for all required fields." )
144
+ return
145
+ }
146
+
125
147
question , err := database .Queries .GetQuestion (ctx , updateQuestion .ID )
126
148
if err != nil {
127
149
if err == pgx .ErrNoRows {
0 commit comments