Skip to content

Commit e7a1bf2

Browse files
committed
feat:admin routes commit:2
1 parent 7252698 commit e7a1bf2

10 files changed

Lines changed: 214 additions & 209 deletions

File tree

database/queries/user.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ 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;
2020
-- name: GetAllUsers :many
21-
SELECT id, email, reg_no, password, role, round_qualified, score, name
21+
SELECT *
2222
FROM users;
23-
-- name: UpgradeUserToRound :exec
23+
-- name: UpgradeUsersToRound :batchexec
2424
UPDATE users
2525
SET round_qualified = GREATEST(round_qualified, $2)
26-
WHERE id = ANY($1::uuid[]);
26+
WHERE id = $1;
2727
-- name: BanUser :exec
2828
UPDATE users
2929
SET is_banned = TRUE

database/schema.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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 DEFAULT FALSE,
10+
is_banned BOOLEAN NOT NULL DEFAULT false,
1111
PRIMARY KEY(id)
1212
);
1313

@@ -41,11 +41,11 @@ CREATE TABLE submissions (
4141

4242
CREATE TABLE testcases (
4343
id UUID NOT NULL UNIQUE,
44-
expected_output TEXT NOT NULL UNIQUE,
45-
memory TEXT NOT NULL UNIQUE,
46-
input TEXT NOT NULL UNIQUE,
47-
hidden BOOLEAN NOT NULL UNIQUE,
48-
runtime DECIMAL NOT NULL UNIQUE,
44+
expected_output TEXT NOT NULL,
45+
memory TEXT NOT NULL,
46+
input TEXT NOT NULL,
47+
hidden BOOLEAN NOT NULL,
48+
runtime DECIMAL NOT NULL,
4949
question_id UUID NOT NULL,
5050
PRIMARY KEY(id)
5151
);
@@ -65,4 +65,4 @@ ON UPDATE NO ACTION ON DELETE NO ACTION;
6565

6666
ALTER TABLE submissions
6767
ADD FOREIGN KEY(user_id) REFERENCES users(id)
68-
ON UPDATE NO ACTION ON DELETE NO ACTION;
68+
ON UPDATE NO ACTION ON DELETE NO ACTION;
Lines changed: 90 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,137 @@
11
package controllers
22

33
import (
4-
"context"
54
"net/http"
6-
"encoding/json"
75
"log"
8-
"github.com/google/uuid"
6+
"github.com/google/uuid"
7+
"fmt"
8+
"github.com/CodeChefVIT/cookoff-backend/internal/db"
99
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
10-
"github.com/go-chi/render"
10+
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1111
)
1212

1313
func GetAllUsers(w http.ResponseWriter, r *http.Request) {
14-
ctx := context.Background()
14+
ctx := r.Context()
1515
users, err := database.Queries.GetAllUsers(ctx)
1616
if err != nil {
17-
http.Error(w, "Unable to fetch users", http.StatusInternalServerError)
17+
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to fetch users")
1818
return
1919
}
20-
render.JSON(w, r, users)
20+
httphelpers.WriteJSON(w, http.StatusOK, users)
2121
}
2222
func UpgradeUserToRound(w http.ResponseWriter, r *http.Request) {
23-
var requestBody map[string]interface{}
24-
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
25-
http.Error(w, "Invalid request payload", http.StatusBadRequest)
26-
return
27-
}
23+
var requestBody struct {
24+
UserIDs []string `json:"user_ids"`
25+
Round float64 `json:"round"`
26+
}
2827

29-
userIDsInterface, ok := requestBody["user_ids"].([]interface{})
30-
if !ok || len(userIDsInterface) == 0 {
31-
http.Error(w, "Invalid user_ids format", http.StatusBadRequest)
32-
return
33-
}
34-
userIDs := make([]uuid.UUID, len(userIDsInterface))
35-
for i, idStr := range userIDsInterface {
36-
id, err := uuid.Parse(idStr.(string))
37-
if err != nil {
38-
http.Error(w, "Invalid user_id", http.StatusBadRequest)
39-
return
40-
}
41-
userIDs[i] = id
42-
}
28+
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
29+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
30+
return
31+
}
4332

44-
roundFloat, ok := requestBody["round"].(float64)
45-
if !ok {
46-
http.Error(w, "Invalid round format", http.StatusBadRequest)
47-
return
48-
}
49-
round := int(roundFloat)
33+
if len(requestBody.UserIDs) == 0 {
34+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_ids format")
35+
return
36+
}
5037

51-
ctx := context.Background()
52-
err := database.Queries.UpgradeUserToRound(ctx, db.UpgradeUserToRoundParams{
53-
Column1: userIDs,
54-
RoundQualified: int32(round),
55-
})
56-
if err != nil {
57-
http.Error(w, "Unable to upgrade users to round", http.StatusInternalServerError)
58-
return
59-
}
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+
}
6058

61-
w.WriteHeader(http.StatusOK)
59+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Users upgraded successfully"})
6260
}
61+
6362
func BanUser(w http.ResponseWriter, r *http.Request) {
64-
var requestBody map[string]string
65-
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
66-
http.Error(w, "Invalid request payload", http.StatusBadRequest)
67-
return
68-
}
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+
}
6968

7069
userIDStr, ok := requestBody["user_id"]
7170
if !ok {
72-
http.Error(w, "user_id not provided", http.StatusBadRequest)
73-
return
71+
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
72+
return
7473
}
7574

76-
userID, err := uuid.Parse(userIDStr)
77-
if err != nil {
78-
http.Error(w, "Invalid user_id", http.StatusBadRequest)
79-
return
80-
}
75+
userID, err := uuid.Parse(userIDStr)
76+
if err != nil {
77+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
78+
return
79+
}
8180

82-
ctx := context.Background()
83-
err = database.Queries.BanUser(ctx, userID)
84-
if err != nil {
85-
http.Error(w, "Unable to ban user", http.StatusInternalServerError)
86-
return
87-
}
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+
}
8887

89-
w.WriteHeader(http.StatusOK)
88+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User banned successfully"})
9089
}
9190
func UnbanUser(w http.ResponseWriter, r *http.Request) {
92-
var requestBody map[string]string
93-
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
94-
http.Error(w, "Invalid request payload", http.StatusBadRequest)
95-
return
96-
}
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+
}
9796

98-
userIDStr, ok := requestBody["user_id"]
97+
userIDStr, ok := requestBody["user_id"]
9998
if !ok {
100-
http.Error(w, "user_id not provided", http.StatusBadRequest)
101-
return
99+
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
100+
return
102101
}
103102

104-
userID, err := uuid.Parse(userIDStr)
105-
if err != nil {
106-
http.Error(w, "Invalid user_id", http.StatusBadRequest)
107-
return
108-
}
103+
userID, err := uuid.Parse(userIDStr)
104+
if err != nil {
105+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
106+
return
107+
}
109108

110-
ctx := context.Background()
111-
err = database.Queries.UnbanUser(ctx, userID)
112-
if err != nil {
113-
http.Error(w, "Unable to unban user", http.StatusInternalServerError)
114-
return
115-
}
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+
}
116115

117-
w.WriteHeader(http.StatusOK)
116+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User unbanned successfully"})
118117
}
119118
type RoundRequest struct {
120-
RoundID string `json:"round_id"`
121-
Enabled bool `json:"enabled"`
119+
RoundID int `json:"round_id"`
122120
}
123121
func SetRoundStatus(w http.ResponseWriter, r *http.Request) {
124122
var reqBody RoundRequest
125-
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
126-
http.Error(w, "Invalid request payload", http.StatusBadRequest)
123+
if err := httphelpers.ParseJSON(r, &reqBody); err != nil {
124+
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
127125
return
128126
}
129-
130-
ctx := context.Background()
131-
status := "disabled"
132-
if reqBody.Enabled {
133-
status = "enabled"
134-
}
135-
136-
err := RedisClient.Set(ctx, reqBody.RoundID, status, 0).Err()
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()
137131
if err != nil {
138-
log.Printf("Failed to update round status: %v\n", err)
139-
http.Error(w, "Failed to update round status", http.StatusInternalServerError)
132+
log.Printf("Failed to enable round: %v\n", err)
133+
httphelpers.WriteError(w, http.StatusInternalServerError, "Failed to enable round")
140134
return
141135
}
142-
143-
w.WriteHeader(http.StatusOK)
144-
w.Write([]byte("Round status updated successfully"))
136+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Round enabled successfully"})
145137
}

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 & 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)