-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_part.go
246 lines (198 loc) · 6.23 KB
/
rest_part.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
package main
import (
"github.com/labstack/echo"
// "gopkg.in/mgutz/dat.v1"
"net/http"
)
///////////////////////////////////////////////////////////////////////
// Parts Maintenance
/*
create table part (
id serial not null primary key,
name text not null,
descr text not null,
stock_code text not null,
reorder_stocklevel numeric(12,2) not null,
reorder_qty numeric(12,2) not null,
latest_price numeric(12,2) not null,
qty_type text not null,
picture text not null
);
*/
type DBpart struct {
ID int `db:"id"`
Name string `db:"name"`
Descr string `db:"descr"`
StockCode string `db:"stock_code"`
ReorderStocklevel float64 `db:"reorder_stocklevel"`
ReorderQty float64 `db:"reorder_qty"`
LatestPrice float64 `db:"latest_price"`
QtyType string `db:"qty_type"`
Picture string `db:"picture"`
Notes string `db:"notes"`
}
type DBpartComponents struct {
ComponentID int `db:"component_id"`
PartID int `db:"part_id"`
Qty int `db:"qty"`
StockCode *string `db:"stock_code"` // component stock code and name
Name *string `db:"name"`
MachineName string `db:"machine_name"`
SiteName string `db:"site_name"`
MachineID int `db:"machine_id"`
SiteID int `db:"site_id"`
}
type DBpartVendors struct {
VendorId int `db:"vendor_id"`
Name string `db:"name"`
Descr string `db:"descr"`
Address string `db:"address"`
VendorCode string `db:"vendor_code"`
LatestPrice float64 `db:"latest_price"`
}
// Get a list of all parts
func queryParts(c *echo.Context) error {
_, err := securityCheck(c, "readPart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
var record []*DBpart
err = DB.SQL(`select * from part order by lower(stock_code)`).QueryStructs(&record)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, record)
}
// Get a list of components / tools that use this part
func queryPartComponents(c *echo.Context) error {
claim, err := securityCheck(c, "readPart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
Sites := getClaimedSites(claim)
var cp []*DBpartComponents
partID := getID(c)
err = DB.SQL(`select
x.component_id,x.qty,c.stock_code,c.name,
m.name as machine_name,m.id as machine_id,
s.name as site_name,s.id as site_id
from component_part x
left join component c on (c.id=x.component_id)
left join machine m on (m.id=c.machine_id)
left join site s on (s.id=c.site_id)
where x.part_id=$1
and c.site_id in $2`,
partID, Sites).QueryStructs(&cp)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, cp)
}
// Get a list of vendors that supply this part
func queryPartVendors(c *echo.Context) error {
_, err := securityCheck(c, "readPart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
var records []*DBpartVendors
partID := getID(c)
err = DB.SQL(`
select
v.id as vendor_id, v.name as name, v.descr as descr, v.address as address,
x.latest_price as latest_price,
x.vendor_code as vendor_code
from part_vendor x
left join vendor v on (v.id=x.vendor_id)
where x.part_id=$1`,
partID).
QueryStructs(&records)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, records)
}
// Get a specific part
func getPart(c *echo.Context) error {
_, err := securityCheck(c, "readPart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
var record DBpart
err = DB.SQL(`select * from part where id=$1`, id).QueryStruct(&record)
if err != nil {
return c.String(http.StatusNoContent, err.Error())
}
return c.JSON(http.StatusOK, record)
}
// Create a new part
func newPart(c *echo.Context) error {
claim, err := securityCheck(c, "writePart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
record := &DBpart{}
if err := c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
err = DB.InsertInto("part").
Whitelist("name", "descr", "stock_code", "reorder_stocklevel", "reorder_qty", "latest_price", "qty_type").
Record(record).
Returning("id").
QueryScalar(&record.ID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Now log the creation of the new site
sysLog(1, "Parts", "P", record.ID, "Part Created", c, claim)
// insert into DB, fill in the ID of the new user
return c.JSON(http.StatusCreated, record)
}
// Update an existing part
func savePart(c *echo.Context) error {
claim, err := securityCheck(c, "writePart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
partID := getID(c)
preRecord := &DBpart{}
DB.Select("id", "name", "descr", "stock_code", "reorder_stocklevel", "reorder_qty", "latest_price", "qty_type", "notes").
From("part").
Where("id=$1", partID).
QueryStruct(preRecord)
record := &DBpart{}
if err = c.Bind(record); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
_, err = DB.Update("part").
SetWhitelist(record, "name", "descr", "stock_code", "reorder_stocklevel", "reorder_qty", "latest_price", "qty_type", "notes").
Where("id = $1", partID).
Exec()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
sysLogUpdate(1, "Parts", "P", partID, "Updated", c, claim, *preRecord, *record)
return c.JSON(http.StatusOK, partID)
}
// Delete an existing part
func deletePart(c *echo.Context) error {
claim, err := securityCheck(c, "writePart")
if err != nil {
return c.String(http.StatusUnauthorized, err.Error())
}
id := getID(c)
_, err = DB.
DeleteFrom("part").
Where("id = $1", id).
Exec()
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Clean up vendor parts, comp parts
DB.SQL(`delete from component_part where part_id=$1`, id).Exec()
DB.SQL(`delete from part_vendor where part_id=$1`, id).Exec()
DB.SQL(`delete from vendor_price where part_id=$1`, id).Exec()
DB.SQL(`delete from stock_level where part_id=$1`, id).Exec()
sysLog(3, "Parts", "P", id, "Part Deleted", c, claim)
return c.String(http.StatusOK, "Part Deleted")
}