Skip to content

Commit 56ed5f4

Browse files
2 parents 174630d + 7e31e14 commit 56ed5f4

23 files changed

Lines changed: 230 additions & 170 deletions

database/queries/ideas.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ SELECT EXISTS (
1818
WHERE team_id = $1
1919
);
2020

21+
-- name: SetIsSelectedByTeamID :exec
22+
UPDATE ideas SET is_selected = $2
23+
WHERE team_id = $1;
24+
2125
-- name: GetIdeasByTeamID :one
2226
SELECT * from ideas WHERE team_id = $1;
2327

database/queries/score.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ INSERT INTO score (
66
design,
77
implementation,
88
presentation,
9-
round
9+
round,
10+
panel
1011
)
11-
VALUES ($1, $2, $3, $4, $5, $6, $7)
12+
VALUES ($1, $2, $3, $4, $5, $6, $7,$8)
1213
RETURNING *;
1314

1415
-- name: GetScoreByTeamAndRound :one
@@ -48,8 +49,8 @@ RETURNING *;
4849

4950
-- name: DeleteScore :one
5051
DELETE FROM score
51-
WHERE id = $1
52-
RETURNING id;
52+
WHERE id = $1 AND round = $2
53+
RETURNING *;
5354

5455
-- name: GetLeaderboard :many
5556
WITH TotalScores AS (

database/queries/submission.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ SET
2525
title = $1,
2626
github_link = $2,
2727
figma_link = $3,
28-
other_link = $4
29-
WHERE team_id = $5
28+
other_link = $4,
29+
description = $5
30+
WHERE team_id = $6
3031
RETURNING *;
3132

3233
-- name: GetSubmissionByTeamID :one

database/queries/teams.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ UPDATE teams
102102
SET round_qualified = round_qualified + 1
103103
WHERE id = $1;
104104

105+
-- name: UpdateTeamScore :exec
106+
UPDATE teams
107+
SET total_score = total_score + $1
108+
WHERE id = $2;

database/queries/users.sql

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,14 @@ FROM users
2222
WHERE email = $1;
2323

2424
-- name: GetAllUsers :many
25-
SELECT
26-
u.id,
27-
u.team_id,
28-
u.name,
29-
u.email,
30-
u.phone_no,
31-
u.reg_no,
32-
u.gender,
33-
u.residency,
34-
u.hostel_block,
35-
u.room_no,
36-
u.github_profile,
37-
u.google_id,
38-
u.google_profile_pic,
39-
u.role,
40-
u.is_leader,
41-
u.is_banned,
42-
u.is_profile_complete,
43-
t.round_qualified
25+
SELECT u.*, t.round_qualified
4426
FROM users u
45-
JOIN teams t
46-
ON t.id = u.team_id
47-
WHERE (
48-
$1 = ''
49-
OR u.name ILIKE '%' || $1 || '%'
50-
OR u.reg_no ILIKE '%' || $1 || '%'
51-
OR u.email ILIKE '%' || $1 || '%'
52-
)
53-
AND (
54-
$2::uuid IS NULL
55-
OR u.id > $2
56-
)
57-
AND (
58-
$4 = ''
59-
OR u.gender = $4
60-
)
27+
JOIN teams t ON t.id = u.team_id
28+
WHERE (u.name LIKE '%' || $1 || '%'
29+
OR u.reg_no LIKE '%' || $1 || '%'
30+
OR u.email LIKE '%' || $1 || '%')
31+
AND u.id > $2
32+
AND ($4 = '' OR u.gender = $4)
6133
ORDER BY u.id
6234
LIMIT $3;
6335

pkg/controllers/admin.go

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -524,65 +524,63 @@ func DeletePanelMember(c echo.Context) error {
524524
})
525525
}
526526

527-
func GetAllUsers(ec echo.Context) error {
528-
ctx := ec.Request().Context()
529-
limitParam := ec.QueryParam("limit")
530-
cursor := ec.QueryParam("cursor")
531-
name := ec.QueryParam("name")
532-
gender := ec.QueryParam("gender")
533-
534-
limit := 20
535-
if limitParam != "" {
536-
lt, err := strconv.Atoi(limitParam)
537-
if err != nil || lt <= 0 {
538-
return ec.JSON(http.StatusBadRequest, &models.Response{
539-
Success: false,
540-
Message: "limit must be a valid positive integer",
541-
})
542-
}
543-
limit = lt
527+
func GetAllUsers(c echo.Context) error {
528+
ctx := c.Request().Context()
529+
limitParam := c.QueryParam("limit")
530+
cursor := c.QueryParam("cursor")
531+
name := c.QueryParam("name")
532+
gender := c.QueryParam("gender")
533+
534+
limit, err := strconv.Atoi(limitParam)
535+
if err != nil {
536+
return c.JSON(http.StatusInternalServerError, &models.Response{
537+
Success: false,
538+
Message: "Invalid limit parameter",
539+
Data: map[string]string{"error": err.Error()},
540+
})
544541
}
545542

546-
var cursorUUID pgtype.UUID
543+
var cursorUUID uuid.UUID
547544
if cursor == "" {
548-
cursorUUID = pgtype.UUID{Valid: false}
545+
cursorUUID = uuid.Nil
549546
} else {
550-
if err := cursorUUID.Scan(cursor); err != nil {
551-
return ec.JSON(http.StatusBadRequest, &models.Response{
552-
Success: false,
553-
Message: "Invalid UUID for cursor",
547+
cursorUUID, err = uuid.Parse(cursor)
548+
if err != nil {
549+
return c.JSON(http.StatusBadRequest, map[string]string{
550+
"error": "Invalid UUID for cursor",
554551
})
555552
}
556553
}
557554

558555
users, err := db.Queries.GetAllUsers(ctx, sqlc.GetAllUsersParams{
559-
Column1: name,
560-
Column2: cursorUUID,
561-
Limit: int32(limit + 1),
556+
Limit: int32(limit),
557+
ID: pgtype.UUID{Bytes: cursorUUID, Valid: true},
558+
Column1: &name,
562559
Column4: gender,
563560
})
564561
if err != nil {
565-
return ec.JSON(http.StatusInternalServerError, &models.Response{
562+
return c.JSON(http.StatusInternalServerError, &models.Response{
566563
Success: false,
567-
Message: err.Error(),
564+
Message: "Failed to fetch users",
565+
Data: map[string]string{"error": err.Error()},
568566
})
569567
}
570568

571-
var nextCursorStr string
572-
if len(users) > int(limit) {
573-
nextCursor := users[len(users)-1].ID
574-
nextCursorStr = nextCursor.String()
575-
users = users[:limit]
576-
} else {
577-
nextCursorStr = ""
569+
var nextCursor uuid.NullUUID
570+
571+
for _, user := range users {
572+
nextCursor = uuid.NullUUID{
573+
UUID: uuid.UUID(user.ID.Bytes),
574+
Valid: true,
575+
}
578576
}
579577

580-
return ec.JSON(http.StatusOK, &models.Response{
578+
return c.JSON(http.StatusOK, &models.Response{
581579
Success: true,
582580
Message: "Users fetched successfully",
583581
Data: map[string]interface{}{
584582
"users": users,
585-
"next_cursor": nextCursorStr,
583+
"next_cursor": nextCursor.UUID.String(),
586584
},
587585
})
588586
}
@@ -1002,6 +1000,17 @@ func IncrementTeamRound(c echo.Context) error {
10021000
Data: map[string]string{"error": err.Error()},
10031001
})
10041002
}
1003+
1004+
if err := db.Queries.SetIsSelectedByTeamID(ctx, sqlc.SetIsSelectedByTeamIDParams{
1005+
TeamID: teamUUID,
1006+
IsSelected: true,
1007+
}); err != nil {
1008+
return c.JSON(http.StatusInternalServerError, &models.Response{
1009+
Success: false,
1010+
Message: "failed to update team selection status",
1011+
Data: map[string]string{"team_id": teamUUID.String(), "error": err.Error()},
1012+
})
1013+
}
10051014
}
10061015

10071016
return c.JSON(http.StatusOK, &models.Response{

pkg/controllers/scores.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func SetScore(c echo.Context) error {
4444
})
4545
}
4646
roundVal := *req.Round
47-
// if len(req.Panel) == 0 {
48-
// return c.JSON(http.StatusBadRequest, &models.Response{
49-
// Success: false,
50-
// Message: "Panel is required",
51-
// })
52-
// }
47+
if len(req.Panel) == 0 {
48+
return c.JSON(http.StatusBadRequest, &models.Response{
49+
Success: false,
50+
Message: "Panel is required",
51+
})
52+
}
5353
team, err := db.Queries.GetTeamById(ctx, teamUUID)
5454
if err != nil {
5555
return c.JSON(http.StatusNotFound, &models.Response{
@@ -111,7 +111,7 @@ func SetScore(c echo.Context) error {
111111
Implementation: implementation,
112112
Presentation: presentation,
113113
Round: roundVal,
114-
// Panel: req.Panel,
114+
Panel: req.Panel,
115115
}
116116

117117
score, err := db.Queries.CreateScore(ctx, params)
@@ -121,7 +121,19 @@ func SetScore(c echo.Context) error {
121121
Message: "Failed to create score",
122122
})
123123
}
124-
124+
total := design + implementation + presentation
125+
if total > 0 {
126+
err := db.Queries.UpdateTeamScore(ctx, sqlc.UpdateTeamScoreParams{
127+
TotalScore: total,
128+
ID: teamUUID,
129+
})
130+
if err != nil {
131+
return c.JSON(http.StatusInternalServerError, &models.Response{
132+
Success: false,
133+
Message: "Failed to update team total score",
134+
})
135+
}
136+
}
125137
return c.JSON(http.StatusOK, &models.Response{
126138
Success: true,
127139
Message: "Score set successfully",
@@ -277,9 +289,21 @@ func DeleteScore(c echo.Context) error {
277289
})
278290
}
279291

280-
_, err = db.Queries.DeleteScore(ctx, pgtype.UUID{
281-
Bytes: scoreIDRaw,
282-
Valid: true,
292+
round, err := strconv.Atoi(c.Param("round"))
293+
if err != nil {
294+
return c.JSON(http.StatusBadRequest, &models.Response{
295+
Success: false,
296+
Message: "Invalid round",
297+
Data: err,
298+
})
299+
}
300+
301+
score, err := db.Queries.DeleteScore(ctx, sqlc.DeleteScoreParams{
302+
ID: pgtype.UUID{
303+
Bytes: scoreIDRaw,
304+
Valid: true,
305+
},
306+
Round: int32(round),
283307
})
284308
if err != nil {
285309
if err == pgx.ErrNoRows {
@@ -296,8 +320,24 @@ func DeleteScore(c echo.Context) error {
296320
})
297321
}
298322

323+
negativeTotal := -(score.Design + score.Implementation + score.Presentation)
324+
if negativeTotal != 0 {
325+
err := db.Queries.UpdateTeamScore(ctx, sqlc.UpdateTeamScoreParams{
326+
TotalScore: negativeTotal,
327+
ID: score.TeamID,
328+
})
329+
if err != nil {
330+
return c.JSON(http.StatusInternalServerError, &models.Response{
331+
Success: false,
332+
Message: "Failed to update team total score",
333+
Data: err,
334+
})
335+
}
336+
}
337+
299338
return c.JSON(http.StatusOK, &models.Response{
300339
Success: true,
301340
Message: "Score successfully deleted",
341+
Data: utils.ToScoreResponse(score),
302342
})
303343
}

pkg/controllers/submission.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ func UpdateSubmission(c echo.Context) error {
130130
}
131131

132132
submission, err := db.Queries.UpdateSubmission(ctx, sqlc.UpdateSubmissionParams{
133-
TeamID: teamID,
134-
Title: req.Title,
135-
GithubLink: req.GithubLink,
136-
FigmaLink: req.FigmaLink,
137-
OtherLink: req.OtherLink,
133+
TeamID: teamID,
134+
Title: req.Title,
135+
Description: req.Description,
136+
GithubLink: req.GithubLink,
137+
FigmaLink: req.FigmaLink,
138+
OtherLink: req.OtherLink,
138139
})
139140
if err != nil {
140141
log.Println("Error while updating submission on db: ", err)

pkg/db/sqlc/comments.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/sqlc/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.

0 commit comments

Comments
 (0)