Skip to content

Commit

Permalink
Merge pull request #58 from Abhinav-055/master
Browse files Browse the repository at this point in the history
no time
  • Loading branch information
Killerrekt authored Sep 27, 2024
2 parents 6ebe8aa + 8d6ef7c commit 1d5ae41
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 20 deletions.
21 changes: 18 additions & 3 deletions database/queries/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;

-- name: GetUserByEmail :one
SELECT id, email, reg_no, password, role, round_qualified, score, name
SELECT *
FROM users
WHERE email = $1;

-- name: GetUserByUsername :one
SELECT id, email, reg_no, password, role, round_qualified, score, name
SELECT *
FROM users
WHERE name = $1;

-- name: GetUserById :one
SELECT id, email, reg_no, password, role, round_qualified, score, name
SELECT *
FROM users
WHERE id = $1;
-- name: GetAllUsers :many
SELECT *
FROM users;
-- name: UpgradeUsersToRound :batchexec
UPDATE users
SET round_qualified = GREATEST(round_qualified, $2)
WHERE id = $1;
-- name: BanUser :exec
UPDATE users
SET is_banned = TRUE
WHERE id = $1;
-- name: UnbanUser :exec
UPDATE users
SET is_banned = FALSE
WHERE id = $1;
9 changes: 1 addition & 8 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE users (
round_qualified INTEGER NOT NULL DEFAULT 0,
score INTEGER DEFAULT 0,
name TEXT NOT NULL,
is_banned BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY(id)
);

Expand Down Expand Up @@ -79,11 +80,3 @@ ON UPDATE NO ACTION ON DELETE CASCADE;
ALTER TABLE submissions
ADD FOREIGN KEY(user_id) REFERENCES users(id)
ON UPDATE NO ACTION ON DELETE CASCADE;








3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ go 1.22
require (
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/jwtauth/v5 v5.3.1
github.com/go-chi/render v1.0.3
github.com/go-playground/validator v9.31.0+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/redis/go-redis/v9 v9.6.1
go.uber.org/zap v1.27.0
)

require (
github.com/ajg/form v1.5.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
Expand All @@ -23,6 +25,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
Expand Down Expand Up @@ -65,6 +69,8 @@ github.com/lestrrat-go/jwx/v2 v2.0.20 h1:sAgXuWS/t8ykxS9Bi2Qtn5Qhpakw1wrcjxChudj
github.com/lestrrat-go/jwx/v2 v2.0.20/go.mod h1:UlCSmKqw+agm5BsOBfEAbTvKsEApaGNqHAEUTv5PJC4=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
Expand Down
137 changes: 137 additions & 0 deletions internal/controllers/admin_wishes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package controllers

import (
"net/http"
"log"
"github.com/google/uuid"
"fmt"
"github.com/CodeChefVIT/cookoff-backend/internal/db"
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
)

func GetAllUsers(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
users, err := database.Queries.GetAllUsers(ctx)
if err != nil {
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to fetch users")
return
}
httphelpers.WriteJSON(w, http.StatusOK, users)
}
func UpgradeUserToRound(w http.ResponseWriter, r *http.Request) {
var requestBody struct {
UserIDs []string `json:"user_ids"`
Round float64 `json:"round"`
}

if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
return
}

if len(requestBody.UserIDs) == 0 {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_ids format")
return
}

var upgradeParams []db.UpgradeUsersToRoundParams
for _, idStr := range requestBody.UserIDs {
id, err := uuid.Parse(idStr)
if err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
return
}

upgradeParams = append(upgradeParams, db.UpgradeUsersToRoundParams{
ID: id,
RoundQualified: int32(requestBody.Round),
})
}

ctx := r.Context()
err := database.Queries.UpgradeUsersToRound(ctx, upgradeParams)
if err != nil {
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to upgrade users to round")
return
}

httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Users upgraded successfully"})
}

func BanUser(w http.ResponseWriter, r *http.Request) {
var requestBody map[string]string
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
return
}

userIDStr, ok := requestBody["user_id"]
if !ok {
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
return
}

userID, err := uuid.Parse(userIDStr)
if err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
return
}

ctx := r.Context()
err = database.Queries.BanUser(ctx, userID)
if err != nil {
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to ban user")
return
}

httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User banned successfully"})
}
func UnbanUser(w http.ResponseWriter, r *http.Request) {
var requestBody map[string]string
if err := httphelpers.ParseJSON(r, &requestBody); err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
return
}

userIDStr, ok := requestBody["user_id"]
if !ok {
httphelpers.WriteError(w, http.StatusBadRequest, "user_id must be a string")
return
}

userID, err := uuid.Parse(userIDStr)
if err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid user_id")
return
}

ctx := r.Context()
err = database.Queries.UnbanUser(ctx, userID)
if err != nil {
httphelpers.WriteError(w, http.StatusInternalServerError, "Unable to unban user")
return
}

httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "User unbanned successfully"})
}
type RoundRequest struct {
RoundID int `json:"round_id"`
}
func SetRoundStatus(w http.ResponseWriter, r *http.Request) {
var reqBody RoundRequest
if err := httphelpers.ParseJSON(r, &reqBody); err != nil {
httphelpers.WriteError(w, http.StatusBadRequest, "Invalid request payload")
return
}
ctx := r.Context()
redisKey := "round:enabled"
roundIDStr := fmt.Sprintf("%d", reqBody.RoundID)
err := database.RedisClient.Set(ctx, redisKey, roundIDStr, 0).Err()
if err != nil {
log.Printf("Failed to enable round: %v\n", err)
httphelpers.WriteError(w, http.StatusInternalServerError, "Failed to enable round")
return
}
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Round enabled successfully"})
}
69 changes: 69 additions & 0 deletions internal/db/batch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d5ae41

Please sign in to comment.