/
user_games_handlers.go
72 lines (60 loc) · 2.03 KB
/
user_games_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package server
import (
"encoding/json"
"net/http"
"github.com/asankov/gira/pkg/models"
"github.com/gorilla/mux"
)
func (s *Server) handleUsersGamesPatch() authorizedHandler {
return func(w http.ResponseWriter, r *http.Request, user *models.User, token string) {
args := mux.Vars(r)
userGameID := args["id"]
if userGameID == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
var req models.ChangeGameStatusRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.Log.Errorf("Error while decoding body: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if req.Status != "" {
if err := req.Status.Validate(); err != nil {
s.respondError(w, r, err.Error(), http.StatusBadRequest)
return
}
if err := s.GameModel.ChangeGameStatus(user.ID, userGameID, req.Status); err != nil {
s.Log.Errorf("Error while changing game status: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
if req.Progress != nil {
if err := s.GameModel.ChangeGameProgress(user.ID, userGameID, req.Progress); err != nil {
s.Log.Errorf("Error while changing game progress: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
// TODO: better response
s.respond(w, r, nil, http.StatusOK)
}
}
func (s *Server) handleUsersGamesDelete() authorizedHandler {
return func(w http.ResponseWriter, r *http.Request, user *models.User, token string) {
args := mux.Vars(r)
gameID := args["id"]
if gameID == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if err := s.GameModel.DeleteGame(user.ID, gameID); err != nil {
s.Log.Errorf("Error while deleting user game: %v", err)
// TODO: determine if user does not own the game or other error
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
s.respond(w, r, nil, http.StatusOK)
}
}