Skip to content

Commit 8421f2f

Browse files
authored
Merge pull request #18 from BrainNotFoundException/Lavnish/QuestionsOfUsersRound
Lavnish/questions of users round
2 parents 2870bb3 + 694a8ae commit 8421f2f

File tree

12 files changed

+280
-18
lines changed

12 files changed

+280
-18
lines changed

database/queries/questions.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ SET isBountyActive = false
5050
WHERE id = $1;
5151

5252
-- name: DeleteQuestion :exec
53-
DELETE FROM questions WHERE id = $1;
53+
DELETE FROM questions WHERE id = $1;
54+
55+
-- name: GetQuestionsByRound :many
56+
SELECT * FROM questions
57+
WHERE round = $1
58+
ORDER BY qType;

database/queries/user.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ FROM users
5757
WHERE ($1::uuid IS NULL OR id > $1)
5858
ORDER BY id ASC
5959
LIMIT $2;
60+
61+
-- name: GetUserRound :one
62+
SELECT round_qualified
63+
FROM users
64+
WHERE id = $1;

docs/sample.yaml

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ paths:
147147
schema:
148148
$ref: "#/components/schemas/ErrorResponse"
149149

150-
/questions:
150+
/question:
151151
post:
152152
summary: Create a new question
153153
tags:
@@ -198,7 +198,7 @@ paths:
198198
"500":
199199
description: Internal server error
200200

201-
/questions/{id}:
201+
/question/{id}:
202202
get:
203203
summary: Get a question by ID
204204
tags:
@@ -270,8 +270,7 @@ paths:
270270
description: Invalid UUID
271271
"500":
272272
description: Internal server error
273-
274-
/questions/{id}/bounty/activate:
273+
/question/{id}/bounty/activate:
275274
put:
276275
summary: Activate bounty for a question
277276
tags:
@@ -291,7 +290,7 @@ paths:
291290
"500":
292291
description: Internal server error
293292

294-
/questions/{id}/bounty/deactivate:
293+
/question/{id}/bounty/deactivate:
295294
put:
296295
summary: Deactivate bounty for a question
297296
tags:
@@ -310,6 +309,84 @@ paths:
310309
description: Invalid UUID
311310
"500":
312311
description: Internal server error
312+
313+
/question/round:
314+
get:
315+
summary: Get the questions in the user's current round
316+
tags:
317+
- Questions
318+
responses:
319+
"200":
320+
description: Successfully fetched questions and testcases
321+
content:
322+
application/json:
323+
schema:
324+
type: object
325+
properties:
326+
status:
327+
type: string
328+
example: success
329+
round:
330+
type: integer
331+
example: 1
332+
questions_testcases:
333+
type: array
334+
description: Array of question–testcase pairs
335+
items:
336+
type: object
337+
properties:
338+
question:
339+
$ref: "#/components/schemas/Question"
340+
testcases:
341+
type: array
342+
items:
343+
$ref: "#/components/schemas/TestCase"
344+
"400":
345+
description: Invalid round or missing/invalid JWT
346+
content:
347+
application/json:
348+
schema:
349+
type: object
350+
properties:
351+
status:
352+
type: string
353+
example: Failed
354+
message:
355+
type: string
356+
example: round number is invalid
357+
error:
358+
type: string
359+
example: "Error Getting user_id"
360+
"401":
361+
description: Unauthorized — missing or invalid token
362+
content:
363+
application/json:
364+
schema:
365+
type: object
366+
properties:
367+
error:
368+
type: string
369+
example: unauthorized
370+
"500":
371+
description: Internal server error (e.g. DB failure, missing testcases)
372+
content:
373+
application/json:
374+
schema:
375+
type: object
376+
properties:
377+
status:
378+
type: string
379+
example: Failed
380+
message:
381+
type: string
382+
example: unable to fetch the questions
383+
question_id:
384+
type: string
385+
format: uuid
386+
description: Included if fetching testcases for a question fails
387+
error:
388+
type: string
389+
example: "pq: relation \"questions\" does not exist"
313390

314391
/testcase/{id}:
315392
get:
@@ -809,7 +886,7 @@ components:
809886
description: Round qualified (0 or 1)
810887
example: 1
811888
score:
812-
type: string
889+
type: integer
813890
description: User score (numeric type from Postgres)
814891
example: 100
815892
Question:
@@ -862,7 +939,43 @@ components:
862939
- sampleTestInput
863940
- sampleTestOutput
864941
- explanation
865-
942+
TestCase:
943+
type: object
944+
description: A test case for a question
945+
properties:
946+
id:
947+
type: string
948+
format: uuid
949+
description: Unique identifier for the test case
950+
expected_output:
951+
type: string
952+
description: The expected output for the given input
953+
memory:
954+
type: number
955+
format: double
956+
description: Memory limit for this test case (numeric value)
957+
input:
958+
type: string
959+
description: The input provided to the program
960+
hidden:
961+
type: boolean
962+
description: Whether the test case is hidden from the user
963+
runtime:
964+
type: number
965+
format: double
966+
description: Runtime limit for this test case (decimal value)
967+
question_id:
968+
type: string
969+
format: uuid
970+
description: ID of the question this test case belongs to
971+
required:
972+
- id
973+
- expected_output
974+
- memory
975+
- input
976+
- hidden
977+
- runtime
978+
- question_id
866979
CreateQuestionParams:
867980
type: object
868981
properties:

pkg/controllers/questions.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/db"
7+
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/helpers/auth"
78
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/utils"
89
"github.com/google/uuid"
910
"github.com/labstack/echo/v4"
@@ -67,6 +68,70 @@ func GetAllQuestions(c echo.Context) error {
6768
})
6869
}
6970

71+
func GetQuestionsByRound(c echo.Context) error {
72+
73+
userID, err := auth.GetUserID(c)
74+
75+
if err != nil {
76+
return c.JSON(http.StatusBadRequest, echo.Map{
77+
"status": "Failed",
78+
"message": "Error Getting user_id",
79+
"error": err.Error(),
80+
})
81+
}
82+
83+
round, err := utils.Queries.GetUserRound(c.Request().Context(), userID)
84+
85+
if err != nil {
86+
return c.JSON(http.StatusInternalServerError, echo.Map{
87+
"status": "Failed",
88+
"message": "could not get the users current round",
89+
"error": err.Error(),
90+
})
91+
}
92+
93+
if round < 0 || round > 3 {
94+
return c.JSON(http.StatusBadRequest, echo.Map{
95+
"status": "Failed",
96+
"message": "round number is invalid",
97+
})
98+
}
99+
100+
questions, err := utils.Queries.GetQuestionsByRound(c.Request().Context(), int32(round))
101+
102+
if err != nil {
103+
return c.JSON(http.StatusInternalServerError, echo.Map{
104+
"status": "Failed",
105+
"message": "unable to fetch the questions",
106+
"error": err.Error(),
107+
})
108+
}
109+
110+
result := []echo.Map{}
111+
112+
for _, q := range questions {
113+
testcases, err := utils.Queries.GetTestCasesByQuestion(c.Request().Context(), q.ID)
114+
if err != nil {
115+
return c.JSON(http.StatusInternalServerError, echo.Map{
116+
"status": "Failed",
117+
"message": "Could not get the testcases for question",
118+
"question_id": q.ID,
119+
"error": err.Error(),
120+
})
121+
}
122+
result = append(result, echo.Map{
123+
"question": q,
124+
"testcases": testcases,
125+
})
126+
}
127+
128+
return c.JSON(http.StatusOK, echo.Map{
129+
"status": "success",
130+
"round": round,
131+
"questions_testcases": result,
132+
})
133+
}
134+
70135
func UpdateQuestion(c echo.Context) error {
71136
id, err := uuid.Parse(c.Param("id"))
72137
if err != nil {

pkg/db/db.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/db/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/db/questions.sql.go

Lines changed: 41 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/db/submission.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/db/testcases.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/db/user.sql.go

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)