88 "github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
99 "github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
1010 httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
11+ "github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1112 "github.com/go-chi/chi/v5"
1213 "github.com/google/uuid"
1314 "github.com/jackc/pgx/v5"
@@ -25,6 +26,17 @@ type Question struct {
2526 OutputFormat * string `json:"output_format"`
2627}
2728
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+
2840func GetAllQuestion (w http.ResponseWriter , r * http.Request ) {
2941 ctx := r .Context ()
3042 fetchedQuestions , err := database .Queries .GetQuestions (ctx )
@@ -79,6 +91,11 @@ func CreateQuestion(w http.ResponseWriter, r *http.Request) {
7991 return
8092 }
8193
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+
8299 questions , err := database .Queries .CreateQuestion (ctx , db.CreateQuestionParams {
83100 ID : uuid .New (),
84101 Description : question .Description ,
@@ -116,12 +133,17 @@ func DeleteQuestion(w http.ResponseWriter, r *http.Request) {
116133
117134func UpdateQuestion (w http.ResponseWriter , r * http.Request ) {
118135 ctx := r .Context ()
119- var updateQuestion Question
136+ var updateQuestion QuestionRequest
120137 if err := httphelpers .ParseJSON (r , & updateQuestion ); err != nil {
121138 httphelpers .WriteError (w , http .StatusInternalServerError , err .Error ())
122139 return
123140 }
124141
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+
125147 question , err := database .Queries .GetQuestion (ctx , updateQuestion .ID )
126148 if err != nil {
127149 if err == pgx .ErrNoRows {
0 commit comments