-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextract.go
266 lines (223 loc) · 7.56 KB
/
extract.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package extract
import (
"errors"
"net/http"
"strconv"
"github.com/taironas/gonawin/helpers"
"github.com/taironas/gonawin/helpers/log"
mdl "github.com/taironas/gonawin/models"
"github.com/taironas/route"
"appengine"
)
// Context type holds the information needed to read the request and log any errors.
//
type Context struct {
c appengine.Context // appengine context
desc string // handler description
r *http.Request // the HTTP request
}
// NewContext returns a new Context.
//
func NewContext(c appengine.Context, desc string, r *http.Request) Context {
return Context{c, desc, r}
}
// UserId returns a userId.
// It gets the 'userId' from the request and parses it to int64
//
func (c Context) UserId() (int64, error) {
strUserID, err := route.Context.Get(c.r, "userId")
if err != nil {
log.Errorf(c.c, "%s error getting user id, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeUserNotFound)}
}
var userID int64
userID, err = strconv.ParseInt(strUserID, 0, 64)
if err != nil {
log.Errorf(c.c, "%s error converting user id from string to int64, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeUserNotFound)}
}
return userID, nil
}
// User returns a User object from the request passed in the context.
//
func (c Context) User() (*mdl.User, error) {
userID, err := c.UserId()
if err != nil {
return nil, err
}
var u *mdl.User
if u, err = mdl.UserByID(c.c, userID); err != nil {
log.Errorf(c.c, "%s user not found", c.desc)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeUserNotFound)}
}
return u, nil
}
// Admin returns a admin mdl.User object with respect to the
// userId passed as param.
//
func (c Context) Admin(userID int64) (*mdl.User, error) {
a, err := mdl.UserByID(c.c, userID)
if err != nil {
log.Errorf(c.c, "%s user not found", c.desc)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeUserNotFound)}
}
return a, nil
}
// TeamId returns the team identifier.
// It gets the 'teamID' from the request and parses it to int64
//
func (c Context) TeamId() (int64, error) {
strTeamID, err := route.Context.Get(c.r, "teamId")
if err != nil {
log.Errorf(c.c, "%s error getting team id, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTeamNotFound)}
}
var teamID int64
teamID, err = strconv.ParseInt(strTeamID, 0, 64)
if err != nil {
log.Errorf(c.c, "%s error converting team id from string to int64, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTeamNotFound)}
}
return teamID, nil
}
// Team returns a team object from the request passed in the context.
//
func (c Context) Team() (*mdl.Team, error) {
teamID, err := c.TeamId()
if err != nil {
return nil, err
}
var t *mdl.Team
t, err = mdl.TeamByID(c.c, teamID)
if err != nil {
log.Errorf(c.c, "%s team with id:%v was not found %v", c.desc, teamID, err)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeTeamNotFound)}
}
return t, nil
}
// RequestID returns a int64 requestId from the HTTP request.
//
func (c Context) RequestID() (int64, error) {
strRequestID, err := route.Context.Get(c.r, "requestId")
if err != nil {
log.Errorf(c.c, "%s error getting request id, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTeamRequestNotFound)}
}
var requestID int64
requestID, err = strconv.ParseInt(strRequestID, 0, 64)
if err != nil {
log.Errorf(c.c, "%s error converting request id from string to int64, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTeamRequestNotFound)}
}
return requestID, nil
}
// TeamRequest returns a a request to join a team from an HTTP request
//
func (c Context) TeamRequest() (*mdl.TeamRequest, error) {
requestID, err := c.RequestID()
if err != nil {
return nil, err
}
var teamRequest *mdl.TeamRequest
if teamRequest, err = mdl.TeamRequestByID(c.c, requestID); err != nil {
log.Errorf(c.c, "%s teams.DenyRequest, team request not found: %v", c.desc, err)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeTeamRequestNotFound)}
}
return teamRequest, nil
}
// TournamentId returns the Id of the tournament that the request holds.
//
func (c Context) TournamentId() (int64, error) {
strTournamentID, err := route.Context.Get(c.r, "tournamentId")
if err != nil {
log.Errorf(c.c, "%s error getting tournament id, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTournamentNotFound)}
}
var tournamentId int64
tournamentId, err = strconv.ParseInt(strTournamentID, 0, 64)
if err != nil {
log.Errorf(c.c, "%s error converting tournament id from string to int64, err:%v", c.desc, err)
return 0, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeTournamentNotFound)}
}
return tournamentId, nil
}
// Tournament returns a tournament instance.
// It gets the 'tournamentId' from the request and queries the datastore to get
// the tournament.
//
func (c Context) Tournament() (*mdl.Tournament, error) {
tournamentId, err := c.TournamentId()
if err != nil {
return nil, err
}
var tournament *mdl.Tournament
if tournament, err = mdl.TournamentByID(c.c, tournamentId); err != nil {
log.Errorf(c.c, "%s tournament not found: %v", c.desc, err)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeTournamentNotFound)}
}
return tournament, nil
}
// Match returns a match instance.
//
func (c Context) Match(tournament *mdl.Tournament) (*mdl.Tmatch, error) {
strmatchIDNumber, err := route.Context.Get(c.r, "matchId")
if err != nil {
log.Errorf(c.c, "%s error getting match id, err:%v", c.desc, err)
return nil, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeMatchCannotUpdate)}
}
var matchIDNumber int64
matchIDNumber, err = strconv.ParseInt(strmatchIDNumber, 0, 64)
if err != nil {
log.Errorf(c.c, "%s error converting match id from string to int64, err:%v", c.desc, err)
return nil, &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeMatchCannotUpdate)}
}
match := mdl.GetMatchByIDNumber(c.c, *tournament, matchIDNumber)
if match == nil {
log.Errorf(c.c, "%s unable to get match with id number :%v", c.desc, matchIDNumber)
return nil, &helpers.NotFound{Err: errors.New(helpers.ErrorCodeMatchNotFoundCannotUpdate)}
}
return match, nil
}
// Count extracts the 'count' value from the given http.Request
// returns 20 if none is found.
//
func (c Context) Count() int64 {
defaultValue := int64(20) // set count to default value
if len(c.r.FormValue("count")) == 0 {
return defaultValue
}
count, err := strconv.ParseInt(c.r.FormValue("count"), 0, 64)
if err != nil {
log.Errorf(c.c, "%s: error during conversion of count parameter: %v", c.desc, err)
count = defaultValue
}
return count
}
// CountOrDefault extracts the 'count' value from the given http.Request
// returns 20 if none is found.
//
func (c Context) CountOrDefault(d int64) int64 {
if len(c.r.FormValue("count")) == 0 {
return d
}
count, err := strconv.ParseInt(c.r.FormValue("count"), 0, 64)
if err != nil {
log.Errorf(c.c, "%s: error during conversion of count parameter: %v", c.desc, err)
count = d // set count to default value
}
return count
}
// Page extracts the 'page' value from the given http.Request
// returns 1 if none is found.
//
func (c Context) Page() int64 {
if len(c.r.FormValue("page")) == 0 {
return int64(1)
}
page, err := strconv.ParseInt(c.r.FormValue("page"), 0, 64)
if err != nil {
log.Errorf(c.c, "%s error during conversion of page parameter: %v", c.desc, err)
page = 1
}
return page
}