@@ -19,6 +19,7 @@ public enum CrudApiActionType
19
19
Create ,
20
20
GetAll ,
21
21
GetOne ,
22
+ GetMany ,
22
23
Merge ,
23
24
Update ,
24
25
Delete
@@ -85,7 +86,6 @@ private void LoadData()
85
86
var dataFilePath = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . DataFile ) , Path . GetDirectoryName ( _proxyConfiguration ? . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
86
87
if ( ! File . Exists ( dataFilePath ) )
87
88
{
88
- _logger ? . LogWarn ( $ "File { dataFilePath } not found. CRUD API will be disabled") ;
89
89
_configuration . Actions = Array . Empty < CrudApiAction > ( ) ;
90
90
return ;
91
91
}
@@ -166,68 +166,128 @@ private void GetAll(SessionEventArgs e, CrudApiAction action, IDictionary<string
166
166
167
167
private void GetOne ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
168
168
{
169
- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
170
- if ( item is null )
169
+ try
171
170
{
172
- SendNotFoundResponse ( e ) ;
173
- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
174
- return ;
171
+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
172
+ if ( item is null )
173
+ {
174
+ SendNotFoundResponse ( e ) ;
175
+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
176
+ return ;
177
+ }
178
+
179
+ SendJsonResponse ( JsonConvert . SerializeObject ( item , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
180
+ _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
175
181
}
182
+ catch ( Exception ex )
183
+ {
184
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
185
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
186
+ }
187
+ }
176
188
177
- SendJsonResponse ( JsonConvert . SerializeObject ( item , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
178
- _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
189
+ private void GetMany ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
190
+ {
191
+ try
192
+ {
193
+ var items = _data ? . SelectTokens ( ReplaceParams ( action . Query , parameters ) ) ;
194
+ if ( items is null )
195
+ {
196
+ items = Array . Empty < JToken > ( ) ;
197
+ }
198
+
199
+ SendJsonResponse ( JsonConvert . SerializeObject ( items , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
200
+ _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
201
+ }
202
+ catch ( Exception ex )
203
+ {
204
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
205
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
206
+ }
179
207
}
180
208
181
209
private void Create ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
182
210
{
183
- _data ? . Add ( JObject . Parse ( e . HttpClient . Request . BodyString ) ) ;
184
- SendJsonResponse ( JsonConvert . SerializeObject ( e . HttpClient . Request . BodyString , Formatting . Indented ) , HttpStatusCode . Created , e ) ;
185
- _logger ? . LogRequest ( [ $ "201 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
211
+ try
212
+ {
213
+ _data ? . Add ( JObject . Parse ( e . HttpClient . Request . BodyString ) ) ;
214
+ SendJsonResponse ( JsonConvert . SerializeObject ( e . HttpClient . Request . BodyString , Formatting . Indented ) , HttpStatusCode . Created , e ) ;
215
+ _logger ? . LogRequest ( [ $ "201 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
216
+ }
217
+ catch ( Exception ex )
218
+ {
219
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
220
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
221
+ }
186
222
}
187
223
188
224
private void Merge ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
189
225
{
190
- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
191
- if ( item is null )
226
+ try
227
+ {
228
+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
229
+ if ( item is null )
230
+ {
231
+ SendNotFoundResponse ( e ) ;
232
+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
233
+ return ;
234
+ }
235
+ var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
236
+ ( ( JContainer ) item ) ? . Merge ( update ) ;
237
+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
238
+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
239
+ }
240
+ catch ( Exception ex )
192
241
{
193
- SendNotFoundResponse ( e ) ;
194
- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
195
- return ;
242
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
243
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
196
244
}
197
- var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
198
- ( ( JContainer ) item ) ? . Merge ( update ) ;
199
- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
200
- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
201
245
}
202
246
203
247
private void Update ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
204
248
{
205
- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
206
- if ( item is null )
249
+ try
250
+ {
251
+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
252
+ if ( item is null )
253
+ {
254
+ SendNotFoundResponse ( e ) ;
255
+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
256
+ return ;
257
+ }
258
+ var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
259
+ ( ( JContainer ) item ) ? . Replace ( update ) ;
260
+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
261
+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
262
+ }
263
+ catch ( Exception ex )
207
264
{
208
- SendNotFoundResponse ( e ) ;
209
- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
210
- return ;
265
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
266
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
211
267
}
212
- var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
213
- ( ( JContainer ) item ) ? . Replace ( update ) ;
214
- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
215
- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
216
268
}
217
269
218
270
private void Delete ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
219
271
{
220
- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
221
- if ( item is null )
272
+ try
222
273
{
223
- SendNotFoundResponse ( e ) ;
224
- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
225
- return ;
226
- }
274
+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
275
+ if ( item is null )
276
+ {
277
+ SendNotFoundResponse ( e ) ;
278
+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
279
+ return ;
280
+ }
227
281
228
- item ? . Remove ( ) ;
229
- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
230
- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
282
+ item ? . Remove ( ) ;
283
+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
284
+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
285
+ }
286
+ catch ( Exception ex )
287
+ {
288
+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
289
+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
290
+ }
231
291
}
232
292
233
293
private Tuple < Action < SessionEventArgs , CrudApiAction , IDictionary < string , string > > , CrudApiAction , IDictionary < string , string > > ? GetMatchingActionHandler ( Request request )
@@ -291,6 +351,7 @@ private void Delete(SessionEventArgs e, CrudApiAction action, IDictionary<string
291
351
CrudApiActionType . Create => Create ,
292
352
CrudApiActionType . GetAll => GetAll ,
293
353
CrudApiActionType . GetOne => GetOne ,
354
+ CrudApiActionType . GetMany => GetMany ,
294
355
CrudApiActionType . Merge => Merge ,
295
356
CrudApiActionType . Update => Update ,
296
357
CrudApiActionType . Delete => Delete ,
0 commit comments