-
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.
Extend unstake request API model with couple views, close #173
- Loading branch information
Showing
8 changed files
with
248 additions
and
10 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,36 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace Tzkt.Api | ||
{ | ||
public class UnstakeRequestStatusBinder : IModelBinder | ||
{ | ||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
var model = bindingContext.ModelName; | ||
var hasValue = false; | ||
|
||
if (!bindingContext.TryGetUnstakeRequestStatus($"{model}", ref hasValue, out var value)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetUnstakeRequestStatus($"{model}.eq", ref hasValue, out var eq)) | ||
return Task.CompletedTask; | ||
|
||
if (!bindingContext.TryGetUnstakeRequestStatus($"{model}.ne", ref hasValue, out var ne)) | ||
return Task.CompletedTask; | ||
|
||
if (!hasValue) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Success(null); | ||
return Task.CompletedTask; | ||
} | ||
|
||
bindingContext.Result = ModelBindingResult.Success(new UnstakeRequestStatusParameter | ||
{ | ||
Eq = value ?? eq, | ||
Ne = ne, | ||
}); | ||
|
||
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
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,45 @@ | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NJsonSchema.Annotations; | ||
|
||
namespace Tzkt.Api | ||
{ | ||
[ModelBinder(BinderType = typeof(UnstakeRequestStatusBinder))] | ||
[JsonSchemaExtensionData("x-tzkt-extension", "query-parameter")] | ||
[JsonSchemaExtensionData("x-tzkt-query-parameter", "pending,finalizable,finalized")] | ||
public class UnstakeRequestStatusParameter : INormalizable | ||
{ | ||
/// <summary> | ||
/// **Equal** filter mode (`.eq` suffix can be omitted, i.e. `?param=...` is the same as `?param.eq=...`). \ | ||
/// Specify an unstake request status to get items where the specified field is equal to the specified value. | ||
/// | ||
/// Example: `?status=pending`. | ||
/// </summary> | ||
public string Eq { get; set; } | ||
|
||
/// <summary> | ||
/// **Not equal** filter mode. \ | ||
/// Specify an unstake request status to get items where the specified field is not equal to the specified value. | ||
/// | ||
/// Example: `?status.ne=finalized`. | ||
/// </summary> | ||
public string Ne { 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}&"); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Tzkt.Api | ||
{ | ||
static class UnstakeRequestStatuses | ||
{ | ||
public const string Pending = "pending"; | ||
public const string Finalizable = "finalizable"; | ||
public const string Finalized = "finalized"; | ||
|
||
public static bool TryParse(string value, out string res) | ||
{ | ||
res = value switch | ||
{ | ||
Pending => Pending, | ||
Finalizable => Finalizable, | ||
Finalized => Finalized, | ||
_ => null | ||
}; | ||
return res != null; | ||
} | ||
|
||
public static string ToString(int cycle, long remainingAmount, int unfrozenCycle) | ||
{ | ||
return cycle > unfrozenCycle ? Pending : remainingAmount != 0 ? Finalizable : Finalized; | ||
} | ||
} | ||
} |
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