-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathOrdersMethods.dbl
331 lines (282 loc) · 11.9 KB
/
OrdersMethods.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
import Harmony.OData.Adapter
import System.Threading.Tasks
import System
import System.Collections.Generic
import System.Text
import Services
import Services.Models
import Harmony.Core.Interface
import Harmony.Core.EF.Extensions
import Microsoft.AspNetCore.Mvc
import Microsoft.AspNetCore.Authorization
import Harmony.Core.Context
import Harmony.OData
import Harmony.Core
import System.Linq
import Newtonsoft.Json.Linq
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.AspNetCore.Http
import Microsoft.AspNetCore.Mvc.Infrastructure
import Harmony.AspNetCore.Context
namespace Services.Controllers
;{Authorize}
public class OrdersMethods extends ODataController
private readwrite property mDbContext, @Services.Models.DBContext
private readwrite property mServiceProvider, @IServiceProvider
;;; <summary>
;;; Constructor
;;; </summary>
;;; <param name="aDbContext">DBContext supplied by dependency injection.</param>
;;; <param name="aServicesProvider">Services provider supplied by dependency injection.</param>
public method OrdersMethods
required in aDbContext, @Services.Models.DbContext
required in aServiceProvider, @IServiceProvider
proc
mDbContext = aDbContext
mServiceProvider = aServiceProvider
endmethod
{HttpPut}
public method CreateNewOrder, int
required in aOrder, @Order
;;TODO: something wrong with the registration method when this parameter is of type ICollection it wrecks the EDM model for OrderItem
required in aOrderItems, @List<OrderItem>
proc
;;Validate inbound data (we're not a controller so we can't use ModelState!)
if (aOrderItems.Count<1)
throw new ApplicationException("No items were provided!")
;TODO: Need more data validation
;;Customer ID needs to be valid
;;Item ID's need to be valid
;;And more
;;Allocate the next available order number to the new order
disposable data keyFactory = (@IPrimaryKeyFactory)mServiceProvider.GetService(^typeof(IPrimaryKeyFactory))
keyFactory.AssignPrimaryKey(aOrder)
;;Propagate the new order number to the order items, and polulate line item numbers
data item, @OrderItem
data itemNumber = 0
foreach item in aOrderItems
begin
item.OrderNumber = aOrder.OrderNumber
item.ItemNumber = (itemNumber+=1)
end
;;Save the new order
mDbContext.Orders.Add(aOrder)
mDbContext.OrderItems.AddRange(aOrderItems)
mDbContext.SaveChanges(keyFactory)
;TODO: What happens if something fails?
mreturn aOrder.OrderNumber
endmethod
{HttpGet("OrdersMethods/FindAvailability")}
{CallableMethodConfigurationAttribute(IsFunction=true, ReturnsFromEntitySet=true)}
{EnableQuery()}
{AdapterRoutingFilter()}
public method FindAvailability, @List<Availability>
{AdapterParameterAttribute}
filter, @AvailabiltyFilter
proc
mreturn new List<Availability>() { new Availability() { PointsCost = 9999 } }
endmethod
endclass
public class ExternalCallController extends ODataController
readwrite property CallContext, @ExternalCallContext
public method ExternalCallController
callContext, @ExternalCallContext
proc
this.CallContext = callContext
endmethod
{HttpGet("GetAllCustomers")}
{CallableMethodConfigurationAttribute(IsFunction=true, ReturnsFromEntitySet=true)}
{EnableQuery()}
{EagerContextMiddlewareAttribute(^typeof(ExternalCallContext))}
public method GetAllCustomers, @Task<List<Customer>>
proc
mreturn CallContext.GetAllCustomers()
endmethod
{HttpGet("Arbitrario_MethodWithParameters")}
{CallableMethodConfigurationAttribute(IsFunction=true, ReturnsFromEntitySet=false)}
public method Arbitrario_MethodWithParameters, @Task<ExternalCallContext.ArbitrarioReturnType>
proc
mreturn CallContext.Arbitrario_MethodWithParameters()
endmethod
endclass
public interface IExternalCallContextCallbacks
method Maybe, @Task<[#]@string>
prompt1, @string
prompt2, @string
endmethod
endinterface
public class ExternalCallContext extends DynamicCallProvider
private contextAccessor, @IActionContextAccessor
private callbacks, @IExternalCallContextCallbacks
public method ExternalCallContext
connection, @IDynamicCallConnection
endparams
parent(connection)
proc
data maybeDelegate, @Delegate
maybeDelegate = (@Func<string, string, Task<[#]@string>>)Maybe
AddLocalRpcMethod("CallMe", maybeDelegate)
endmethod
public async method Maybe, @Task<[#]@string>
prompt1, @string
prompt2, @string
proc
if(callbacks != ^null) then
begin
mreturn await callbacks.Maybe(prompt1, prompt2)
end
else
mreturn new string[#] { "no instance provided" }
endmethod
public override method InitServices, void
sp, @IServiceProvider
proc
parent.InitServices(sp)
contextAccessor = (@IActionContextAccessor)sp.GetService(^typeof(IActionContextAccessor))
endmethod
public method InitCallbacks, void
callbacks, @IExternalCallContextCallbacks
proc
this.callbacks = callbacks
endmethod
public override method Recycle, @Task
proc
callbacks = ^null
contextAccessor = ^null
mreturn parent.Recycle()
endmethod
;;; <summary>
;;; Called before each bridge call, before returning to your code
;;; </summary>
;;; <param name="name">name of the method being called</param>
;;; <param name="args">Arguments being passed to the method, which you could change!</param>
;;; <returns></returns>
protected internal override method BeforeCall, [#]@Object
name, @string
args, [#]@Object
proc
;;This example could be used to access current user info
;;and artificially add an additional parameter to the bridge call
;;to pass user or auth info.
data authedArgs = args
Array.Resize(authedArgs, args.Length + 1)
;contextAccessor.ActionContext.HttpContext.User.Identity.Name ???
authedArgs[args.Length + 1] = "claimed!"
mreturn authedArgs
endmethod
; ;;; <summary>
; ;;; Called after each bridge call, before returning to your code
; ;;; </summary>
; ;;; <param name="name">Name of the method that was called</param>
; ;;; <param name="result">Results from the method that yoo could change!</param>
; ;;; <returns></returns>
; protected internal virtual method AfterCall<T>, @Tuple<T, [#]@Object>
; name, @string
; result, @Tuple<T, [#]@Object>
; proc
; ;;Not sure of a use case, but you can override AfterCall and it will be called
; ;;after each bridge call, before control returns to your code.
; mreturn result
; endmethod
public async method GetAllCustomers, @Task<List<Customer>>
proc
;;force metadata to be loaded if its not
DataObjectMetadataBase.LookupType(^typeof(Customer))
data resultTpl = await CallMethod("GetAllCustomers", new List<Customer>(), string.Empty)
mreturn ((@IEnumerable<Customer>)resultTpl.Item2[1]).ToList<Customer>()
endmethod
public async method Arbitrario_MethodWithParameters, @Task<ArbitrarioReturnType>
proc
data intArray = new int[#] {5, 4, 3, 2, 1 }
data resultTpl = await CallMethod("Arbitrario.MethodWithParameters", 5, "hello", new string[#] { "this", "is", "strings" }, (@object)intArray, new string[0])
mreturn new ArbitrarioReturnType() { ReturnCode = ArgumentHelper.Argument<int>(0, resultTpl), IntList = ArgumentHelper.Argument<List<int>>(4, resultTpl), StringList = ArgumentHelper.Argument<List<string>>(5, resultTpl) }
endmethod
public async method Arbitrario_MethodWithParameters2, @Task<ArbitrarioReturnType>
aNumber, int
aString, @string
aStringArray, [#]string
aIntArray, [#]int
proc
data resultTpl = await CallMethod("Arbitrario.MethodWithParameters", aNumber, aString, aStringArray, aIntArray, new string[0])
mreturn new ArbitrarioReturnType() { ReturnCode = ArgumentHelper.Argument<int>(0, resultTpl), IntList = ArgumentHelper.Argument<List<int>>(4, resultTpl), StringList = ArgumentHelper.Argument<List<string>>(5, resultTpl) }
endmethod
public async method Arbitrario_Optional, @Task<ArbitrarioOptionalReturnType>
parm, @ArbitrarioOptionalParameter
proc
data resultTpl = await CallMethod("arbitrario_optional", parm.p1, ArgumentHelper.MaybeOptional(parm.p2), ArgumentHelper.MaybeOptional(parm.p3), ArgumentHelper.MaybeOptional(parm.p4))
data resultArray = resultTpl.Item2.ToList()
data returnValue = new ArbitrarioOptionalReturnType()
returnValue.p3 = ^as(resultArray[2], @string)
returnValue.p4 = ^as(resultArray[3], Nullable<int>)
mreturn returnValue
endmethod
public class ArbitrarioReturnType
public readwrite property ReturnCode, int
public readwrite property IntList, @List<int>
public readwrite property StringList, @List<string>
endclass
public class ArbitrarioOptionalParameter
public readwrite property p1, int
public readwrite property p2, @string
public readwrite property p3, @string
public readwrite property p4, int?
endclass
public class ArbitrarioOptionalReturnType
public readwrite property p3, @string
public readwrite property p4, int?
endclass
endclass
public class AvailabilityController extends ODataController
{CallableMethodConfigurationAttribute(IsFunction=true, ReturnsFromEntitySet=true)}
{EnableQuery()}
{AdapterRoutingFilter()}
{HttpGet("Availability/FindAvailability")}
public method FindAvailability, @ActionResult<List<Availability>>
{AdapterParameterAttribute}
filter, @AvailabiltyFilter
proc
mreturn Ok(new List<Availability>() { new Availability() { PointsCost = 4, Date = new DateTimeOffset(DateTime.Now) } })
endmethod
{CallableMethodConfigurationAttribute(IsFunction=true, ReturnsFromEntitySet=true)}
{EnableQuery()}
public method FindFirstAvailability, @Availability
{AdapterParameterAttribute}
filter, @AvailabiltyFilter
proc
mreturn new Availability() { PointsCost = 9999 }
endmethod
{HttpPost("DoAnAction")}
{EnableQuery()}
; {ApiVersionNeutral}
public method DoAnAction, @ActionResult<int>
{FromBody}
parameters, @ODataActionParameters
proc
if(parameters == ^null)
begin
mreturn new BadRequestResult()
end
mreturn new ActionResult<int>(5)
endmethod
{HttpGet("DoAFunction")}
public method DoAFunction, @IActionResult
parm1, int
parm2, @string
proc
if(parm2 == ^null)
begin
mreturn new BadRequestResult()
end
mreturn new OkObjectResult("Hello")
endmethod
endclass
; public class ODataActionAndFunctionController extends ODataController
;
;
;
; endclass
endnamespace