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+ }
0 commit comments