-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_vendor.go
291 lines (237 loc) · 7.42 KB
/
rest_vendor.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"github.com/labstack/echo"
"log"
"net/http"
)
///////////////////////////////////////////////////////////////////////
// Generated By codegen from SQL table vendor
// Vendor REST Functions
type DBvendor struct {
ID int `db:"id"`
Name string `db:"name"`
Descr string `db:"descr"`
Address string `db:"address"`
Phone string `db:"phone"`
Fax string `db:"fax"`
ContactName string `db:"contact_name"`
ContactEmail string `db:"contact_email"`
OrdersEmail string `db:"orders_email"`
Rating string `db:"rating"`
Notes string `db:"notes"`
}
type DBpartVendor struct {
PartId int `db:"part_id"`
VendorId int `db:"vendor_id"`
VendorCode string `db:"vendor_code"`
LatestPrice float64 `db:"latest_price"`
}
type DBvendorPrice struct {
PartId int `db:"part_id"`
VendorId int `db:"vendor_id"`
Datefrom string `db:"datefrom"`
Price float64 `db:"price"`
MinQty float64 `db:"min_qty"`
Notes string `db:"notes"`
}
// Get a list of vendors
func queryVendor(c *echo.Context) error {
_, err := securityCheck(c, "readVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
var records []*DBvendor
err = DB.
Select("id", "name", "descr", "address", "phone", "fax", "contact_name", "contact_email", "orders_email", "rating", "notes").
From("vendor").
QueryStructs(&records)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, records)
}
type DBvendorParts struct {
PartId int `db:"part_id"`
Name string `db:"name"`
Descr string `db:"descr"`
StockCode string `db:"stock_code"`
QtyType string `db:"qty_type"`
VendorCode string `db:"vendor_code"`
LatestPrice float64 `db:"latest_price"`
}
// Get a list of parts provided by this vendor
func queryVendorParts(c *echo.Context) error {
_, err := securityCheck(c, "readVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
var records []*DBvendorParts
err = DB.SQL(`
select
p.id as part_id, p.name as name, p.descr as descr, p.stock_code as stock_code,
x.latest_price as latest_price, p.qty_type as qty_type,
x.vendor_code as vendor_code
from part_vendor x
left join part p on (p.id=x.part_id)
where x.vendor_id=$1`,
id).
QueryStructs(&records)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, records)
}
// Get a specific vendor
func getVendor(c *echo.Context) error {
_, err := securityCheck(c, "readVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
var record DBvendor
err = DB.
Select("id", "name", "descr", "address", "phone", "fax", "contact_name", "contact_email", "orders_email", "rating", "notes").
From("vendor").
Where("id = $1", id).
QueryStruct(&record)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, record)
}
type NewItemPrice struct {
PartID int
NewPrice float64
MinQty float64
VendorCode string
}
type NewPriceList struct {
VendorID int
NewPriceArray []NewItemPrice
}
// Receive a new PriceList from the front end, and dynamically
// Create the XRef records from vendor to part
func newVendorPrices(c *echo.Context) error {
claim, err := securityCheck(c, "writeVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
log.Println("State param of", id)
// Bind the input params to our structure
record := &NewPriceList{}
if err := c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// place
partVendor := &DBpartVendor{}
vendorPrice := &DBvendorPrice{}
for k, v := range record.NewPriceArray {
log.Println("Item", k, "Part ID:", v.PartID, "Price:", v.NewPrice, "Qty:", v.MinQty, "Code:", v.VendorCode)
// Update or Insert part_vendor
partVendor.PartId = v.PartID
partVendor.VendorId = id
partVendor.VendorCode = v.VendorCode
partVendor.LatestPrice = v.NewPrice
_, err := DB.
Upsert("part_vendor").
Columns("part_id", "vendor_id", "vendor_code", "latest_price").
Record(partVendor).
Where("part_id=$1 and vendor_id=$2", v.PartID, id).
Exec()
if err != nil {
log.Println("PartVendor:", err.Error())
}
// Create a new vendor_price record
vendorPrice.VendorId = id
vendorPrice.PartId = v.PartID
vendorPrice.Price = v.NewPrice
vendorPrice.MinQty = v.MinQty
_, err = DB.
InsertInto("vendor_price").
Whitelist("vendor_id", "part_id", "price", "min_qty").
Record(vendorPrice).
Exec()
if err != nil {
log.Println("VendorPrice:", err.Error())
}
// Update the part to have a new 'latest price'
_, err = DB.SQL(`update part set latest_price=$1 where id=$2`, v.NewPrice, v.PartID).Exec()
if err != nil {
log.Println("Part:", err.Error())
}
// Now log the creation of the new price
sysLog(1, "Vendor", "p", id, fmt.Sprintf("New Price $%0.2f for part %d", v.NewPrice, v.PartID), c, claim)
}
return c.String(http.StatusCreated, "Added Prices")
}
// Create a new vendor
func newVendor(c *echo.Context) error {
claim, err := securityCheck(c, "writeVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
record := &DBvendor{}
if err := c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
err = DB.InsertInto("vendor").
Whitelist("name", "descr", "address", "phone", "fax", "contact_name", "contact_email", "orders_email", "rating", "notes").
Record(record).
Returning("id").
QueryScalar(&record.ID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Now log the creation of the new vendor
sysLog(1, "Vendor", "V", record.ID, "Vendor Created", c, claim)
// insert into DB, fill in the ID of the new vendor
return c.JSON(http.StatusCreated, record)
}
// Update an existing vendor
func saveVendor(c *echo.Context) error {
claim, err := securityCheck(c, "writeVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
preRecord := &DBvendor{}
DB.Select("id", "name", "descr", "address", "phone", "fax", "contact_name", "contact_email", "orders_email", "rating", "notes").
From("vendor").
Where("id=$1", id).
QueryStruct(preRecord)
record := &DBvendor{}
if err = c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
_, err = DB.Update("vendor").
SetWhitelist(record, "name", "descr", "address", "phone", "fax", "contact_name", "contact_email", "orders_email", "rating", "notes").
Where("id = $1", id).
Exec()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
sysLogUpdate(1, "Vendor", "V", id, "Updated", c, claim, *preRecord, *record)
return c.JSON(http.StatusOK, id)
}
// Delete an existing vendor
func deleteVendor(c *echo.Context) error {
claim, err := securityCheck(c, "writeVendor")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
_, err = DB.
DeleteFrom("vendor").
Where("id = $1", id).
Exec()
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
DB.SQL(`delete from part_vendor where vendor_id=$1`, id)
DB.SQL(`delete from vendor_price where vendor_id=$1`, id)
sysLog(3, "Vendor", "V", id, "Vendor Deleted", c, claim)
return c.String(http.StatusOK, "Vendor Deleted")
}