Skip to content

Commit 919b8a0

Browse files
change all responses to switch to types.ApiResponse
1 parent 321ddcb commit 919b8a0

File tree

5 files changed

+97
-24
lines changed

5 files changed

+97
-24
lines changed

backend/internal/controllers/game.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"labyrinth/internal/router"
7+
"labyrinth/internal/types"
78
"net/http"
89
)
910

@@ -32,7 +33,14 @@ func GameConfigHandler(rtr *router.Router) http.HandlerFunc {
3233
return
3334
}
3435

35-
err = json.NewEncoder(w).Encode(gameConfig)
36+
responsePayload, _ := json.Marshal(gameConfig)
37+
apiResponse := types.ApiResponse{
38+
Success: true,
39+
Message: "",
40+
Payload: responsePayload,
41+
}
42+
43+
err = json.NewEncoder(w).Encode(apiResponse)
3644
if err != nil {
3745
//http.Error(w, "internal server error", http.StatusInternalServerError)
3846
w.WriteHeader(http.StatusInternalServerError)

backend/internal/controllers/team.go

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TeamCreationHandler(rtr *router.Router) http.HandlerFunc {
4040
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
4141

4242
//http.Error(w, "error reading teamName field, invalid json payload", http.StatusBadRequest)
43-
w.WriteHeader(http.StatusBadRequest)
43+
//w.WriteHeader(http.StatusBadRequest)
4444
apiResponse := types.ApiResponse{
4545
Success: false,
4646
Message: "invalid json payload",
@@ -94,9 +94,15 @@ func TeamCreationHandler(rtr *router.Router) http.HandlerFunc {
9494

9595
go teamChannel.Start()
9696

97-
json.NewEncoder(w).Encode(map[string]string{
98-
"team_id": teamId,
99-
})
97+
payload, _ := json.Marshal(map[string]string{"team_id": teamId})
98+
99+
apiResponse := types.ApiResponse{
100+
Success: true,
101+
Message: "success",
102+
Payload: payload,
103+
}
104+
105+
json.NewEncoder(w).Encode(apiResponse)
100106

101107
})
102108

@@ -126,7 +132,16 @@ func TeamUpdateHandler(rtr *router.Router) http.HandlerFunc {
126132

127133
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
128134

129-
http.Error(w, "error getting team_id field, invalid json payload", http.StatusBadRequest)
135+
//http.Error(w, "error getting team_id field, invalid json payload", http.StatusBadRequest)
136+
137+
apiResponse := types.ApiResponse{
138+
Success: false,
139+
Message: "error getting team_id from json payload",
140+
Payload: nil,
141+
}
142+
143+
json.NewEncoder(w).Encode(apiResponse)
144+
130145
return
131146

132147
}
@@ -155,7 +170,15 @@ func TeamUpdateHandler(rtr *router.Router) http.HandlerFunc {
155170
teamChannel := rtr.State.ChanPool.GetChannel(team.ID)
156171
teamChannel.Broadcast(protocol.Packet{Type: "BackgroundMessage", BackgroundMessage: protocol.BackgroundMessage{Relay: fmt.Sprintf("teamId:%s -> %s joined the team", team.ID, profile.Email), MsgContext: "channel_creation"}})
157172

158-
if err := json.NewEncoder(w).Encode(team); err != nil {
173+
payload, _ := json.Marshal(team)
174+
175+
apiResponse := types.ApiResponse{
176+
Success: true,
177+
Message: "Member added to team successfully!",
178+
Payload: payload,
179+
}
180+
181+
if err := json.NewEncoder(w).Encode(apiResponse); err != nil {
159182
http.Error(w, "error encoding response", http.StatusInternalServerError)
160183
}
161184

@@ -186,10 +209,18 @@ func GetTeamHandler(rtr *router.Router) http.HandlerFunc {
186209

187210
if teamId == "" && userId == "" {
188211
//http.Error(w, "Either user_id or team_id must be provided", http.StatusBadRequest)
189-
w.WriteHeader(http.StatusBadRequest)
190-
json.NewEncoder(w).Encode(map[string]string{
191-
"error": "either used_id or team_id not provided",
192-
})
212+
//w.WriteHeader(http.StatusBadRequest)
213+
//json.NewEncoder(w).Encode(map[string]string{
214+
// "error": "either user_id or team_id not provided",
215+
//})
216+
217+
apiResponse := types.ApiResponse{
218+
Success: false,
219+
Message: "either user_id or team_id not provided",
220+
Payload: nil,
221+
}
222+
json.NewEncoder(w).Encode(apiResponse)
223+
193224
return
194225
}
195226

@@ -202,18 +233,26 @@ func GetTeamHandler(rtr *router.Router) http.HandlerFunc {
202233
parsedId, parseErr := uuid.Parse(userId)
203234
if parseErr != nil {
204235
//http.Error(w, "invalid player_id format", http.StatusBadRequest)
205-
w.WriteHeader(http.StatusBadRequest)
206-
json.NewEncoder(w).Encode(map[string]string{
207-
"error": "player id format is invalid, should be uuid string",
208-
})
236+
//w.WriteHeader(http.StatusBadRequest)
237+
//json.NewEncoder(w).Encode(map[string]string{
238+
// "error": "player id format is invalid, should be uuid string",
239+
//})
240+
241+
apiResponse := types.ApiResponse{
242+
Success: false,
243+
Message: "player id format is invalid, should be uuid string",
244+
Payload: nil,
245+
}
246+
json.NewEncoder(w).Encode(apiResponse)
247+
209248
}
210249
team, err = rtr.State.DB.GetTeamByUserId(context.Background(), parsedId)
211250
}
212251

213252
if err != nil {
214253
rtr.Logger.Error("failed to fetch team", slog.String("error", err.Error()))
215254
//http.Error(w, "internal server error", http.StatusInternalServerError)
216-
w.WriteHeader(http.StatusBadRequest)
255+
w.WriteHeader(http.StatusInternalServerError)
217256
json.NewEncoder(w).Encode(map[string]string{
218257
"error": "internal server error",
219258
})

backend/internal/controllers/user.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"labyrinth/internal/router"
7+
"labyrinth/internal/types"
78
"net/http"
89
)
910

@@ -31,10 +32,19 @@ func TeamMemberStatusUpdateHandler(rtr *router.Router) http.HandlerFunc {
3132

3233
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
3334
//http.Error(w, "error reading userStatus field, invalid json payload", http.StatusBadRequest)
34-
w.WriteHeader(http.StatusBadRequest)
35-
json.NewEncoder(w).Encode(map[string]string{
36-
"error": "invalid json payload",
37-
})
35+
//w.WriteHeader(http.StatusBadRequest)
36+
//json.NewEncoder(w).Encode(map[string]string{
37+
// "error": "invalid json payload",
38+
//})
39+
40+
apiResponse := types.ApiResponse{
41+
Success: false,
42+
Message: "invalid json payload",
43+
Payload: nil,
44+
}
45+
46+
json.NewEncoder(w).Encode(apiResponse)
47+
3848
return
3949
}
4050

@@ -71,7 +81,16 @@ func TeamMemberStatusUpdateHandler(rtr *router.Router) http.HandlerFunc {
7181
return
7282
}
7383

74-
err = json.NewEncoder(w).Encode(team)
84+
responsePayload, _ := json.Marshal(team)
85+
86+
apiResponse := types.ApiResponse{
87+
Success: true,
88+
Message: "Successfully updated ready status!",
89+
Payload: responsePayload,
90+
}
91+
92+
err = json.NewEncoder(w).Encode(apiResponse)
93+
7594
if err != nil {
7695
//http.Error(w, "error encoding json", http.StatusInternalServerError)
7796
w.WriteHeader(http.StatusInternalServerError)

backend/internal/protocol/protocol.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ type Packet struct {
77
GameMessage `json:"gameMessage"`
88
}
99

10+
type TeamPacket struct {
11+
Relay string `json:"message"`
12+
MsgContext string `json:"msgcontext"`
13+
}
14+
1015
type BackgroundMessage struct {
1116
Relay string `json:"message"`
1217
MsgContext string `json:"msgcontext"`

backend/internal/types/response.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package types
22

3+
import "encoding/json"
4+
35
type ApiResponse struct {
4-
Success bool `json:"success"`
5-
Message string `json:"message"`
6-
Payload map[string]interface{} `json:"payload"`
6+
Success bool `json:"success"`
7+
Message string `json:"message"`
8+
Payload json.RawMessage `json:"payload,omitempty"`
79
}

0 commit comments

Comments
 (0)