/
price.go
167 lines (134 loc) · 4.67 KB
/
price.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
/*
* Copyright (c) 2014 Santiago Arias | Remy Jourde
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package teams
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"appengine"
"github.com/taironas/gonawin/extract"
"github.com/taironas/gonawin/helpers"
"github.com/taironas/gonawin/helpers/log"
templateshlp "github.com/taironas/gonawin/helpers/templates"
mdl "github.com/taironas/gonawin/models"
)
// Prices handler, use it to get the team's prices.
// GET /j/teams/[0-9]+/prices/ Get the prices of a team team with the given team id.
// Response: array of JSON formatted prices.
//
func Prices(w http.ResponseWriter, r *http.Request, u *mdl.User) error {
if r.Method != "GET" {
return &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeNotSupported)}
}
c := appengine.NewContext(r)
desc := "Team Prices Handler:"
extract := extract.NewContext(c, desc, r)
var team *mdl.Team
var err error
team, err = extract.Team()
if err != nil {
return err
}
prices := team.Prices(c)
pvm := buildTeamPricesViewModel(prices)
return templateshlp.RenderJSON(w, c, pvm)
}
type teamPricesViewModel struct {
Prices []*mdl.Price
}
func buildTeamPricesViewModel(prices []*mdl.Price) teamPricesViewModel {
return teamPricesViewModel{Prices: prices}
}
// PriceByTournament handler, use it to get the price of a team for a specific tournament.
// GET /j/teams/[0-9]+/prices/[0-9]+/ Retrieves price of a team with the given id for the specified tournament.
// Response: JSON formatted price.
//
func PriceByTournament(w http.ResponseWriter, r *http.Request, u *mdl.User) error {
if r.Method == "GET" {
return &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeNotSupported)}
}
c := appengine.NewContext(r)
desc := "Team Price by tournament Handler:"
extract := extract.NewContext(c, desc, r)
var t *mdl.Team
var err error
t, err = extract.Team()
if err != nil {
return err
}
var tournamentId int64
tournamentId, err = extract.TournamentId()
if err != nil {
return err
}
p := t.PriceByTournament(c, tournamentId)
pvm := buildTeamPriceViewModel(p)
return templateshlp.RenderJSON(w, c, pvm)
}
// UpdatePrice handler, use it to update the price of a team for a specific tournament.
// POST /j/teams/[0-9]+/prices/update/[0-9]+/ Updates price of a team with the given id for the specified tournament.
// Response: JSON formatted price.
//
func UpdatePrice(w http.ResponseWriter, r *http.Request, u *mdl.User) error {
if r.Method != "POST" {
return &helpers.BadRequest{Err: errors.New(helpers.ErrorCodeNotSupported)}
}
c := appengine.NewContext(r)
desc := "Team update price Handler:"
extract := extract.NewContext(c, desc, r)
var t *mdl.Team
var err error
t, err = extract.Team()
if err != nil {
return err
}
var tournamentId int64
tournamentId, err = extract.TournamentId()
if err != nil {
return err
}
p := t.PriceByTournament(c, tournamentId)
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf(c, "%s Error when reading request body err: %v", desc, err)
return &helpers.InternalServerError{Err: errors.New(helpers.ErrorCodeTeamCannotUpdate)}
}
var priceData PriceData
err = json.Unmarshal(body, &priceData)
if err != nil {
log.Errorf(c, "%s Error when decoding request body err: %v", desc, err)
return &helpers.InternalServerError{Err: errors.New(helpers.ErrorCodeTeamCannotUpdate)}
}
if helpers.IsStringValid(priceData.Description) && (p.Description != priceData.Description) {
// update data
p.Description = priceData.Description
p.Update(c)
} else {
log.Errorf(c, "%s Cannot update because updated is not valid.", desc)
log.Errorf(c, "%s Update description = %s", desc, priceData.Description)
return &helpers.InternalServerError{Err: errors.New(helpers.ErrorCodeTeamCannotUpdate)}
}
pvm := buildTeamPriceViewModel(p)
return templateshlp.RenderJSON(w, c, pvm)
}
type teamPriceViewModel struct {
Price *mdl.Price
}
func buildTeamPriceViewModel(price *mdl.Price) teamPriceViewModel {
return teamPriceViewModel{Price: price}
}