-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryMaker.cs
More file actions
794 lines (744 loc) · 32 KB
/
QueryMaker.cs
File metadata and controls
794 lines (744 loc) · 32 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
using QueryMakerLibrary.Components;
using QueryMakerLibrary.Constants;
using static QueryMakerLibrary.Components.Filter;
using static QueryMakerLibrary.Components.Sort;
namespace QueryMakerLibrary
{
/// <summary>
/// <see cref="QueryMakerLibrary" />'s main class which contains the components used for making the query.
/// </summary>
public sealed class QueryMaker
{
#region Constructors
/// <summary>
/// <para>Initializes a new instance of the <see cref="QueryMaker" /> class.</para>
/// <para>Set the parameter for operations which want to perform.</para>
/// <para>All components default to null.</para>
/// <para>NOTE: Any component left as null will not perform it's corresponding action.</para>
/// </summary>
/// <param name="filter">
/// <para>Instance of <see cref="Components.Filter" /> used for performing filtering action.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null filtering action will not be performed
/// </param>
/// <param name="sort">
/// <para>Instance of <see cref="Components.Sort" /> used for performing sorting action.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null sorting action will not be performed
/// </param>
/// <param name="page">
/// <para>Instance of <see cref="Components.Page" /> used for performing paging action.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null paging action will not be performed
/// </param>
/// <param name="select">
/// <para>Instance of <see cref="Components.Select" /> used for performing selecting action.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null selecting action will not be performed
/// </param>
public QueryMaker(Filter? filter = null, Sort? sort = null, Page? page = null, Select? select = null)
{
Filter = filter;
Sort = sort;
Page = page;
Select = select;
}
#endregion Constructors
#region Private Fields
private Sort? _sort = null;
#endregion Private Fields
#region Private Properties
private Sort? DeepestSort { get; set; } = null;
#endregion Private Properties
#region Public Properties
/// <summary>
/// <para>Property of type <see cref="Components.Filter" /> used for performing filtering.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null filtering will not be performed
/// </summary>
public Filter? Filter { get; set; } = null;
/// <summary>
/// <para>Property of type <see cref="Components.Sort" /> used for performing sorting.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null sorting will not be performed
/// </summary>
public Sort? Sort
{
get => _sort;
set
{
_sort = value;
DeepestSort = value;
}
}
/// <summary>
/// <para>Property of type <see cref="Components.Sort" /> used for performing paging.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null paging will not be performed
/// </summary>
public Page? Page { get; set; } = null;
/// <summary>
/// <para>Property of type <see cref="Components.Sort" /> used for performing selecting.</para>
/// <para>Defaults to null.</para>
/// NOTE: If left as null selecting will not be performed
/// </summary>
public Select? Select { get; set; } = null;
#endregion Public Properties
#region Public Instance Methods
/// <summary>
/// Adds expressions to <paramref name="query" /> using properties from this instance.
/// </summary>
/// <returns>
/// Instance of <see cref="QueryMakerResult{T}" /> with resulting paginated and unpaginated queries.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add expressions</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public QueryMakerResult<T> MakeQueryResult<T>(IQueryable<T> query)
{
try
{
return this.CreateActionsResult(query);
}
catch (Exception exception)
{
throw Errors.Exception(Errors.ErrorMessage, exception.Message);
}
}
/// <summary>
/// Adds expressions to <paramref name="query" /> using properties from this instance
/// </summary>
/// <returns>
/// Resulting <see cref="IQueryable{T}" /> with performed components actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add expressions</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public IQueryable<T> MakeQuery<T>(IQueryable<T> query)
=> MakeQueryResult(query).PaginatedQuery;
/// <summary>
/// <para>Shortcut method which only performs filtering on <paramref name="query" /> using <see cref="Filter" /> property from this instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T})" /> or <see cref="MakeQueryResult{T}(IQueryable{T})" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added filter actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add filtering</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public IQueryable<T> Filtering<T>(IQueryable<T> query)
=> new QueryMaker(filter: Filter).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs sorting on <paramref name="query" /> using <see cref="Sort" /> property from this instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T})" /> or <see cref="MakeQueryResult{T}(IQueryable{T})" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added sorting actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add sorting</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public IQueryable<T> Sorting<T>(IQueryable<T> query)
=> new QueryMaker(sort: Sort).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs paging on <paramref name="query" /> using <see cref="Page" /> property from this instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T})" /> or <see cref="MakeQueryResult{T}(IQueryable{T})" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added paging actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add paging</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public IQueryable<T> Paging<T>(IQueryable<T> query)
=> new QueryMaker(page: Page).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs selecting on <paramref name="query" /> using <see cref="Select" /> property from this instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T})" /> or <see cref="MakeQueryResult{T}(IQueryable{T})" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added select actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add selecting</para>
/// NOTE: If set null will throw exception
/// </param>
/// <exception cref="Exception" />
public IQueryable<T> Selecting<T>(IQueryable<T> query)
=> new QueryMaker(select: Select).MakeQuery(query);
/// <summary>
/// Add <see cref="Filter" /> component to this instance
/// </summary>
/// <param name="filter">
/// <para>Instance of <see cref="Components.Filter" /> to add.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter" /> component.
/// </returns>
public QueryMaker FilterBy(Filter filter)
{
Filter = filter;
return this;
}
/// <summary>
/// Add <see cref="Filter" /> component to this instance.
/// </summary>
/// <param name="field">
/// <para>Field to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter" /> component.
/// </returns>
public QueryMaker FilterBy(string field, FilterActions action, object? value,
bool ignoreCase = true, bool negate = false)
{
return FilterBy(new Filter(field, action, value, ignoreCase, negate));
}
/// <summary>
/// Add <see cref="Filter" /> component to this instance with required parameters for performing filtering using multiple fields.
/// </summary>
/// <param name="fields">
/// <para>Array of fields to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="fieldsOperation">
/// <para>Operation to perform between <paramref name="fields" />.</para>
/// <para>Refer to <see cref="FilterOperations" /> for possible values</para>
/// <para>Defaults to <see cref="FilterOperations.OrElse" />.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterOperations" /> value, then will throw exception when performing filtering.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter" /> component.
/// </returns>
public QueryMaker FilterBy(string[] fields, FilterActions action, object? value,
FilterOperations fieldsOperation = FilterOperations.OrElse, bool ignoreCase = true, bool negate = false)
{
return FilterBy(new Filter(fields, action, value, fieldsOperation, ignoreCase, negate));
}
/// <summary>
/// <para>Add <see cref="Filter" /> component to this instance as a joiner with subfilters to join.</para>
/// <para><see cref="Filter.IsJoiner" /> will be set to true by default</para>
/// </summary>
/// <param name="subFiltersOperation">
/// <para>Operation to perform between passed <paramref name="subFilters" />.</para>
/// <para>Refer to <see cref="FilterOperations" /> for possible values</para>
/// <para>NOTE: If not set to a valid <see cref="FilterOperations" /> value and <see cref="Filter.SubFilters" /> property has items, then will throw exception when joining subfilters.</para>
/// </param>
/// <param name="subFilters">
/// <para>Array of <see cref="Components.Filter" /> instances to be joined</para>
/// NOTE: If this property has items and <see cref="Filter.SubFiltersOperation" /> property is not set to a valid value, then will throw exception
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> added <paramref name="subFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker FilterBy(FilterOperations subFiltersOperation, params Filter[] subFilters)
{
return FilterBy(new Filter(subFiltersOperation, subFilters));
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with AndAlso evaluation.</para>
/// </summary>
/// <param name="subFilters">
/// <para>Array of <see cref="Components.Filter" /> instances to be joined</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <paramref name="subFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker AndAlsoFilterBy(params Filter[] subFilters)
{
if (Filter is not null)
{
Filter.SubFiltersOperation = FilterOperations.AndAlso;
Filter.SubFilters = subFilters;
}
return this;
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with AndAlso evaluation.</para>
/// </summary>
/// <param name="field">
/// <para>Field to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter.SubFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker AndAlsoFilterBy(string field, FilterActions action, object? value,
bool ignoreCase = true, bool negate = false)
{
return AndAlsoFilterBy(new Filter(field, action, value, ignoreCase, negate));
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with AndAlso evaluation.</para>
/// </summary>
/// <param name="fields">
/// <para>Array of fields to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="fieldsOperation">
/// <para>Operation to perform between <paramref name="fields" />.</para>
/// <para>Refer to <see cref="FilterOperations" /> for possible values</para>
/// <para>Defaults to <see cref="FilterOperations.OrElse" />.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterOperations" /> value, then will throw exception when performing filtering.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter.SubFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker AndAlsoFilterBy(string[] fields, FilterActions action, object? value,
FilterOperations fieldsOperation = FilterOperations.OrElse, bool ignoreCase = true, bool negate = false)
{
return AndAlsoFilterBy(new Filter(fields, action, value, fieldsOperation, ignoreCase, negate));
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with OrElse evaluation.</para>
/// </summary>
/// <param name="subFilters">
/// <para>Array of <see cref="Components.Filter" /> instances to be joined</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <paramref name="subFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker OrElseFilterBy(params Filter[] subFilters)
{
if (Filter is not null)
{
Filter.SubFiltersOperation = FilterOperations.OrElse;
Filter.SubFilters = subFilters;
}
return this;
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with OrElse evaluation.</para>
/// </summary>
/// <param name="field">
/// <para>Field to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter.SubFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker OrElseFilterBy(string field, FilterActions action, object? value,
bool ignoreCase = true, bool negate = false)
{
return OrElseFilterBy(new Filter(field, action, value, ignoreCase, negate));
}
/// <summary>
/// <para>Add subfilters to be performed after <see cref="Filter" /> with OrElse evaluation.</para>
/// </summary>
/// <param name="fields">
/// <para>Array of fields to perform filtering on.</para>
/// <para>If performing filtering on a primitive type enumerable (like a list of strings) then can leave empty.</para>
/// </param>
/// <param name="action">
/// <para>Action to perform.</para>
/// <para>Refer to <see cref="FilterActions" /> for possible values.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterActions" /> value, will throw exception when performing filtering.</para>
/// </param>
/// <param name="value">
/// <para>Value to filter by.</para>
/// </param>
/// <param name="fieldsOperation">
/// <para>Operation to perform between <paramref name="fields" />.</para>
/// <para>Refer to <see cref="FilterOperations" /> for possible values</para>
/// <para>Defaults to <see cref="FilterOperations.OrElse" />.</para>
/// <para>NOTE: If not set to a valid <see cref="FilterOperations" /> value, then will throw exception when performing filtering.</para>
/// </param>
/// <param name="ignoreCase">
/// <para>Set true to ignore case sensitivity on evaluation performed on filtering.</para>
/// <para>Defaults to true.</para>
/// </param>
/// <param name="negate">
/// <para>Set true to negate evaluation performed on filtering.</para>
/// <para>Defaults to false.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Filter.SubFilters" /> on <see cref="Filter" /> component.
/// </returns>
public QueryMaker OrElseFilterBy(string[] fields, FilterActions action, object? value,
FilterOperations fieldsOperation = FilterOperations.OrElse, bool ignoreCase = true, bool negate = false)
{
return OrElseFilterBy(new Filter(fields, action, value, fieldsOperation, ignoreCase, negate));
}
/// <summary>
/// Add <see cref="Sort" /> component to this instance
/// </summary>
/// <param name="sort">
/// <para>Instance of <see cref="Components.Sort" /> to add.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Sort" /> component.
/// </returns>
public QueryMaker SortBy(Sort sort)
{
Sort = sort;
return this;
}
/// <summary>
/// Add <see cref="Sort" /> component to this instance
/// </summary>
/// <param name="field">
/// <para>Field to sort by.</para>
/// <para>Defaults to empty string value.</para>
/// <para>NOTE: If left empty or null, then will throw exception.</para>
/// </param>
/// <param name="direction">
/// <para>Sorting direction.</para>
/// <para>Refer to <see cref="SortDirections" /> for possible values</para>
/// <para>Defaults to <see cref="SortDirections.Ascending" />.</para>
/// <para>NOTE: If not a valid direction, then will throw exception.</para>
/// </param>
/// <param name="then">
/// <para>Sorting performed after this one.</para>
/// <para>Defaults to null.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Sort" /> component.
/// </returns>
public QueryMaker SortBy(string field, SortDirections direction = SortDirections.Ascending, Sort? then = null)
{
return SortBy(new (field, direction, then));
}
/// <summary>
/// <para>Add sorting to be performed after the previous sort set.</para>
/// </summary>
/// <param name="sort">
/// <para>Instance of <see cref="Sort" /> to perform after the previous sorting set.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <paramref name="sort" /> on <see cref="Sort.Then" /> property of <see cref="Sort" /> component.
/// </returns>
public QueryMaker AndThenSortBy(Sort sort)
{
if (DeepestSort is null)
{
return SortBy(sort);
}
else
{
DeepestSort = DeepestSort.AndThen(sort).Then
?? throw new NullReferenceException(Errors.AddingSort);
}
return this;
}
/// <summary>
/// <para>Add sorting to be performed after previous sorting set.</para>
/// </summary>
/// <param name="field">
/// <para>Field to sort by.</para>
/// <para>Defaults to empty string value.</para>
/// <para>NOTE: If left empty or null, then will throw exception.</para>
/// </param>
/// <param name="direction">
/// <para>Sorting direction.</para>
/// <para>Refer to <see cref="SortDirections" /> for possible values</para>
/// <para>Defaults to <see cref="SortDirections.Ascending" />.</para>
/// <para>NOTE: If not a valid direction, then will throw exception.</para>
/// </param>
/// <param name="then">
/// <para>Sorting performed after this one.</para>
/// <para>Defaults to null.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added on <see cref="Sort.Then" /> property of <see cref="Sort" /> component.
/// </returns>
public QueryMaker AndThenSortBy(string field, SortDirections direction = SortDirections.Ascending, Sort? then = null)
{
if (DeepestSort is null)
{
return SortBy(field, direction, then);
}
else
{
DeepestSort = DeepestSort.AndThen(field, direction, then).Then
?? throw new NullReferenceException(Errors.AddingSort);
}
return this;
}
/// <summary>
/// Add <see cref="Page" /> component to this instance
/// </summary>
/// <param name="page">
/// <para>Instance of <see cref="Components.Page" /> to add.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Page" /> component.
/// </returns>
public QueryMaker WithPage(Page page)
{
Page = page;
return this;
}
/// <summary>
/// Add <see cref="Page" /> component to this instance
/// </summary>
/// <param name="skip">
/// <para>Quantity of elements to skip on paging action.</para>
/// <para>Defaults to 0.</para>
/// <para>NOTE: If left as 0 then will not perform skip.</para>
/// </param>
/// <param name="take">
/// <para>Quantity of elements to get on paging action.</para>
/// <para>Defaults to 0.</para>
/// <para>NOTE: If left as 0 then will not perform take.</para>
/// </param>
/// <param name="index">
/// <para>Field used as index for faster pagination.</para>
/// <para>Defaults to empty string.</para>
/// <para>NOTE: If left empty, then regular pagination will be performed without using an index.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Page" /> component.
/// </returns>
public QueryMaker WithPage(int skip = 0, int take = 0, string index = "")
{
return WithPage(new (skip, take, index));
}
/// <summary>
/// Add <see cref="Select" /> component to this instance
/// </summary>
/// <param name="select">
/// <para>Instance of <see cref="Components.Select" /> to add.</para>
/// </param>
public QueryMaker WithSelect(Select select)
{
Select = select;
return this;
}
/// <summary>
/// Add <see cref="Select" /> component to this instance
/// </summary>
/// <param name="fields">
/// <para>Fields to select on query.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Select" /> component.
/// </returns>
public QueryMaker WithSelect(params string[] fields)
{
return WithSelect(new Select(fields));
}
/// <summary>
/// Add <see cref="Select" /> component to this instance with distinct by operation.
/// <para>Note: If <paramref name="fields" /> is left empty then will return query as is.</para>
/// </summary>
/// <param name="fields">
/// <para>Fields by which to distinct by.</para>
/// </param>
/// <returns>
/// This instance of <see cref="QueryMaker" /> with added <see cref="Select" /> component.
/// </returns>
public QueryMaker WithDistinctBy(params string[] fields)
{
return WithSelect(new Select(fields, true));
}
#endregion Public Instance Methods
#region Public Static Methods
/// <summary>
/// Adds expressions to <paramref name="query" /> using properties from <paramref name="queryMaker" /> instance.
/// </summary>
/// <returns>
/// Instance of <see cref="QueryMakerResult{T}" /> with resulting paginated and unpaginated queries.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add expressions</para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="queryMaker">
/// Instance of QueryMaker class with components of actions to perform
/// <para>NOTE: If set null then will perform no actions and return and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static QueryMakerResult<T> MakeQueryResult<T>(IQueryable<T> query, QueryMaker queryMaker)
{
try
{
return queryMaker.MakeQueryResult(query);
}
catch (Exception exception)
{
throw Errors.Exception(Errors.ErrorMessage, exception.Message);
}
}
/// <summary>
/// Adds expressions to <paramref name="query" /> using properties from <paramref name="queryMaker" /> instance
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with performed <paramref name="queryMaker" /> components actions.
/// </returns>
/// <param name="query">
/// <para>The current instance of <see cref="IQueryable{T}" /></para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="queryMaker">
/// Instance of QueryMaker class with components of actions to perform
/// <para>NOTE: If set null then will perform no actions and return and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static IQueryable<T> MakeQuery<T>(IQueryable<T> query, QueryMaker queryMaker)
=> queryMaker.MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs filtering on <paramref name="query" /> using <paramref name="filter" />.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T}, QueryMaker)" /> or <see cref="MakeQueryResult{T}(IQueryable{T}, QueryMaker)" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added filter actions.
/// </returns>
/// <param name="query">
/// <para>The current instance of <see cref="IQueryable{T}" /></para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="filter">
/// The instance of <see cref= "Components.Filter" /> component to use for applying the filter on <paramref name="query" />.
/// <para>NOTE: If set null then will not perform filtering and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static IQueryable<T> Filtering<T>(IQueryable<T> query, Filter filter)
=> new QueryMaker(filter: filter).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs sorting on <paramref name="query" /> using <paramref name="sort" /> instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T}, QueryMaker)" /> or <see cref="MakeQueryResult{T}(IQueryable{T}, QueryMaker)" />.s</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added sorting actions.
/// </returns>
/// <param name="query">
/// <para>The current instance of <see cref="IQueryable{T}" /></para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="sort">
/// The instance of <see cref= "Components.Sort" /> component to use for applying the sort on <paramref name="query" />.
/// <para>NOTE: If set null then will not perform sorting and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static IQueryable<T> Sorting<T>(IQueryable<T> query, Sort sort)
=> new QueryMaker(sort: sort).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs paging actions on <paramref name="query" /> using <paramref name="page" /> instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T}, QueryMaker)" /> or <see cref="MakeQueryResult{T}(IQueryable{T}, QueryMaker)" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added paging actions.
/// </returns>
/// <param name="query">
/// <para>Instance of <see cref="IQueryable{T}" /> to add paging</para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="page">
/// The instance of <see cref= "Components.Page" /> component to use for applying the page on <paramref name="query" />.
/// <para>NOTE: If set null then will not perform paging and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static IEnumerable<T> Paging<T>(IQueryable<T> query, Page page)
=> new QueryMaker(page: page).MakeQuery(query);
/// <summary>
/// <para>Shortcut method which only performs selecting on <paramref name="query" /> using <paramref name="select" /> instance.</para>
/// <para>For perming all operations, use <see cref="MakeQuery{T}(IQueryable{T}, QueryMaker)" /> or <see cref="MakeQueryResult{T}(IQueryable{T}, QueryMaker)" />.</para>
/// </summary>
/// <returns>
/// An <see cref="IQueryable{T}" /> with added select actions.
/// </returns>
/// <param name="query">
/// <para>The current instance of <see cref="IQueryable{T}" /></para>
/// NOTE: If set null will throw exception
/// </param>
/// <param name="select">
/// The instance of <see cref= "Components.Select" /> component to use for applying the page on <paramref name="query" />.
/// <para>NOTE: If set null then will not perform selecting and return <paramref name="query" /> as is.</para>
/// </param>
/// <exception cref="Exception" />
public static IQueryable<T> Selecting<T>(IQueryable<T> query, Select select)
=> new QueryMaker(select: select).MakeQuery(query);
#endregion Public Static Methods
}
}