Skip to content

Commit

Permalink
Add .in, .ni modes to contract kind and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Feb 26, 2021
1 parent d04bfd5 commit 9c26add
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
50 changes: 50 additions & 0 deletions Tzkt.Api/Extensions/ModelBindingContextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,56 @@ public static bool TryGetContractKind(this ModelBindingContext bindingContext, s
return true;
}

public static bool TryGetContractKindList(this ModelBindingContext bindingContext, string name, ref bool hasValue, out List<int> result)
{
result = null;
var valueObject = bindingContext.ValueProvider.GetValue(name);

if (valueObject != ValueProviderResult.None)
{
bindingContext.ModelState.SetModelValue(name, valueObject);
if (!string.IsNullOrEmpty(valueObject.FirstValue))
{
var rawValues = valueObject.FirstValue.Split(',', StringSplitOptions.RemoveEmptyEntries);

if (rawValues.Length == 0)
{
bindingContext.ModelState.TryAddModelError(name, "List should contain at least one item.");
return false;
}

hasValue = true;
result = new List<int>(rawValues.Length);

foreach (var rawValue in rawValues)
{
if (rawValue == ContractKinds.Asset)
{
hasValue = true;
result.Add(2);
}
else if (rawValue == ContractKinds.SmartContract)
{
hasValue = true;
result.Add(1);
}
else if (rawValue == ContractKinds.Delegator)
{
hasValue = true;
result.Add(0);
}
else
{
bindingContext.ModelState.TryAddModelError(name, "List contains invalid contract kind.");
return false;
}
}
}
}

return true;
}

public static bool TryGetVoterStatus(this ModelBindingContext bindingContext, string name, ref bool hasValue, out int? result)
{
result = null;
Expand Down
10 changes: 9 additions & 1 deletion Tzkt.Api/Parameters/Binders/ContractKindBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
if (!bindingContext.TryGetContractKind($"{model}.ne", ref hasValue, out var ne))
return Task.CompletedTask;

if (!bindingContext.TryGetContractKindList($"{model}.in", ref hasValue, out var @in))
return Task.CompletedTask;

if (!bindingContext.TryGetContractKindList($"{model}.ni", ref hasValue, out var ni))
return Task.CompletedTask;

if (!hasValue)
{
bindingContext.Result = ModelBindingResult.Success(null);
Expand All @@ -31,7 +37,9 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
bindingContext.Result = ModelBindingResult.Success(new ContractKindParameter
{
Eq = value ?? eq,
Ne = ne
Ne = ne,
In = @in,
Ni = ni
});

return Task.CompletedTask;
Expand Down
23 changes: 21 additions & 2 deletions Tzkt.Api/Parameters/ContractKindParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.Annotations;

namespace Tzkt.Api
Expand All @@ -19,9 +20,27 @@ public class ContractKindParameter
/// **Not equal** filter mode. \
/// Specify a contract kind to get items where the specified field is not equal to the specified value.
///
/// Example: `?type.ne=delegator_contract`.
/// Example: `?kind.ne=delegator_contract`.
/// </summary>
[JsonSchemaType(typeof(string))]
public int? Ne { get; set; }

/// <summary>
/// **In list** (any of) filter mode. \
/// Specify a comma-separated list of contract kinds to get items where the specified field is equal to one of the specified values.
///
/// Example: `?kind.in=smart_contract,asset`.
/// </summary>
[JsonSchemaType(typeof(List<string>))]
public List<int> In { get; set; }

/// <summary>
/// **Not in list** (none of) filter mode. \
/// Specify a comma-separated list of contract kinds to get items where the specified field is not equal to all the specified values.
///
/// Example: `?kind.ni=smart_contract,asset`.
/// </summary>
[JsonSchemaType(typeof(List<string>))]
public List<int> Ni { get; set; }
}
}
6 changes: 3 additions & 3 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3031,11 +3031,11 @@ IEnumerable<string> GetTzips(int? value)
if (value == null || value == 0) return null;
var res = new List<string>(1);

if (((int)value & (int)Data.Models.Tzip.FA2) > 0)
if (((int)value & (int)Data.Models.Tzip.FA2) == (int)Data.Models.Tzip.FA2)
res.Add("fa2");
else if (((int)value & (int)Data.Models.Tzip.FA12) > 0)
else if (((int)value & (int)Data.Models.Tzip.FA12) == (int)Data.Models.Tzip.FA12)
res.Add("fa12");
else if (((int)value & (int)Data.Models.Tzip.FA1) > 0)
else if (((int)value & (int)Data.Models.Tzip.FA1) == (int)Data.Models.Tzip.FA1)
res.Add("fa1");

return res;
Expand Down
8 changes: 4 additions & 4 deletions Tzkt.Api/Repositories/OperationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4409,13 +4409,13 @@ public async Task<IEnumerable<TransactionOperation>> GetTransactions(
res = res.Where(x => x.Parameters != null);
if (parameters.As != null)
{
var pattern = $"^{parameters.As.Replace("%", ".*")}$";
res = res.Where(x => System.Text.RegularExpressions.Regex.IsMatch(x.Parameters, pattern));
var pattern = $"^{parameters.As.Replace("%", ".*").Replace("[", "\\[").Replace("]", "\\]").Replace("{", "\\{").Replace("}", "\\}")}$";
res = res.Where(x => x.Parameters != null && System.Text.RegularExpressions.Regex.IsMatch(x.Parameters, pattern));
}
if (parameters.Un != null)
{
var pattern = $"^{parameters.Un.Replace("%", ".*")}$";
res = res.Where(x => !System.Text.RegularExpressions.Regex.IsMatch(x.Parameters, pattern));
var pattern = $"^{parameters.Un.Replace("%", ".*").Replace("[", "\\[").Replace("]", "\\]").Replace("{", "\\{").Replace("}", "\\}")}$";
res = res.Where(x => x.Parameters != null && !System.Text.RegularExpressions.Regex.IsMatch(x.Parameters, pattern));
}

if (sort?.Asc != null)
Expand Down
12 changes: 12 additions & 0 deletions Tzkt.Api/Utils/SqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public SqlBuilder Filter(string column, ContractKindParameter kind)
if (kind.Ne != null)
AppendFilter($@"""{column}"" != {kind.Ne}");

if (kind.In != null)
{
AppendFilter($@"""{column}"" = ANY (@p{Counter})");
Params.Add($"p{Counter++}", kind.In);
}

if (kind.Ni != null && kind.Ni.Count > 0)
{
AppendFilter($@"NOT (""{column}"" = ANY (@p{Counter}))");
Params.Add($"p{Counter++}", kind.Ni);
}

return this;
}

Expand Down

0 comments on commit 9c26add

Please sign in to comment.