-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more filters for /voting endpoints
- Loading branch information
Showing
10 changed files
with
306 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace Tzkt.Api | ||
{ | ||
public class EpochStatusBinder : IModelBinder | ||
{ | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
var model = bindingContext.ModelName; | ||
var hasValue = false; | ||
|
||
if (!bindingContext.TryGetEpochStatus($"{model}", ref hasValue, out var value)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetEpochStatus($"{model}.eq", ref hasValue, out var eq)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetEpochStatus($"{model}.ne", ref hasValue, out var ne)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetEpochStatusList($"{model}.in", ref hasValue, out var @in)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetEpochStatusList($"{model}.ni", ref hasValue, out var ni)) | ||
return Task.CompletedTask; | ||
|
||
if (!hasValue) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Success(null); | ||
return Task.CompletedTask; | ||
} | ||
|
||
bindingContext.Result = ModelBindingResult.Success(new EpochStatusParameter | ||
{ | ||
Eq = value ?? eq, | ||
Ne = ne, | ||
In = @in, | ||
Ni = ni | ||
}); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NJsonSchema.Annotations; | ||
|
||
namespace Tzkt.Api | ||
{ | ||
[ModelBinder(BinderType = typeof(EpochStatusBinder))] | ||
[JsonSchemaExtensionData("x-tzkt-extension", "query-parameter")] | ||
[JsonSchemaExtensionData("x-tzkt-query-parameter", "no_proposals,voting,completed,failed")] | ||
public class EpochStatusParameter : INormalizable | ||
{ | ||
/// <summary> | ||
/// **Equal** filter mode (optional, i.e. `param.eq=123` is the same as `param=123`). \ | ||
/// Specify an epoch status to get items where the specified field is equal to the specified value. | ||
/// | ||
/// Example: `?status=completed`. | ||
/// </summary> | ||
public string Eq { get; set; } | ||
|
||
/// <summary> | ||
/// **Not equal** filter mode. \ | ||
/// Specify an epoch status to get items where the specified field is not equal to the specified value. | ||
/// | ||
/// Example: `?status.ne=no_proposals`. | ||
/// </summary> | ||
public string Ne { get; set; } | ||
|
||
/// <summary> | ||
/// **In list** (any of) filter mode. \ | ||
/// Specify a comma-separated list of epoch statuses to get items where the specified field is equal to one of the specified values. | ||
/// | ||
/// Example: `?status.in=completed,failed`. | ||
/// </summary> | ||
public List<string> In { get; set; } | ||
|
||
/// <summary> | ||
/// **Not in list** (none of) filter mode. \ | ||
/// Specify a comma-separated list of epoch statuses to get items where the specified field is not equal to all the specified values. | ||
/// | ||
/// Example: `?status.ni=completed,failed`. | ||
/// </summary> | ||
public List<string> Ni { get; set; } | ||
|
||
public string Normalize(string name) | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
if (Eq != null) | ||
{ | ||
sb.Append($"{name}.eq={Eq}&"); | ||
} | ||
|
||
if (Ne != null) | ||
{ | ||
sb.Append($"{name}.ne={Ne}&"); | ||
} | ||
|
||
if (In?.Count > 0) | ||
{ | ||
sb.Append($"{name}.in={string.Join(",", In.OrderBy(x => x))}&"); | ||
} | ||
|
||
if (Ni?.Count > 0) | ||
{ | ||
sb.Append($"{name}.ni={string.Join(",", Ni.OrderBy(x => x))}&"); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.