Skip to content

Commit

Permalink
Fix TimestampParameter binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Apr 15, 2024
1 parent 22a9667 commit 435801d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
31 changes: 25 additions & 6 deletions Tzkt.Api/Parameters/Binders/TimestampBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,36 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
return Task.CompletedTask;
}

bindingContext.Result = ModelBindingResult.Success(new TimestampParameter
var param = new TimestampParameter
{
Eq = (value ?? eq) == null ? null : Time.FindLevel((DateTime)(value ?? eq), SearchMode.Exact),
Ne = ne == null ? null : Time.FindLevel((DateTime)ne, SearchMode.Exact),
Gt = gt == null ? null : Time.FindLevel((DateTime)gt, SearchMode.ExactOrLower),
Ge = ge == null ? null : Time.FindLevel((DateTime)ge, SearchMode.ExactOrHigher),
Lt = lt == null ? null : Time.FindLevel((DateTime)lt, SearchMode.ExactOrHigher),
Le = le == null ? null : Time.FindLevel((DateTime)le, SearchMode.ExactOrLower),
In = @in?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
Ni = ni?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
});
};

if (gt != null)
{
var level = Time.FindLevel((DateTime)gt, SearchMode.ExactOrLower);
param.Gt = level != -1 ? level : null;
}
if (ge != null)
{
var level = Time.FindLevel((DateTime)ge, SearchMode.ExactOrHigher);
param.Ge = level != -1 ? level : int.MaxValue;
}
if (lt != null)
{
var level = Time.FindLevel((DateTime)lt, SearchMode.ExactOrHigher);
param.Lt = level != -1 ? level : null;
}
if (le != null)
{
var level = Time.FindLevel((DateTime)le, SearchMode.ExactOrLower);
param.Le = level != -1 ? level : int.MinValue;
}

bindingContext.Result = ModelBindingResult.Success(param);

return Task.CompletedTask;
}
Expand Down
23 changes: 0 additions & 23 deletions Tzkt.Api/Parameters/TimestampParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,6 @@ public class TimestampParameter : INormalizable
[JsonSchemaType(typeof(List<DateTime>))]
public List<int> Ni { get; set; }

#region static
public static Int32Parameter FromDateTimeParameter(DateTimeParameter timestamp, TimeCache time)
{
if (timestamp == null) return null;

var res = new Int32Parameter();

if (timestamp.Eq != null)
res.Eq = time.FindLevel((DateTime)timestamp.Eq, SearchMode.Exact);

if (timestamp.Ne != null)
res.Ne = time.FindLevel((DateTime)timestamp.Ne, SearchMode.Exact);

if (timestamp.Gt != null)
res.Gt = time.FindLevel((DateTime)timestamp.Gt, SearchMode.ExactOrLower);

if (timestamp.Ge != null)
res.Ge = time.FindLevel((DateTime)timestamp.Ge, SearchMode.ExactOrLower);

return res;
}
#endregion

public string Normalize(string name)
{
var sb = new StringBuilder();
Expand Down
27 changes: 22 additions & 5 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2165,13 +2165,30 @@ public async Task<IEnumerable<Operation>> GetOperations(
{
Eq = timestamp.Eq == null ? null : Time.FindLevel((DateTime)timestamp.Eq, SearchMode.Exact),
Ne = timestamp.Ne == null ? null : Time.FindLevel((DateTime)timestamp.Ne, SearchMode.Exact),
Gt = timestamp.Gt == null ? null : Time.FindLevel((DateTime)timestamp.Gt, SearchMode.ExactOrLower),
Ge = timestamp.Ge == null ? null : Time.FindLevel((DateTime)timestamp.Ge, SearchMode.ExactOrHigher),
Lt = timestamp.Lt == null ? null : Time.FindLevel((DateTime)timestamp.Lt, SearchMode.ExactOrHigher),
Le = timestamp.Le == null ? null : Time.FindLevel((DateTime)timestamp.Le, SearchMode.ExactOrLower),
In = timestamp.In?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
Ni = timestamp.Ni?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList(),
Ni = timestamp.Ni?.Select(x => Time.FindLevel(x, SearchMode.Exact)).ToList()
};

if (timestamp.Gt != null)
{
var lvl = Time.FindLevel((DateTime)timestamp.Gt, SearchMode.ExactOrLower);
_timestamp.Gt = lvl != -1 ? lvl : null;
}
if (timestamp.Ge != null)
{
var lvl = Time.FindLevel((DateTime)timestamp.Ge, SearchMode.ExactOrHigher);
_timestamp.Ge = lvl != -1 ? lvl : int.MaxValue;
}
if (timestamp.Lt != null)
{
var lvl = Time.FindLevel((DateTime)timestamp.Lt, SearchMode.ExactOrHigher);
_timestamp.Lt = lvl != -1 ? lvl : null;
}
if (timestamp.Le != null)
{
var lvl = Time.FindLevel((DateTime)timestamp.Le, SearchMode.ExactOrLower);
_timestamp.Le = lvl != -1 ? lvl : int.MinValue;
}
}

switch (account)
Expand Down

0 comments on commit 435801d

Please sign in to comment.