Skip to content

Commit 1d5ae41

Browse files
authored
Merge pull request #58 from Abhinav-055/master
no time
2 parents 6ebe8aa + 8d6ef7c commit 1d5ae41

11 files changed

Lines changed: 345 additions & 20 deletions

File tree

database/queries/user.sql

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
44
RETURNING *;
55

66
-- name: GetUserByEmail :one
7-
SELECT id, email, reg_no, password, role, round_qualified, score, name
7+
SELECT *
88
FROM users
99
WHERE email = $1;
1010

1111
-- name: GetUserByUsername :one
12-
SELECT id, email, reg_no, password, role, round_qualified, score, name
12+
SELECT *
1313
FROM users
1414
WHERE name = $1;
1515

1616
-- name: GetUserById :one
17-
SELECT id, email, reg_no, password, role, round_qualified, score, name
17+
SELECT *
1818
FROM users
1919
WHERE id = $1;
20+
-- name: GetAllUsers :many
21+
SELECT *
22+
FROM users;
23+
-- name: UpgradeUsersToRound :batchexec
24+
UPDATE users
25+
SET round_qualified = GREATEST(round_qualified, $2)
26+
WHERE id = $1;
27+
-- name: BanUser :exec
28+
UPDATE users
29+
SET is_banned = TRUE
30+
WHERE id = $1;
31+
-- name: UnbanUser :exec
32+
UPDATE users
33+
SET is_banned = FALSE
34+
WHERE id = $1;

database/schema.sql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CREATE TABLE users (
77
round_qualified INTEGER NOT NULL DEFAULT 0,
88
score INTEGER DEFAULT 0,
99
name TEXT NOT NULL,
10+
is_banned BOOLEAN NOT NULL DEFAULT false,
1011
PRIMARY KEY(id)
1112
);
1213

@@ -79,11 +80,3 @@ ON UPDATE NO ACTION ON DELETE CASCADE;
7980
ALTER TABLE submissions
8081
ADD FOREIGN KEY(user_id) REFERENCES users(id)
8182
ON UPDATE NO ACTION ON DELETE CASCADE;
82-
83-
84-
85-
86-
87-
88-
89-

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ go 1.22
55
require (
66
github.com/go-chi/chi/v5 v5.1.0
77
github.com/go-chi/jwtauth/v5 v5.3.1
8+
github.com/go-chi/render v1.0.3
89
github.com/go-playground/validator v9.31.0+incompatible
910
github.com/golang-jwt/jwt/v4 v4.5.0
1011
github.com/google/uuid v1.6.0
1112
github.com/jackc/pgx/v5 v5.6.0
1213
github.com/joho/godotenv v1.5.1
14+
github.com/lib/pq v1.10.9
1315
github.com/redis/go-redis/v9 v9.6.1
1416
go.uber.org/zap v1.27.0
1517
)
1618

1719
require (
20+
github.com/ajg/form v1.5.1 // indirect
1821
github.com/go-playground/locales v0.14.1 // indirect
1922
github.com/go-playground/universal-translator v0.18.1 // indirect
2023
github.com/golang/protobuf v1.5.2 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
2+
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
13
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
24
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
35
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
@@ -23,6 +25,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
2325
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
2426
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
2527
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
28+
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
29+
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
2630
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
2731
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2832
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
@@ -65,6 +69,8 @@ github.com/lestrrat-go/jwx/v2 v2.0.20 h1:sAgXuWS/t8ykxS9Bi2Qtn5Qhpakw1wrcjxChudj
6569
github.com/lestrrat-go/jwx/v2 v2.0.20/go.mod h1:UlCSmKqw+agm5BsOBfEAbTvKsEApaGNqHAEUTv5PJC4=
6670
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
6771
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
72+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
73+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
6874
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6975
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7076
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
"log"
6+
"github.com/google/uuid"
7+
"fmt"
8+
"github.com/CodeChefVIT/cookoff-backend/internal/db"
9+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
10+
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
11+
)
12+
13+
func GetAllUsers(w http.ResponseWriter, r *http.Request) {
14+
ctx := r.Context()
15+
users, err := database.Queries.GetAllUsers(ctx)
16+
if err != nil {
17+
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to fetch users")
18+
return
19+
}
20+
httphelpers.WriteJSON(w, http.StatusOK, users)
21+
}
22+
func UpgradeUserToRound(w http.ResponseWriter, r *http.Request) {
23+
var requestBody struct {
24+
UserIDs []string `json:"user_ids"`
25+
Round float64 `json:"round"`
26+
}
27+
28+
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
29+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
30+
return
31+
}
32+
33+
if len(requestBody.UserIDs) == 0 {
34+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_ids format")
35+
return
36+
}
37+
38+
var upgradeParams []db.UpgradeUsersToRoundParams
39+
for _, idStr := range requestBody.UserIDs {
40+
id, err := uuid.Parse(idStr)
41+
if err != nil {
42+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
43+
return
44+
}
45+
46+
upgradeParams = append(upgradeParams, db.UpgradeUsersToRoundParams{
47+
ID: id,
48+
RoundQualified: int32(requestBody.Round),
49+
})
50+
}
51+
52+
ctx := r.Context()
53+
err := database.Queries.UpgradeUsersToRound(ctx, upgradeParams)
54+
if err != nil {
55+
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to upgrade users to round")
56+
return
57+
}
58+
59+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Users upgraded successfully"})
60+
}
61+
62+
func BanUser(w http.ResponseWriter, r *http.Request) {
63+
var requestBody map[string]string
64+
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
65+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
66+
return
67+
}
68+
69+
userIDStr, ok := requestBody["user_id"]
70+
if !ok {
71+
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
72+
return
73+
}
74+
75+
userID, err := uuid.Parse(userIDStr)
76+
if err != nil {
77+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
78+
return
79+
}
80+
81+
ctx := r.Context()
82+
err = database.Queries.BanUser(ctx, userID)
83+
if err != nil {
84+
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to ban user")
85+
return
86+
}
87+
88+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User banned successfully"})
89+
}
90+
func UnbanUser(w http.ResponseWriter, r *http.Request) {
91+
var requestBody map[string]string
92+
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
93+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
94+
return
95+
}
96+
97+
userIDStr, ok := requestBody["user_id"]
98+
if !ok {
99+
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
100+
return
101+
}
102+
103+
userID, err := uuid.Parse(userIDStr)
104+
if err != nil {
105+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
106+
return
107+
}
108+
109+
ctx := r.Context()
110+
err = database.Queries.UnbanUser(ctx, userID)
111+
if err != nil {
112+
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to unban user")
113+
return
114+
}
115+
116+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User unbanned successfully"})
117+
}
118+
type RoundRequest struct {
119+
RoundID int `json:"round_id"`
120+
}
121+
func SetRoundStatus(w http.ResponseWriter, r *http.Request) {
122+
var reqBody RoundRequest
123+
if err := httphelpers.ParseJSON(r, &reqBody); err != nil {
124+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
125+
return
126+
}
127+
ctx := r.Context()
128+
redisKey := "round:enabled"
129+
roundIDStr := fmt.Sprintf("%d", reqBody.RoundID)
130+
err := database.RedisClient.Set(ctx, redisKey, roundIDStr, 0).Err()
131+
if err != nil {
132+
log.Printf("Failed to enable round: %v\n", err)
133+
httphelpers.WriteError(w, http.StatusInternalServerError, "Failed to enable round")
134+
return
135+
}
136+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Round enabled successfully"})
137+
}

internal/db/batch.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/db.go

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

internal/db/models.go

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

0 commit comments

Comments
 (0)