-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstation_handlers.go
245 lines (195 loc) · 5.74 KB
/
station_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
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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type StationHandlers struct {
DB *gorm.DB
Config *Config
}
// CreateStation creates a new station.
func (sh *StationHandlers) CreateStation(c *gin.Context) {
name := c.PostForm("name")
apiResponse := ApiResponse{}
if name == "" {
apiResponse.Success = false
apiResponse.Error = "Invalid inputs"
c.JSON(http.StatusOK, apiResponse)
return
}
station := &Station{Name: name}
sh.DB.Create(station).Commit()
apiResponse.Success = true
if station.ID > 0 {
apiResponse.Data = StationJSON{
ID: station.ID,
Name: station.Name,
Products: []ProductJSON{},
CreatedAt: station.CreatedAt,
UpdatedAt: station.UpdatedAt,
}
}
c.JSON(http.StatusOK, apiResponse)
}
// AddProductToStation adds a product to a station.
func (sh *StationHandlers) AddProductToStation(c *gin.Context) {
apiResponse := ApiResponse{}
stationId, err := getIntFromParams("stationId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
productId, err := getIntFromParams("productId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
station := &Station{}
product := &Product{}
// We need to do some validation to make sure that both the station and
// the product exist, so we don't add garbage to the database.
sh.DB.First(station, "id = ?", stationId)
sh.DB.First(product, "id = ?", productId)
// Error out if either the station or the product didn't load.
if station.ID == 0 || product.ID == 0 {
apiResponse.Success = false
apiResponse.Error = "Invalid station or product ID"
c.JSON(http.StatusOK, apiResponse)
return
}
// Finally, create the station product.
sh.DB.Create(&StationProduct{
StationID: uint64(stationId),
ProductID: uint64(productId),
}).Commit()
apiResponse.Success = true
c.JSON(http.StatusOK, apiResponse)
}
// RemoveProductFromStation removes a product from a station.
func (sh *StationHandlers) RemoveProductFromStation(c *gin.Context) {
apiResponse := ApiResponse{}
stationId, err := getIntFromParams("stationId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
productId, err := getIntFromParams("productId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
result := sh.DB.Exec(`
delete from station_products
where station_id = ? and product_id = ?
`, stationId, productId)
// Error out if either the station or the product didn't load.
if result.RowsAffected == 0 {
apiResponse.Success = false
apiResponse.Error = "No such station product"
c.JSON(http.StatusOK, apiResponse)
return
}
apiResponse.Success = true
c.JSON(http.StatusOK, apiResponse)
}
func (sh *StationHandlers) constructProductsArray(station *Station) []ProductJSON {
products := []ProductJSON{}
for _, product := range station.StationProducts {
productJSON := ProductJSON{
ID: product.Product.ID,
Name: product.Product.Name,
Type: product.Product.ProductType.Name,
Price: product.Product.Price,
Discontinued: product.Product.Discontinued == 1,
SoldOut: product.Product.SoldOut == 1,
ProductType: ProductTypeJSON{
ID: product.Product.ProductType.ID,
Name: product.Product.ProductType.Name,
Title: product.Product.ProductType.Title,
},
}
products = append(products, productJSON)
}
return products
}
// Station returns the station by its id.
func (sh *StationHandlers) Station(c *gin.Context) {
apiResponse := ApiResponse{}
stationId, err := getIntFromParams("stationId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
station := &Station{}
sh.DB.
Preload("StationProducts.Product").
Preload("StationProducts.Product.ProductType").
First(station, "id = ?", stationId)
apiResponse.Success = true
if station.ID > 0 {
products := sh.constructProductsArray(station)
apiResponse.Data = StationJSON{
ID: station.ID,
Name: station.Name,
Products: products,
CreatedAt: station.CreatedAt,
UpdatedAt: station.UpdatedAt,
}
}
c.JSON(http.StatusOK, apiResponse)
}
// Stations returns a list of all stations.
func (sh *StationHandlers) Stations(c *gin.Context) {
apiResponse := ApiResponse{}
var stations []Station = []Station{}
var stationsJson []StationJSON = []StationJSON{}
sh.DB.
Preload("StationProducts.Product").
Preload("StationProducts.Product.ProductType").
Order("id desc").
Find(&stations)
for _, station := range stations {
products := sh.constructProductsArray(&station)
newStation := StationJSON{
ID: station.ID,
Name: station.Name,
Products: products,
CreatedAt: station.CreatedAt,
UpdatedAt: station.UpdatedAt,
}
stationsJson = append(stationsJson, newStation)
}
apiResponse.Success = true
apiResponse.Data = stationsJson
c.JSON(http.StatusOK, apiResponse)
}
// Delete removes a station by its id.
func (sh *StationHandlers) Delete(c *gin.Context) {
apiResponse := ApiResponse{}
stationId, err := getIntFromParams("stationId", c)
if err != nil {
apiResponse.Success = false
apiResponse.Error = err.Error()
c.JSON(http.StatusOK, apiResponse)
return
}
sh.DB.Exec("delete from station_products where station_id = ?", stationId)
result := sh.DB.Exec("delete from stations where id = ?", stationId)
apiResponse.Success = false
apiResponse.Error = result.Commit().Error.Error()
if result.RowsAffected > 0 {
apiResponse.Success = true
}
c.JSON(http.StatusOK, apiResponse)
}