1
+ /// REST services for managing the coffee business
1
2
Class ICO .Handler Extends %CSP .REST
2
3
{
3
-
4
+ /// Honor the CORS header <a href="https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=GREST_CORS">as described here</a>
4
5
Parameter HandleCorsRequest = 1 ;
5
-
6
+ /// Default response content type
6
7
Parameter CONTENTTYPE = " application/json" ;
7
-
8
+ /// Number of days since roasting for the coffee to be considered "stale"
8
9
Parameter MAXAGE = 5 ;
9
10
11
+ /// Removes bags of coffee from the catalog
12
+ /// Input: <ul><li>id: catalog ID in catalog</li><li>quantity: number of bags to remove</li></ul>
13
+ /// Output: New product quantity on-hand, or an error
10
14
ClassMethod SellProduct (id As %String , quantity As %Numeric ) As %Status
11
15
{
12
16
try {
17
+ // does the ID exist?
13
18
if (1 '= ##class (ICO.catalog ).%ExistsId (id ))
14
19
{
15
20
set err = {}
@@ -18,6 +23,7 @@ ClassMethod SellProduct(id As %String, quantity As %Numeric) As %Status
18
23
}
19
24
else
20
25
{
26
+ // do we have enough quantity?
21
27
set item = ##class (ICO.catalog ).%OpenId (id )
22
28
if (quantity > item .quantity )
23
29
{
@@ -27,33 +33,43 @@ ClassMethod SellProduct(id As %String, quantity As %Numeric) As %Status
27
33
}
28
34
else
29
35
{
36
+ // decrement the database and return the new quantity
30
37
set item .quantity = (item .quantity - quantity )
31
38
do item .%JSONExportToString (.outstring )
32
39
write outstring
33
40
set sc = item .%Save ()
34
41
}
35
42
}
36
43
} catch (oException ) {
44
+ // return the error
37
45
set expobj = {}
38
46
set expobj ." exception" = oException .%AsSystemError ()
39
47
write expobj .%ToJSON ()
40
48
}
41
49
Quit $$$OK
42
50
}
43
51
52
+ /// Gets bagged coffee inventory for sale from the catalog
53
+ /// Input: fresh: if <pre>1</pre>, return stale bags (roasted more than <PARAMETER>MAXAGE</PARAMETER> days ago).
54
+ /// Output: JSON listing of products
44
55
ClassMethod GetProducts (fresh As %Boolean = 1 ) As %Status
45
56
{
46
57
try {
58
+ // fields to return
47
59
set sqlquery = " SELECT catalog_id, product_code, quantity, price, time_roasted, roasting_notes, img FROM ICO.catalog"
60
+ // set the WHERE clause based on whether we want fresh or not (the -? will be replaced by MAXAGE)
48
61
if fresh = 1 {
49
62
set sqlquery = sqlquery _" WHERE time_roasted > DATEADD('day',-?,CURRENT_DATE)"
50
63
} else {
51
64
set sqlquery = sqlquery _" WHERE time_roasted <= DATEADD('day',-?,CURRENT_DATE)"
52
65
}
66
+ // if nothing is left, exclude from the response
53
67
set sqlquery = sqlquery _" AND quantity > 0"
68
+ // run the query
54
69
set rs = ##class (%SQL.Statement ).%ExecDirect (,sqlquery , ..#MAXAGE)
55
70
set itemsarray = []
56
71
72
+ // iterate over the results and build a dynamic array
57
73
while rs .%Next ()
58
74
{
59
75
do itemsarray .%Push (
@@ -68,6 +84,7 @@ ClassMethod GetProducts(fresh As %Boolean = 1) As %Status
68
84
}
69
85
)
70
86
}
87
+ // translate the dynamic arry into JSON and return to client
71
88
set itemsobj = {}
72
89
set itemsobj ." rowcount" = rs .%ROWCOUNT
73
90
set itemsobj ." products" = itemsarray
@@ -80,6 +97,7 @@ ClassMethod GetProducts(fresh As %Boolean = 1) As %Status
80
97
Quit $$$OK
81
98
}
82
99
100
+ /// Create JSON from POST content
83
101
ClassMethod GetJSONFromRequest (Output obj As %DynamicObject ) As %Boolean
84
102
{
85
103
set ok = 1
@@ -91,15 +109,20 @@ ClassMethod GetJSONFromRequest(Output obj As %DynamicObject) As %Boolean
91
109
Quit ok
92
110
}
93
111
112
+ /// Accepts JSON in an HTTP POST and persists it into the bagged coffee catalog
113
+ /// Input: None explicit. Will get HTTP POST content when calling <METHOD>GetJSONFromRequest</METHOD>
114
+ /// Output: on success, JSON having a key/value of "success": 1, and the object saved, or an error.
94
115
ClassMethod CatalogProduct () As %Status
95
116
{
117
+ // get HTTP POST content
96
118
if '..GetJSONFromRequest (.obj ) {
97
119
set %response .Status = ..#HTTP400BADREQUEST
98
120
set error = {" error" : " No JSON body in request" }
99
121
write error .%ToJSON ()
100
122
Quit $$$OK
101
123
}
102
124
125
+ /// construct a new ICO.catalog object and populate it from the input
103
126
try {
104
127
set catobj = ##class (ICO.catalog ).%New ()
105
128
set catobj .productcode = obj ." product_code"
@@ -121,9 +144,13 @@ ClassMethod CatalogProduct() As %Status
121
144
Quit $$$OK
122
145
}
123
146
147
+ /// Takes raw coffee beans out of inventory so they can be virtually roasted
148
+ /// Input: <ul><li>id: vendor id in catalog</li><li>quantity: amount in kilograms</li></ul>
149
+ /// Output: on success, JSON of the item and quantity withdrawn, or an error.
124
150
ClassMethod GetRawBeans (id As %String , quantity As %Numeric ) As %Status
125
151
{
126
152
try {
153
+ // does the vendor ID exist?
127
154
if (1 '= ##class (ICO.inventory ).%ExistsId (id ))
128
155
{
129
156
set err = {}
@@ -132,6 +159,7 @@ ClassMethod GetRawBeans(id As %String, quantity As %Numeric) As %Status
132
159
}
133
160
else
134
161
{
162
+ // do we have enough quantity?
135
163
set item = ##class (ICO.inventory ).%OpenId (id )
136
164
if (quantity > item .quantitykg )
137
165
{
@@ -141,6 +169,7 @@ ClassMethod GetRawBeans(id As %String, quantity As %Numeric) As %Status
141
169
}
142
170
else
143
171
{
172
+ // decrement the quantity and return the requested inventory
144
173
set item .quantitykg = (item .quantitykg - quantity )
145
174
set sc = item .%Save ()
146
175
do item .%JSONExportToString (.outstring )
@@ -155,6 +184,9 @@ ClassMethod GetRawBeans(id As %String, quantity As %Numeric) As %Status
155
184
Quit $$$OK
156
185
}
157
186
187
+ /// Return raw coffee bean inventory
188
+ /// Input: None
189
+ /// Output: on success, JSON listing all products on hand, or an error.
158
190
ClassMethod ListRawBeans () As %Status
159
191
{
160
192
try {
0 commit comments