Skip to content

Commit cce4c47

Browse files
Retrieving questions by user id and function to get user id
1 parent 8fdafa6 commit cce4c47

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

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/helpers/auth/jwt.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package auth
22

33
import (
4+
"fmt"
45
"time"
56

67
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/db"
78
"github.com/CodeChefVIT/cookoff-10.0-be/pkg/utils"
89
"github.com/golang-jwt/jwt/v5"
10+
"github.com/google/uuid"
11+
"github.com/labstack/echo/v4"
912
)
1013

1114
var jwtSecret = []byte(utils.Config.JwtSecret)
1215

1316
type AccessTokenClaims struct {
1417
Username string `json:"username"`
15-
UserID string `json:"user_id"`
18+
UserID string `json:"user_id"`
1619
Role string `json:"role"`
1720
Type string `json:"type"`
1821
jwt.RegisteredClaims
1922
}
2023

2124
type RefreshTokenClaims struct {
22-
UserID string `json:"user_id"`
23-
Type string `json:"type"`
25+
UserID string `json:"user_id"`
26+
Type string `json:"type"`
2427
jwt.RegisteredClaims
2528
}
2629

2730
func CreateAccessToken(user *db.User) (string, error) {
28-
expirationTime := time.Now().Add(1*time.Minute)
31+
expirationTime := time.Now().Add(1 * time.Minute)
2932
claims := &AccessTokenClaims{
3033
Username: user.Name,
3134
UserID: user.ID.String(),
@@ -53,3 +56,20 @@ func CreateRefreshToken(user *db.User) (string, error) {
5356
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
5457
return token.SignedString(jwtSecret)
5558
}
59+
60+
func GetUserID(c echo.Context) (uuid.UUID, error) {
61+
user := c.Get("user").(*jwt.Token)
62+
claims := user.Claims.(jwt.MapClaims)
63+
64+
userIDStr, ok := claims["user_id"].(string)
65+
if !ok {
66+
return uuid.UUID{}, fmt.Errorf("user_id not found in token")
67+
}
68+
69+
uid, err := uuid.Parse(userIDStr)
70+
if err != nil {
71+
return uuid.UUID{}, fmt.Errorf("invalid user_id format: %v", err)
72+
}
73+
74+
return uid, nil
75+
}

0 commit comments

Comments
 (0)