-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extensions.Data.cs
1100 lines (992 loc) · 49.1 KB
/
Extensions.Data.cs
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Related components
using System;
using System.Linq;
using System.Dynamic;
using System.Globalization;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using net.vieapps.Components.Utility;
using net.vieapps.Components.Repository;
#endregion
namespace net.vieapps.Services
{
public static partial class Extensions
{
#region Evaluate an formula
/// <summary>
/// Evaluates an Formula expression
/// </summary>
/// <param name="formula">The string that presents the formula</param>
/// <param name="object">The current object (that bound to 'this' parameter when formula is an Javascript expression)</param>
/// <param name="requestInfo">The requesting information</param>
/// <param name="params">The additional parameters</param>
/// <param name="embedObjects">The collection that presents objects are embed as global variables, can be simple classes (generic is not supported), strucs or delegates (for evaluating an Javascript expression)</param>
/// <param name="embedTypes">The collection that presents objects are embed as global types (for evaluating an Javascript expression)</param>
/// <returns></returns>
public static object Evaluate(this string formula, ExpandoObject @object, ExpandoObject requestInfo = null, ExpandoObject @params = null, IDictionary<string, object> embedObjects = null, IDictionary<string, Type> embedTypes = null)
{
// check
formula = formula?.Trim();
if (string.IsNullOrWhiteSpace(formula) || !formula.StartsWith("@"))
throw new InformationInvalidException($"The formula expression [{formula}] is invalid (the formula expression must started by the '@' character)");
var isJsExpression = formula.StartsWith("@[") && formula.EndsWith("]");
var position = isJsExpression ? -1 : formula.IndexOf("(");
if (position > 0 && !formula.EndsWith(")"))
throw new InformationInvalidException($"The formula expression [{formula}] is invalid (the open and close tokens are required when the formula got a parameter, ex: @request.Body(ContentType.ID) - just like an Javascript function)");
// prepare
object value = null;
var name = formula;
if (position > 0)
{
name = formula.Left(position).Trim();
formula = formula.Substring(position + 1, formula.Length - position - 2).Trim();
formula = string.IsNullOrWhiteSpace(formula) || formula.Equals("@") ? "@now" : formula;
}
// value of an JavaScript expression
if (isJsExpression || name.IsEquals("@script") || name.IsEquals("@javascript") || name.IsEquals("@js"))
value = formula.JsEvaluate(@object, requestInfo, @params, embedObjects, embedTypes);
// value of current object
else if (name.IsEquals("@current") || name.IsEquals("@object"))
value = formula.StartsWith("@")
? formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)
: @object?.Get(formula);
// value of request information
else if (name.IsStartsWith("@request"))
value = formula.StartsWith("@")
? formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)
: name.IsEquals("@request.Session")
? requestInfo?.Get<ExpandoObject>("Session")?.Get(formula)
: name.IsEquals("@request.Query")
? requestInfo?.Get<ExpandoObject>("Query")?.Get(formula)
: name.IsEquals("@request.Header")
? requestInfo?.Get<ExpandoObject>("Header")?.Get(formula)
: name.IsEquals("@request.Extra")
? requestInfo?.Get<ExpandoObject>("Extra")?.Get(formula)
: name.IsEquals("@request.Body")
? (requestInfo?.Get("Body") is ExpandoObject bodyAsExpando ? bodyAsExpando : requestInfo?.Get("Body") is string bodyAsString ? bodyAsString?.ToExpandoObject() : null)?.Get(formula)
: requestInfo?.Get(formula);
// value of parameters
else if (name.IsEquals("@params") || name.IsEquals("@global"))
value = formula.StartsWith("@")
? formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)
: @params?.Get(formula);
// current date-time
else if (name.IsEquals("@now") || name.IsEquals("@datetime.Now") || name.IsEquals("@date.Now") || name.IsEquals("@time.Now"))
value = name.IsEquals("@date.Now")
? DateTime.Parse($"{DateTime.Now:yyyy/MM/dd} 00:00:00")
: DateTime.Now;
// current date-time (as string)
else if (name.IsEquals("@today") || name.IsStartsWith("@todayStr") || name.IsEquals("@datetime.Today") || name.IsEquals("@date.Today") || name.IsEquals("@time.Today") || name.IsStartsWith("@nowStr") || name.IsStartsWith("@datetime.NowStr") || name.IsStartsWith("@date.NowStr") || name.IsStartsWith("@time.NowStr"))
value = DateTime.Now.ToDTString(false, name.IsStartsWith("@nowStr") || name.IsStartsWith("@datetime.NowStr") || name.IsStartsWith("@date.NowStr") || name.IsStartsWith("@time.NowStr"));
// import static text/html/json from a remote end-point
else if ((name.IsEquals("@import") || name.IsEquals("@static")) && (formula.IsStartsWith("https://") || formula.IsStartsWith("http://")))
try
{
string url = formula, element = null;
position = url.IndexOf(",");
if (position > 0)
{
element = url.Right(url.Length - position - 1).Trim();
url = url.Left(position).Trim();
}
var fetch = new Uri(url).FetchHttpAsync(null, 5);
if (fetch.Wait(5000))
value = string.IsNullOrWhiteSpace(element)
? fetch.Result
: fetch.Result?.ToExpandoObject()?.Get(element)?.ToString();
}
catch (Exception ex)
{
value = $"Error [{name}({formula})] => {ex.Message}";
}
// generate UUID
else if (name.IsEquals("@uuid") || name.IsEquals("@generateID") || name.IsEquals("@generateUUID"))
{
var mode = "md5";
position = formula.IndexOf(",");
if (position > 0)
{
mode = formula.Right(formula.Length - position - 1).Trim();
formula = formula.Left(position).Trim();
formula = string.IsNullOrWhiteSpace(formula) || formula.Equals("@") ? "@now" : formula;
}
value = (formula.StartsWith("@") ? formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString() ?? "" : formula).GenerateUUID(null, mode);
}
// get 'left-string'
else if (name.IsStartsWith("@left"))
{
var length = 0;
position = formula.IndexOf(",");
if (position > 0)
{
length = Int32.TryParse(formula.Right(formula.Length - position - 1).Trim(), out var len) ? len : 0;
formula = formula.Left(position).Trim();
formula = string.IsNullOrWhiteSpace(formula) || formula.Equals("@") ? "@now" : formula;
}
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString();
value = value is string @string ? @string.Left(length > 0 ? length : @string.Length) : value;
}
// get 'right-string'
else if (name.IsStartsWith("@right"))
{
var length = 0;
position = formula.IndexOf(",");
if (position > 0)
{
length = Int32.TryParse(formula.Right(formula.Length - position - 1).Trim(), out var len) ? len : 0;
formula = formula.Left(position).Trim();
formula = string.IsNullOrWhiteSpace(formula) || formula.Equals("@") ? "@now" : formula;
}
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString();
value = value is string @string ? @string.Right(length > 0 ? length : @string.Length) : value;
}
// convert the value to floating point number
else if (name.IsStartsWith("@toDec") || name.IsStartsWith("@toNum") || name.IsStartsWith("@toFloat") || name.IsStartsWith("@toDouble"))
{
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes);
value = value is DateTime datetime
? datetime.ToUnixTimestamp().As<decimal>()
: value != null && value.IsNumericType()
? value.As<decimal>()
: value;
}
// convert the value to integral number
else if (name.IsStartsWith("@toInt") || name.IsStartsWith("@toLong") || name.IsStartsWith("@toByte") || name.IsStartsWith("@toShort"))
{
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes);
value = value is DateTime datetime
? datetime.ToUnixTimestamp()
: value != null && value.IsNumericType()
? value.As<long>()
: value;
}
// convert the value to string
else if (name.IsStartsWith("@toStr") || name.IsStartsWith("@date.toStr") || name.IsStartsWith("@time.toStr"))
{
var cultureInfoName = "";
var format = formula.IsStartsWith("@date")
? "dd/MM/yyyy HH:mm:ss"
: formula.IsStartsWith("@time") ? "hh:mm tt @ dd/MM/yyyy" : "";
position = formula.IndexOf(",");
if (position > 0)
{
format = formula.Right(formula.Length - position - 1).Trim();
formula = formula.Left(position).Trim();
formula = string.IsNullOrWhiteSpace(formula) || formula.Equals("@") ? "@now" : formula;
position = format.IndexOf("|");
if (position > 0)
{
cultureInfoName = format.Right(format.Length - position - 1).Trim();
format = format.Left(position).Trim();
}
}
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes);
value = value == null || string.IsNullOrWhiteSpace(format)
? value?.ToString()
: value.IsDateTimeType()
? string.IsNullOrWhiteSpace(cultureInfoName) ? value.As<DateTime>().ToString(format) : value.As<DateTime>().ToString(format, CultureInfo.GetCultureInfo(cultureInfoName))
: value.IsFloatingPointType()
? string.IsNullOrWhiteSpace(cultureInfoName) ? value.As<decimal>().ToString(format) : value.As<decimal>().ToString(format, CultureInfo.GetCultureInfo(cultureInfoName))
: value.IsIntegralType()
? string.IsNullOrWhiteSpace(cultureInfoName) ? value.As<long>().ToString(format) : value.As<long>().ToString(format, CultureInfo.GetCultureInfo(cultureInfoName))
: value.ToString();
}
// convert the value to lower-case string
else if (name.IsStartsWith("@toLower"))
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString().ToLower();
// convert the value to upper-case string
else if (name.IsStartsWith("@toUpper"))
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString().ToUpper();
// convert the value to capitalized-words string
else if (name.IsStartsWith("@toCapitalizedWords"))
value = formula.Evaluate(@object, requestInfo, @params, embedObjects, embedTypes)?.ToString().GetCapitalizedWords();
// unknown => return the original formula
else
value = $"{name}{(string.IsNullOrWhiteSpace(formula) ? "" : $"({formula})")}";
return value;
}
/// <summary>
/// Evaluates an Formula expression
/// </summary>
/// <param name="formula">The string that presents the formula</param>
/// <param name="object">The object for fetching data from</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="params">The additional parameters for fetching data from</param>
/// <param name="embedObjects">The collection that presents objects are embed as global variables, can be simple classes (generic is not supported), strucs or delegates (for evaluating an Javascript expression)</param>
/// <param name="embedTypes">The collection that presents objects are embed as global types (for evaluating an Javascript expression)</param>
/// <returns></returns>
public static object Evaluate(this string formula, object @object = null, RequestInfo requestInfo = null, ExpandoObject @params = null, IDictionary<string, object> embedObjects = null, IDictionary<string, Type> embedTypes = null)
=> formula?.Evaluate(@object is IBusinessEntity bizObject ? bizObject.ToExpandoObject() : @object?.ToExpandoObject(), requestInfo?.AsExpandoObject, @params, embedObjects, embedTypes);
#endregion
#region Filter
/// <summary>
/// Prepares the comparing values of the filtering expression (means evaluating all Formula/Javascript expressions)
/// </summary>
/// <param name="filterBy">The filtering expression</param>
/// <param name="object">The object for fetching data from</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="params">The additional parameters for fetching data from</param>
/// <param name="onCompleted">The action to run when the preparing process is completed</param>
/// <returns>The filtering expression with all formula/expression values had been evaluated</returns>
public static IFilterBy Prepare(this IFilterBy filterBy, ExpandoObject @object, ExpandoObject requestInfo = null, ExpandoObject @params = null, Action<IFilterBy> onCompleted = null)
{
// prepare value of a single filter (that presented by an JavaScript/Formulla expression)
if (filterBy is FilterBy filter)
{
if (filter?.Value != null && filter.Value is string value && value.StartsWith("@"))
filter.Value = value.Evaluate(@object, requestInfo, @params);
}
// prepare a group of filters
else
(filterBy as FilterBys)?.Children?.ForEach(filterby => filterby?.Prepare(@object, requestInfo, @params, onCompleted));
// complete
onCompleted?.Invoke(filterBy);
return filterBy;
}
/// <summary>
/// Prepares the comparing values of the filtering expression (means evaluating all Formula/Javascript expressions)
/// </summary>
/// <param name="filterBy">The filtering expression</param>
/// <param name="object">The object for fetching data from</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="params">The additional parameters for fetching data from</param>
/// <param name="onCompleted">The action to run when the preparing process is completed</param>
/// <returns>The filtering expression with all formula/expression values had been evaluated</returns>
public static IFilterBy Prepare(this IFilterBy filterBy, object @object = null, RequestInfo requestInfo = null, ExpandoObject @params = null, Action<IFilterBy> onCompleted = null)
=> filterBy?.Prepare(@object is IBusinessEntity bizObject ? bizObject.ToExpandoObject() : @object?.ToExpandoObject(), requestInfo?.AsExpandoObject, @params, onCompleted);
/// <summary>
/// Prepares the comparing values of the filtering expression (means evaluating all Formula/Javascript expressions)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filterBy">The filtering expression</param>
/// <param name="object">The object for fetching data from</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="params">The additional parameters for fetching data from</param>
/// <param name="onCompleted">The action to run when the preparing process is completed</param>
/// <returns>The filtering expression with all formula/expression values had been evaluated</returns>
public static IFilterBy<T> Prepare<T>(this IFilterBy<T> filterBy, object @object = null, RequestInfo requestInfo = null, ExpandoObject @params = null, Action<IFilterBy<T>> onCompleted = null) where T : class
=> (filterBy as IFilterBy)?.Prepare(@object, requestInfo, @params, onCompleted as Action<IFilterBy>) as IFilterBy<T>;
/// <summary>
/// Prepares the comparing values of the filtering expression (means evaluating all Formula/Javascript expressions)
/// </summary>
/// <param name="filterBy">The filtering expression</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="onCompleted">The action to run when the preparing process is completed</param>
/// <returns>The filtering expression with all formula/expression values had been evaluated</returns>
public static IFilterBy Prepare(this IFilterBy filterBy, RequestInfo requestInfo, Action<IFilterBy> onCompleted = null)
{
filterBy?.Prepare(null, requestInfo, null);
onCompleted?.Invoke(filterBy);
return filterBy;
}
/// <summary>
/// Prepares the comparing values of the filtering expression (means evaluating all Formula/Javascript expressions)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filterBy">The filtering expression</param>
/// <param name="requestInfo">The object that presents the information of the request information</param>
/// <param name="onCompleted">The action to run when the preparing process is completed</param>
/// <returns>The filtering expression with all formula/expression values had been evaluated</returns>
public static IFilterBy<T> Prepare<T>(this IFilterBy<T> filterBy, RequestInfo requestInfo, Action<IFilterBy<T>> onCompleted = null) where T : class
{
filterBy?.Prepare(null, requestInfo, null);
onCompleted?.Invoke(filterBy);
return filterBy;
}
/// <summary>
/// Gets a child expression (comparision expression) by the specified name
/// </summary>
/// <param name="filter"></param>
/// <param name="name">The name of a child expression</param>
/// <returns></returns>
public static IFilterBy GetChild(this IFilterBy filter, string name)
=> filter != null && filter is FilterBys && !string.IsNullOrWhiteSpace(name)
? (filter as FilterBys).Children?.FirstOrDefault(filterby => filterby is FilterBy filterBy && name.IsEquals(filterBy.Attribute))
: null;
/// <summary>
/// Gets a child expression (comparision expression) by the specified name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter"></param>
/// <param name="name">The name of a child expression</param>
/// <returns></returns>
public static IFilterBy<T> GetChild<T>(this IFilterBy<T> filter, string name) where T : class
=> filter != null && filter is FilterBys<T> && !string.IsNullOrWhiteSpace(name)
? (filter as IFilterBy).GetChild(name) as IFilterBy<T>
: null;
/// <summary>
/// Gets the value of a child expression (comparision expression) by the specified name
/// </summary>
/// <param name="filter"></param>
/// <param name="name">The name of a child expression</param>
/// <returns></returns>
public static string GetValue(this IFilterBy filter, string name)
=> filter != null && !string.IsNullOrWhiteSpace(name)
? (filter.GetChild(name) as FilterBy)?.Value as string
: null;
/// <summary>
/// Gets the value of a child expression (comparision expression) by the specified name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter"></param>
/// <param name="name">The name of a child expression</param>
/// <returns></returns>
public static string GetValue<T>(this IFilterBy<T> filter, string name) where T : class
=> filter != null && !string.IsNullOrWhiteSpace(name)
? (filter.GetChild(name) as FilterBy<T>)?.Value as string
: null;
static IFilterBy<T> GetFilterBy<T>(this JObject expression) where T : class
{
var property = expression.Properties()?.FirstOrDefault();
if (property == null || property.Value == null)
return null;
IFilterBy<T> filter = null;
var attribute = property.Name;
// group of comparisions
if (attribute.IsEquals("And") || attribute.IsEquals("Or"))
{
filter = attribute.IsEquals("Or") ? Filters<T>.Or() : Filters<T>.And();
(property.Value is JObject pobj ? pobj.ToJArray(kvp => new JObject { { kvp.Key, kvp.Value } }) : property.Value).ForEach(exp => (filter as FilterBys<T>).Add(exp != null && exp is JObject eobj ? eobj.GetFilterBy<T>() : null));
if (!(filter as FilterBys<T>).Children.Any())
filter = null;
}
// single comparision
else
{
var @operator = "";
var value = JValue.CreateNull();
// special comparison
if (property.Value is JValue pvalue)
{
@operator = pvalue.Value.ToString();
if ([email protected]("IsNull") && [email protected]("IsNotNull") && [email protected]("IsEmpty") && [email protected]("IsNotEmpty"))
@operator = null;
}
// normal comparison
else if (property.Value is JObject pobj)
{
property = pobj.Properties()?.FirstOrDefault();
if (property != null && property.Value != null && property.Value is JValue jvalue && jvalue.Value != null)
{
@operator = property.Name;
value = jvalue;
}
else
@operator = null;
}
// unknown comparison
else
@operator = null;
filter = @operator != null
? new FilterBy<T>(new JObject
{
{ "Attribute", attribute },
{ "Operator", @operator },
{ "Value", value }
})
: null;
}
return filter;
}
/// <summary>
/// Converts the (client) JSON object to a filtering expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static IFilterBy<T> ToFilterBy<T>(this JObject expression) where T : class
{
var property = expression.Properties()?.FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.Name) && !p.Name.IsEquals("Query"));
if (property == null || property.Value == null)
return null;
var filter = property.Name.IsEquals("Or") ? Filters<T>.Or() : Filters<T>.And();
if (!property.Name.IsEquals("And") && !property.Name.IsEquals("Or"))
expression.ToJArray(kvp => new JObject
{
{ kvp.Key, kvp.Value }
}).ForEach(exp => filter.Add((exp as JObject).GetFilterBy<T>()));
else
{
var children = property.Name.IsEquals("Or") ? expression["Or"] : expression["And"];
(children is JObject cobj ? cobj.ToJArray(kvp => new JObject
{
{ kvp.Key, kvp.Value }
}) : children as JArray).ForEach(exp => filter.Add(exp != null && exp is JObject eobj ? eobj.GetFilterBy<T>() : null));
}
return filter != null && filter.Children.Any() ? filter : null;
}
/// <summary>
/// Converts the (client) ExpandoObject object to a filtering expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static IFilterBy<T> ToFilterBy<T>(this ExpandoObject expression) where T : class
=> expression != null ? JObject.FromObject(expression).ToFilterBy<T>() : null;
/// <summary>
/// Converts the (server) JSON object to a filtering expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static IFilterBy<T> ToFilter<T>(this JObject expression) where T : class
{
var @operator = expression?.Get<string>("Operator");
return @operator != null
? @operator.IsEquals("Or") || @operator.IsEquals("And")
? new FilterBys<T>(expression, @operator.IsEquals("Or") ? GroupOperator.Or : GroupOperator.And)
: new FilterBy<T>(expression) as IFilterBy<T>
: null;
}
/// <summary>
/// Converts the (server) ExpandoObject object to a filtering expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static IFilterBy<T> ToFilter<T>(this ExpandoObject expression) where T : class
=> expression != null ? JObject.FromObject(expression).ToFilter<T>() : null;
static JToken GetClientJson(this JToken serverJson, out string name)
{
var @operator = serverJson.Get<string>("Operator");
var children = serverJson.Get<JArray>("Children");
if (children == null)
{
@operator = @operator ?? CompareOperator.Equals.ToString();
name = serverJson.Get<string>("Attribute");
return @operator.IsEquals("IsNull") || @operator.IsEquals("IsNotNull") || @operator.IsEquals("IsEmpty") || @operator.IsEquals("IsNotEmpty")
? new JValue(@operator) as JToken
: new JObject { [@operator] = serverJson["Value"] };
}
else
{
@operator = @operator ?? GroupOperator.And.ToString();
name = @operator;
return children.ToJArray(json =>
{
var value = json.GetClientJson(out @operator);
return new JObject { [@operator] = json.GetClientJson(out @operator) };
});
}
}
/// <summary>
/// Converts the filtering expression to JSON for using at client-side
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter"></param>
/// <param name="query"></param>
/// <returns></returns>
public static JObject ToClientJson<T>(this IFilterBy<T> filter, string query = null) where T : class
{
var clientJson = new JObject();
if (!string.IsNullOrWhiteSpace(query))
clientJson["Query"] = query;
var json = filter.ToJson().GetClientJson(out var @operator);
clientJson[@operator] = json;
return clientJson;
}
/// <summary>
/// Generates the UUID of this filter expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter"></param>
/// <returns></returns>
public static string GenerateUUID<T>(this IFilterBy<T> filter) where T : class
=> filter?.ToClientJson().ToString(Formatting.None).ToLower().GenerateUUID();
#endregion
#region Sort
/// <summary>
/// Converts the (client) JSON object to a sorting expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static SortBy<T> ToSortBy<T>(this JObject expression) where T : class
{
SortBy<T> sort = null;
expression?.ForEach(kvp =>
{
var attribute = kvp.Key;
if (!((kvp.Value as JValue).Value?.ToString() ?? "Ascending").TryToEnum<SortMode>(out var mode))
mode = SortMode.Ascending;
sort = sort != null
? mode.Equals(SortMode.Ascending)
? sort.ThenByAscending(attribute)
: sort.ThenByDescending(attribute)
: mode.Equals(SortMode.Ascending)
? Sorts<T>.Ascending(attribute)
: Sorts<T>.Descending(attribute);
});
return sort;
}
/// <summary>
/// Converts the (client) ExpandoObject object to a sorting expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static SortBy<T> ToSortBy<T>(this ExpandoObject expression) where T : class
=> expression != null ? JObject.FromObject(expression).ToSortBy<T>() : null;
/// <summary>
/// Converts the (server) JSON object to a sorting expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static SortBy<T> ToSort<T>(this JObject expression) where T : class
=> !string.IsNullOrWhiteSpace(expression?.Get<string>("Attribute")) ? new SortBy<T>(expression) : null;
/// <summary>
/// Converts the (server) ExpandoObject object to a sorting expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static SortBy<T> ToSort<T>(this ExpandoObject expression) where T : class
=> expression != null ? JObject.FromObject(expression).ToSort<T>() : null;
static void GetClientJson(this JToken serverJson, JObject clientJson)
{
var attribute = serverJson?.Get<string>("Attribute");
if (!string.IsNullOrWhiteSpace(attribute))
clientJson[attribute] = serverJson.Get("Mode", "Ascending");
serverJson?.Get<JObject>("ThenBy")?.GetClientJson(clientJson);
}
/// <summary>
/// Converts the sorting expression to JSON for using at client-side
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sort"></param>
/// <returns></returns>
public static JObject ToClientJson<T>(this SortBy<T> sort) where T : class
{
JObject clientJson = null;
if (sort != null)
{
clientJson = new JObject();
sort.ToJson().GetClientJson(clientJson);
}
return clientJson;
}
/// <summary>
/// Generates the UUID of this sort expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sortby"></param>
/// <returns></returns>
public static string GenerateUUID<T>(this SortBy<T> sortby) where T : class
=> sortby?.ToClientJson().ToString(Formatting.None).ToLower().GenerateUUID();
#endregion
#region Pagination
/// <summary>
/// Computes the total of pages from total of records and page size
/// </summary>
/// <param name="totalRecords"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static int GetTotalPages(long totalRecords, int pageSize)
{
var totalPages = pageSize > 0 ? (int)(totalRecords / pageSize) : 1;
if (pageSize > 0 && totalRecords - (totalPages * pageSize) > 0)
totalPages += 1;
return totalPages;
}
/// <summary>
/// Computes the total of pages from total of records and page size
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static int GetTotalPages(this (long TotalRecords, int TotalPages) info)
=> Extensions.GetTotalPages(info.TotalRecords, info.TotalPages);
/// <summary>
/// Gets the pagination from this JSON
/// </summary>
/// <param name="pagination"></param>
/// <returns></returns>
public static (long TotalRecords, int TotalPages, int PageSize, int PageNumber) GetPagination(this JObject pagination)
{
var totalRecords = pagination["TotalRecords"] != null && pagination["TotalRecords"] is JValue totalRecordsAsJValue && totalRecordsAsJValue.Value != null
? totalRecordsAsJValue.Value.CastAs<long>()
: -1;
var pageSize = pagination["PageSize"] != null && pagination["PageSize"] is JValue pageSizeAsJValue && pageSizeAsJValue.Value != null
? pageSizeAsJValue.Value.CastAs<int>()
: 20;
if (pageSize < 0)
pageSize = 20;
var totalPages = pagination["TotalPages"] != null && pagination["TotalPages"] is JValue totalPagesAsJValue && totalPagesAsJValue.Value != null
? totalPagesAsJValue.Value.CastAs<int>()
: -1;
if (totalPages < 0)
totalPages = Extensions.GetTotalPages(totalRecords, pageSize);
var pageNumber = pagination["PageNumber"] != null && pagination["PageNumber"] is JValue pageNumberAsJValue && pageNumberAsJValue.Value != null
? pageNumberAsJValue.Value.CastAs<int>()
: 20;
if (pageNumber < 1)
pageNumber = 1;
else if (totalPages > 0 && pageNumber > totalPages)
pageNumber = totalPages;
return (totalRecords, totalPages, pageSize, pageNumber);
}
/// <summary>
/// Gets the pagination from this object
/// </summary>
/// <param name="pagination"></param>
/// <returns></returns>
public static (long TotalRecords, int TotalPages, int PageSize, int PageNumber) GetPagination(this ExpandoObject pagination)
{
var totalRecords = pagination.Get<long>("TotalRecords", -1);
var pageSize = pagination.Get("PageSize", 20);
pageSize = pageSize < 0 ? 10 : pageSize;
var totalPages = pagination.Get("TotalPages", -1);
totalPages = totalPages < 0
? totalRecords > 0 ? Extensions.GetTotalPages(totalRecords, pageSize) : 0
: totalPages;
var pageNumber = pagination.Get("PageNumber", 1);
pageNumber = pageNumber < 1
? 1
: totalPages > 0 && pageNumber > totalPages ? totalPages : pageNumber;
return (totalRecords, totalPages, pageSize, pageNumber);
}
/// <summary>
/// Gets the pagination JSON
/// </summary>
/// <param name="totalRecords"></param>
/// <param name="totalPages"></param>
/// <param name="pageSize"></param>
/// <param name="pageNumber"></param>
/// <returns></returns>
public static JObject GetPagination(long totalRecords, int totalPages, int pageSize, int pageNumber)
=> new JObject
{
{ "TotalRecords", totalRecords },
{ "TotalPages", totalPages},
{ "PageSize", pageSize },
{ "PageNumber", pageNumber }
};
/// <summary>
/// Gets the pagination JSON
/// </summary>
/// <param name="pagination"></param>
/// <returns></returns>
public static JObject GetPagination(this (long TotalRecords, int TotalPages, int PageSize, int PageNumber) pagination)
=> Extensions.GetPagination(pagination.TotalRecords, pagination.TotalPages, pagination.PageSize, pagination.PageNumber);
#endregion
#region Cache keys
/// <summary>
/// Gets the caching key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static string GetCacheKey<T>() where T : class
=> typeof(T).GetTypeName(true);
/// <summary>
/// Gets the caching key
/// </summary>
/// <param name="prefix">The string that presents the prefix of the caching key</param>
/// <param name="pageSize">The page size</param>
/// <param name="pageNumber">The page number</param>
/// <param name="addPageNumberHolder">true to add page number as a holder ({{pageNumber}})</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns></returns>
public static string GetCacheKey(string prefix, int pageSize = 0, int pageNumber = 0, bool addPageNumberHolder = false, string suffix = null)
=> $"{prefix}{(pageNumber > 0 ? $"#p:{(addPageNumberHolder ? "{{pageNumber}}" : $"{pageNumber}")}{(pageSize > 0 ? $"~{pageSize}" : "")}" : "")}{suffix ?? ""}";
/// <summary>
/// Gets the caching key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prefix">The string that presents the prefix of the caching key</param>
/// <param name="pageSize">The page size</param>
/// <param name="pageNumber">The page number</param>
/// <param name="addPageNumberHolder">true to add page number as '[page-number]' holder</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns></returns>
public static string GetCacheKey<T>(string prefix, int pageSize = 0, int pageNumber = 0, bool addPageNumberHolder = false, string suffix = null) where T : class
=> $"{Extensions.GetCacheKey<T>()}{Extensions.GetCacheKey(prefix, pageSize, pageNumber, addPageNumberHolder, suffix)}";
/// <summary>
/// Gets the caching key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">The filter expression</param>
/// <param name="sort">The sort expression</param>
/// <param name="pageSize">The page size</param>
/// <param name="pageNumber">The page number</param>
/// <param name="addPageNumberHolder">true to add page number as '[page-number]' holder</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns></returns>
public static string GetCacheKey<T>(IFilterBy<T> filter, SortBy<T> sort, int pageSize = 0, int pageNumber = 0, bool addPageNumberHolder = false, string suffix = null) where T : class
=> Extensions.GetCacheKey<T>($"{(filter != null ? $"#f:{filter.GenerateUUID()}" : "")}{(sort != null ? $"#s:{sort.GenerateUUID()}" : "")}", pageSize, pageNumber, addPageNumberHolder, suffix);
static List<string> KeyPatterns => "total,json,xml".ToList();
static List<string> RelatedKeyPatterns => "thumbnails,attachments,others,newers,olders".ToList();
/// <summary>
/// Gets the related caching key for working with collection of objects
/// </summary>
/// <param name="key">The pre-buid key</param>
/// <param name="pageSize">The size of one page</param>
/// <returns>The collection presents all related caching keys (10 first pages)</returns>
public static List<string> GetRelatedCacheKeys(string key, int pageSize = 0)
{
var singleKey = Extensions.GetCacheKey(key, 0, 1);
var relatedKeys = new List<string> { key, singleKey };
Extensions.KeyPatterns.Concat(Extensions.RelatedKeyPatterns).ForEach(pattern =>
{
relatedKeys.Add($"{key}:{pattern}");
relatedKeys.Add($"{singleKey}:{pattern}");
});
var paginationKey = Extensions.GetCacheKey(key, pageSize > 0 ? pageSize : 20, 1, true);
for (var pageNumber = 1; pageNumber <= 10; pageNumber++)
{
var pageKey = paginationKey.Replace(StringComparison.OrdinalIgnoreCase, "{{pageNumber}}", $"{pageNumber}");
relatedKeys.Add(pageKey);
Extensions.KeyPatterns.ForEach(pattern => relatedKeys.Add($"{pageKey}:{pattern}"));
}
return relatedKeys;
}
/// <summary>
/// Gets the related caching key for working with collection of objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">The filter expression</param>
/// <param name="sort">The sort expression</param>
/// <param name="pageSize">The size of one page</param>
/// <returns>The collection presents all related caching keys (10 first pages)</returns>
public static List<string> GetRelatedCacheKeys<T>(IFilterBy<T> filter, SortBy<T> sort, int pageSize = 0) where T : class
=> Extensions.GetRelatedCacheKeys(Extensions.GetCacheKey<T>(filter, sort), pageSize);
/// <summary>
/// Gets the caching key for workingwith the number of total objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prefix">The string that presents the prefix of the caching key</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfTotalObjects<T>(string prefix, string suffix = null) where T : class
=> $"{Extensions.GetCacheKey<T>(prefix)}:total{suffix ?? ""}";
/// <summary>
/// Gets the caching key for workingwith the number of total objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">The filter expression</param>
/// <param name="sort">The sort expression</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfTotalObjects<T>(IFilterBy<T> filter, SortBy<T> sort, string suffix = null) where T : class
=> Extensions.GetCacheKeyOfTotalObjects<T>((filter != null ? $"#f:{filter.GenerateUUID()}" : "") + (sort != null ? $"#s:{sort.GenerateUUID()}" : ""), suffix);
/// <summary>
/// Gets the caching key for working with the JSON of objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prefix">The string that presents the prefix of the caching key</param>
/// <param name="pageNumber">The page number</param>
/// <param name="pageSize">The page size</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfObjectsJson<T>(string prefix, int pageSize = 0, int pageNumber = 0, string suffix = null) where T : class
=> $"{Extensions.GetCacheKey<T>(prefix, pageSize, pageNumber)}:json{suffix ?? ""}";
/// <summary>
/// Gets the caching key for working with the JSON of objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">The filter expression</param>
/// <param name="sort">The sort expression</param>
/// <param name="pageNumber">The page number</param>
/// <param name="pageSize">The page size</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfObjectsJson<T>(IFilterBy<T> filter, SortBy<T> sort, int pageSize = 0, int pageNumber = 0, string suffix = null) where T : class
=> Extensions.GetCacheKeyOfObjectsJson<T>((filter != null ? $"#f:{filter.GenerateUUID()}" : "") + (sort != null ? $"#s:{sort.GenerateUUID()}" : ""), pageSize, pageNumber, suffix);
/// <summary>
/// Gets the caching key for working with the XML of objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prefix">The string that presents the prefix of the caching key</param>
/// <param name="pageNumber">The page number</param>
/// <param name="pageSize">The page size</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfObjectsXml<T>(string prefix, int pageSize = 0, int pageNumber = 0, string suffix = null) where T : class
=> $"{Extensions.GetCacheKey<T>(prefix, pageSize, pageNumber)}:xml{suffix ?? ""}";
/// <summary>
/// Gets the caching key for working with the XML of objects
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filter">The filter expression</param>
/// <param name="sort">The sort expression</param>
/// <param name="pageNumber">The page number</param>
/// <param name="pageSize">The page size</param>
/// <param name="suffix">The string that presents the suffix of the caching key</param>
/// <returns>The string that presents a caching key</returns>
public static string GetCacheKeyOfObjectsXml<T>(IFilterBy<T> filter, SortBy<T> sort, int pageSize = 0, int pageNumber = 0, string suffix = null) where T : class
=> Extensions.GetCacheKeyOfObjectsXml<T>((filter != null ? $"#f:{filter.GenerateUUID()}" : "") + (sort != null ? $"#s:{sort.GenerateUUID()}" : ""), pageSize, pageNumber, suffix);
#endregion
#region Double braces tokens & date-time quater
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="doubleBracesTokens"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this List<Tuple<string, string>> doubleBracesTokens, ExpandoObject @object, ExpandoObject requestInfo = null, ExpandoObject @params = null)
=> doubleBracesTokens == null || doubleBracesTokens.Count < 1
? new Dictionary<string, object>()
: doubleBracesTokens
.Select(token => token.Item2)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToDictionary(token => token, token => token.StartsWith("@") ? token.Evaluate(@object, requestInfo, @params) : token);
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="doubleBracesTokens"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this List<Tuple<string, string>> doubleBracesTokens, JToken @object, JToken requestInfo = null, JToken @params = null)
=> doubleBracesTokens?.PrepareDoubleBracesParameters(@object?.ToExpandoObject(), requestInfo?.ToExpandoObject(), @params?.ToExpandoObject());
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="doubleBracesTokens"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this List<Tuple<string, string>> doubleBracesTokens, object @object = null, RequestInfo requestInfo = null, ExpandoObject @params = null)
=> doubleBracesTokens?.PrepareDoubleBracesParameters(@object is IBusinessEntity bizObject ? bizObject.ToExpandoObject() : @object?.ToExpandoObject(), requestInfo?.AsExpandoObject, @params);
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="string"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this string @string, ExpandoObject @object, ExpandoObject requestInfo = null, ExpandoObject @params = null)
=> string.IsNullOrWhiteSpace(@string)
? new Dictionary<string, object>()
: @string.GetDoubleBracesTokens().PrepareDoubleBracesParameters(@object, requestInfo, @params);
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="string"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this string @string, JToken @object, JToken requestInfo = null, JToken @params = null)
=> @string?.PrepareDoubleBracesParameters(@object?.ToExpandoObject(), requestInfo?.ToExpandoObject(), @params?.ToExpandoObject()) ?? new Dictionary<string, object>();
/// <summary>
/// Prepares the parameters of double braces (mustache-style - {{ }}) parameters
/// </summary>
/// <param name="string"></param>
/// <param name="object"></param>
/// <param name="requestInfo"></param>
/// <param name="params"></param>
/// <returns></returns>
public static IDictionary<string, object> PrepareDoubleBracesParameters(this string @string, object @object = null, RequestInfo requestInfo = null, ExpandoObject @params = null)
=> @string?.PrepareDoubleBracesParameters(@object is IBusinessEntity bizObject ? bizObject.ToExpandoObject() : @object?.ToExpandoObject(), requestInfo?.AsExpandoObject, @params) ?? new Dictionary<string, object>();
/// <summary>
/// Gets the time quater
/// </summary>
/// <param name="time"></param>
/// <param name="getHighValue"></param>
/// <returns></returns>
public static DateTime GetTimeQuarter(this DateTime time, bool getHighValue = true)
=> time.Minute <= 15
? getHighValue ? DateTime.Parse($"{time:yyyy/MM/dd HH}:15:00") : DateTime.Parse($"{time:yyyy/MM/dd HH}:00:00")
: time.Minute <= 30
? getHighValue ? DateTime.Parse($"{time:yyyy/MM/dd HH}:30:00") : DateTime.Parse($"{time:yyyy/MM/dd HH}:16:00")
: time.Minute <= 45
? getHighValue ? DateTime.Parse($"{time:yyyy/MM/dd HH}:45:00") : DateTime.Parse($"{time:yyyy/MM/dd HH}:31:00")
: getHighValue ? DateTime.Parse($"{time:yyyy/MM/dd HH}:59:59") : DateTime.Parse($"{time:yyyy/MM/dd HH}:46:00");
#endregion