-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathItemsController.dbl
411 lines (375 loc) · 17.8 KB
/
ItemsController.dbl
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
;;*****************************************************************************
;;
;; Title: ItemsController.dbl
;;
;; Description: OData controller for the ITEMS structure.
;;
;;*****************************************************************************
;; WARNING: GENERATED CODE!
;; This file was generated by CodeGen. Avoid editing the file if possible.
;; Any changes you make will be lost of the file is re-generated.
;;*****************************************************************************
import Microsoft.AspNetCore.Http
import Microsoft.OData
import Microsoft.AspNetCore.JsonPatch
import Microsoft.AspNetCore.Mvc
import Microsoft.AspNetCore.OData.Routing.Controllers
import Microsoft.AspNetCore.OData.Routing.Attributes
import Microsoft.AspNetCore.OData.Query
import Microsoft.AspNetCore.OData.Results
import Microsoft.AspNetCore.OData.Formatter
import Microsoft.EntityFrameworkCore
import Microsoft.EntityFrameworkCore.Infrastructure
import Microsoft.Extensions.Options
import System.Collections.Generic
import System.ComponentModel.DataAnnotations
import System.Net.Mime
import Harmony.Core.EF.Extensions
import Harmony.Core.Interface
import Harmony.OData
import Harmony.AspNetCore
import Newtonsoft.Json
import Services.Models
namespace Services.Controllers
{Produces("application/json")}
;;; <summary>
;;; Item master record
;;; </summary>
;;; <remarks>
;;; OData endpoints for Item master record
;;; </remarks>
public partial class ItemsController extends ODataController
;;Services provided via dependency injection
private _DbContext, @Services.Models.DBContext
private _ServiceProvider, @IServiceProvider
private _AppSettings, @IOptions<AppSettings>
;;; <summary>
;;; Constructs a new instance of ItemsController
;;; </summary>
;;; <param name="aDbContext">Database context instance (DI)</param>
;;; <param name="aServiceProvider">Service provider instance (DI)</param>
;;; <param name="aAppSettings">Application settings</param>
public method ItemsController
aDbContext, @Services.Models.DBContext
aServiceProvider, @IServiceProvider
aAppSettings, @IOptions<AppSettings>
proc
this._DbContext = aDbContext
this._ServiceProvider = aServiceProvider
this._AppSettings = aAppSettings
endmethod
{HttpGet("Items")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Item>),StatusCodes.Status200OK)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query the entire collection of records
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItems, @IActionResult
proc
mreturn Ok(_DbContext.Items.AsNoTracking())
endmethod
{HttpGet("Items(ItemNumber={aItemNumber})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(Item),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query a single record identified by unique primary key
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aItemNumber" example="19">Item number</param>
;;; <returns>Returns a SingleResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItemByPK, @SingleResult<Item>
required in aItemNumber, int
proc
mreturn new SingleResult<Item>(_DbContext.Items.AsNoTracking().FindQuery<Item>(_DbContext, aItemNumber))
endmethod
{HttpGet("Items(VendorNumber={aVendorNumber})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Item>),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query a subset of records identified by non-unique alternate key VendorNumber
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aVendorNumber" example="41">Vendor number</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItemsByVendorNumber, @IActionResult
required in aVendorNumber, int
proc
data result = _DbContext.Items.AsNoTracking().FindAlternate("VendorNumber",aVendorNumber)
if (result == ^null)
mreturn NotFound()
mreturn Ok(result)
endmethod
{HttpGet("Items(FlowerColor={aFlowerColor})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Item>),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query a subset of records identified by non-unique alternate key Color
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aFlowerColor" example="Red">Flower color</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItemsByColor, @IActionResult
required in aFlowerColor, String
proc
data result = _DbContext.Items.AsNoTracking().FindAlternate("FlowerColor",aFlowerColor)
if (result == ^null)
mreturn NotFound()
mreturn Ok(result)
endmethod
{HttpGet("Items(Size={aSize})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Item>),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query a subset of records identified by non-unique alternate key Size
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aSize" example="5">Size in gallons</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItemsBySize, @IActionResult
required in aSize, int
proc
data result = _DbContext.Items.AsNoTracking().FindAlternate("Size",aSize)
if (result == ^null)
mreturn NotFound()
mreturn Ok(result)
endmethod
{HttpGet("Items(CommonName={aCommonName})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Item>),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Query a subset of records identified by non-unique alternate key Name
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aCommonName" example="European Hackberry">Common name</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method GetItemsByName, @IActionResult
required in aCommonName, String
proc
data result = _DbContext.Items.AsNoTracking().FindAlternate("CommonName",aCommonName)
if (result == ^null)
mreturn NotFound()
mreturn Ok(result)
endmethod
{Consumes(MediaTypeNames.Application.Json)}
{Produces("application/json")}
{ProducesResponseType(^typeof(Item),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status400BadRequest)}
{HttpPost("Items")}
;;; <summary>
;;; Create a new record (automatically assigned primary key)
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="200">OK. The operation completed successfully.</response>
;;; <response code="400">Bad request. The request was malformed or contained inappropriate content.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method PostItem, @IActionResult
{FromBody}
required in aItem, @Item
proc
;;Remove the primary key fields from ModelState
ModelState.Remove("ItemNumber")
;; Validate inbound data
if (!ModelState.IsValid)
mreturn ValidationHelper.ReturnValidationError(ModelState)
;;Get the next available primary key value
disposable data keyFactory = (@IPrimaryKeyFactory)_ServiceProvider.GetService(^typeof(IPrimaryKeyFactory))
KeyFactory.AssignPrimaryKey(aItem)
;;Add the new item
try
begin
_DbContext.Items.Add(aItem)
_DbContext.SaveChanges(keyFactory)
end
catch (e, @ValidationException)
begin
ModelState.AddModelError("RelationValidation",e.Message)
mreturn ValidationHelper.ReturnValidationError(ModelState)
end
endtry
mreturn Created(aItem)
endmethod
{HttpPut("Items(ItemNumber={aItemNumber})")}
{Consumes(MediaTypeNames.Application.Json)}
{Produces("application/json")}
{ProducesResponseType(StatusCodes.Status201Created)}
{ProducesResponseType(StatusCodes.Status400BadRequest)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
;;; <summary>
;;; Update a record if it exists otherwise create a new record (primary key provided by client)
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aItemNumber" example="19">Item number</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="201">Created. The entity was created but no data is being returned.</response>
;;; <response code="400">Bad request. The request was malformed or contained inappropriate content.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method PutItem, @IActionResult
required in aItemNumber, int
{FromBody}
required in aItem, @Item
proc
;; Validate inbound data
if (!ModelState.IsValid)
mreturn ValidationHelper.ReturnValidationError(ModelState)
;;Ensure that the key values in the URI win over any data that may be in the model object
aItem.ItemNumber = aItemNumber
try
begin
;;Add and commit
data existing = _DbContext.Items.Find(aItemNumber)
if(existing == ^null) then
begin
_DbContext.Items.Add(aItem)
_DbContext.SaveChanges()
mreturn Created(aItem)
end
else
begin
aItem.CopyTo(existing)
_DbContext.SaveChanges()
mreturn NoContent()
end
end
catch (e, @InvalidOperationException)
begin
mreturn BadRequest(e)
end
catch (e, @ValidationException)
begin
ModelState.AddModelError("RelationValidation",e.Message)
mreturn ValidationHelper.ReturnValidationError(ModelState)
end
endtry
endmethod
{HttpPatch("Items(ItemNumber={aItemNumber})")}
{Consumes(MediaTypeNames.Application.Json)}
{Produces("application/json")}
{ProducesResponseType(StatusCodes.Status204NoContent)}
{ProducesResponseType(StatusCodes.Status400BadRequest)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
;;; <summary>
;;; Patch (partial update) an existing record
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aItemNumber" example="19">Item number</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="204">No content. The operation was successful but no data is being returned.</response>
;;; <response code="400">Bad request. The request was malformed or contained inappropriate content.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method PatchItem, @IActionResult
required in aItemNumber, int
{FromBody}
required in aItem, @JsonPatchDocument<Item>
proc
;; Validate inbound data
if (!ModelState.IsValid)
mreturn ValidationHelper.ReturnValidationError(ModelState)
;;Patch the existing item
try
begin
;;Get the item to be updated
data itemToUpdate = _DbContext.Items.Find(aItemNumber)
data patchError, @JsonPatchError, ^null
;;Did we find it?
if(itemToUpdate == ^null)
mreturn NotFound()
;;Apply the changes to the item we read
aItem.ApplyTo(itemToUpdate, lambda(error) { patchError = error })
;;if the patchdoc was bad return the error info
if(patchError != ^null)
mreturn BadRequest(string.Format("Error applying patch document: error message {0}, caused by {1}", patchError.ErrorMessage, JsonConvert.SerializeObject(patchError.Operation)))
;;Update and commit
_DbContext.Items.Update(itemToUpdate)
_DbContext.SaveChanges()
end
catch (e, @InvalidOperationException)
begin
mreturn BadRequest(e)
end
catch (e, @ValidationException)
begin
ModelState.AddModelError("RelationValidation",e.Message)
mreturn ValidationHelper.ReturnValidationError(ModelState)
end
endtry
mreturn NoContent()
endmethod
{HttpDelete("Items(ItemNumber={aItemNumber})")}
{ProducesResponseType(StatusCodes.Status204NoContent)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
;;; <summary>
;;; Delete a record
;;; </summary>
;;; <remarks>
;;;
;;; </remarks>
;;; <param name="aItemNumber" example="19">Item number</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
;;; <response code="204">No content. The operation was successful but no data is being returned.</response>
;;; <response code="404">Not found. The entity being addressed does not exist.</response>
;;; <response code="500">Internal server error. Report the issue to the API maintainer.</response>
public method DeleteItem, @IActionResult
required in aItemNumber, int
proc
;;Get the item to be deleted
data itemToRemove = _DbContext.Items.Find(aItemNumber)
;;Did we find it?
if (itemToRemove == ^null)
mreturn NotFound()
;;Delete and commit
_DbContext.Items.Remove(itemToRemove)
_DbContext.SaveChanges()
mreturn NoContent()
endmethod
endclass
endnamespace