-
Notifications
You must be signed in to change notification settings - Fork 34
/
SortParameter.cs
54 lines (46 loc) 路 1.44 KB
/
SortParameter.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
锘縰sing Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;
namespace Tzkt.Api
{
[ModelBinder(BinderType = typeof(SortBinder))]
[JsonSchemaExtensionData("x-tzkt-extension", "query-parameter")]
public class SortParameter : INormalizable
{
/// <summary>
/// **Ascending** sort mode (optional, i.e. `sort.asc=id` is the same as `sort=id`). \
/// Specify a field name to sort by.
///
/// Example: `?sort=balance`.
/// </summary>
public string Asc { get; set; }
/// <summary>
/// **Descending** sort mode. \
/// Specify a field name to sort by descending.
///
/// Example: `?sort.desc=id`.
/// </summary>
public string Desc { get; set; }
public bool Validate(params string[] fields)
{
if (Asc != null)
{
for (int i = 0; i < fields.Length; i++)
if (fields[i] == Asc)
return true;
return false;
}
if (Desc != null)
{
for (int i = 0; i < fields.Length; i++)
if (fields[i] == Desc)
return true;
return false;
}
return true;
}
public string Normalize(string name)
{
return Asc != null ? $"sort.asc={Asc}&" : $"sort.desc={Desc}&";
}
}
}