11package controllers
22
33import (
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
1313func 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}
2222func 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+
6362func 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}
9190func 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}
119118type RoundRequest struct {
120- RoundID string `json:"round_id"`
121- Enabled bool `json:"enabled"`
119+ RoundID int `json:"round_id"`
122120}
123121func 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}
0 commit comments