Skip to content

Commit

Permalink
Add more account filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 15, 2020
1 parent 80baa4b commit 2f43640
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
28 changes: 19 additions & 9 deletions Tzkt.Api/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public AccountsController(AccountRepository accounts, BalanceHistoryRepository h
/// Returns a list of accounts.
/// </remarks>
/// <param name="type">Filters accounts by type (`user`, `delegate`, `contract`).</param>
/// <param name="kind">Filter accounts by contract kind (`delegator_contract` or `smart_contract`)</param>
/// <param name="kind">Filters accounts by contract kind (`delegator_contract` or `smart_contract`)</param>
/// <param name="balance">Filters accounts by balance</param>
/// <param name="staked">Filters accounts by participation in staking</param>
/// <param name="select">Specify comma-separated list of fields to include into response or leave it undefined to return full object. If you select single field, response will be an array of values in both `.fields` and `.values` modes.</param>
/// <param name="sort">Sorts delegators by specified field. Supported fields: `id` (default), `balance`, `firstActivity`, `lastActivity`, `numTransactions`, `numContracts`.</param>
/// <param name="offset">Specifies which or how many items should be skipped</param>
Expand All @@ -48,6 +50,8 @@ public AccountsController(AccountRepository accounts, BalanceHistoryRepository h
public async Task<ActionResult<IEnumerable<Account>>> Get(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked,
SelectParameter select,
SortParameter sort,
OffsetParameter offset,
Expand All @@ -64,25 +68,25 @@ public AccountsController(AccountRepository accounts, BalanceHistoryRepository h
#endregion

if (select == null)
return Ok(await Accounts.Get(type, kind, sort, offset, limit));
return Ok(await Accounts.Get(type, kind, balance, staked, sort, offset, limit));

if (select.Values != null)
{
if (select.Values.Length == 1)
return Ok(await Accounts.Get(type, kind, sort, offset, limit, select.Values[0]));
return Ok(await Accounts.Get(type, kind, balance, staked, sort, offset, limit, select.Values[0]));
else
return Ok(await Accounts.Get(type, kind, sort, offset, limit, select.Values));
return Ok(await Accounts.Get(type, kind, balance, staked, sort, offset, limit, select.Values));
}
else
{
if (select.Fields.Length == 1)
return Ok(await Accounts.Get(type, kind, sort, offset, limit, select.Fields[0]));
return Ok(await Accounts.Get(type, kind, balance, staked, sort, offset, limit, select.Fields[0]));
else
{
return Ok(new SelectionResponse
{
Cols = select.Fields,
Rows = await Accounts.Get(type, kind, sort, offset, limit, select.Fields)
Rows = await Accounts.Get(type, kind, balance, staked, sort, offset, limit, select.Fields)
});
}
}
Expand All @@ -95,10 +99,16 @@ public AccountsController(AccountRepository accounts, BalanceHistoryRepository h
/// Returns a number of accounts.
/// </remarks>
/// <param name="type">Filters accounts by type (`user`, `delegate`, `contract`).</param>
/// <param name="kind">Filter accounts by contract kind (`delegator_contract` or `smart_contract`)</param>
/// <param name="kind">Filters accounts by contract kind (`delegator_contract` or `smart_contract`)</param>
/// <param name="balance">Filters accounts by balance</param>
/// <param name="staked">Filters accounts by participation in staking</param>
/// <returns></returns>
[HttpGet("count")]
public Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind)
public Task<int> GetCount(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked)
{
#region optimize
if (type == null && kind == null)
Expand All @@ -108,7 +118,7 @@ public Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind)
type = new AccountTypeParameter { Eq = 2 };
#endregion

return Accounts.GetCount(type, kind);
return Accounts.GetCount(type, kind, balance, staked);
}

/// <summary>
Expand Down
18 changes: 16 additions & 2 deletions Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,13 @@ public async Task<Account> GetProfile(string address, HashSet<string> types, Sor
}

#region accounts
public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind)
public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter kind, Int64Parameter balance, BoolParameter staked)
{
var sql = new SqlBuilder(@"SELECT COUNT(*) FROM ""Accounts""")
.Filter("Type", type)
.Filter("Kind", kind);
.Filter("Kind", kind)
.Filter("Balance", balance)
.Filter("Staked", staked);

using var db = GetConnection();
return await db.QueryFirstAsync<int>(sql.Query, sql.Params);
Expand All @@ -354,13 +356,17 @@ public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter
public async Task<IEnumerable<Account>> Get(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked,
SortParameter sort,
OffsetParameter offset,
int limit)
{
var sql = new SqlBuilder(@"SELECT * FROM ""Accounts""")
.Filter("Type", type)
.Filter("Kind", kind)
.Filter("Balance", balance)
.Filter("Staked", staked)
.Take(sort, offset, limit, x => x switch
{
"balance" => ("Balance", "Balance"),
Expand Down Expand Up @@ -536,6 +542,8 @@ public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter
public async Task<object[][]> Get(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked,
SortParameter sort,
OffsetParameter offset,
int limit,
Expand Down Expand Up @@ -599,6 +607,8 @@ public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter
var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""Accounts""")
.Filter("Type", type)
.Filter("Kind", kind)
.Filter("Balance", balance)
.Filter("Staked", staked)
.Take(sort, offset, limit, x => x switch
{
"balance" => ("Balance", "Balance"),
Expand Down Expand Up @@ -868,6 +878,8 @@ public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter
public async Task<object[]> Get(
AccountTypeParameter type,
ContractKindParameter kind,
Int64Parameter balance,
BoolParameter staked,
SortParameter sort,
OffsetParameter offset,
int limit,
Expand Down Expand Up @@ -928,6 +940,8 @@ public async Task<int> GetCount(AccountTypeParameter type, ContractKindParameter
var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""Accounts""")
.Filter("Type", type)
.Filter("Kind", kind)
.Filter("Balance", balance)
.Filter("Staked", staked)
.Take(sort, offset, limit, x => x switch
{
"balance" => ("Balance", "Balance"),
Expand Down

0 comments on commit 2f43640

Please sign in to comment.