Skip to content

Commit

Permalink
Add more filters to /voting/periods endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Mar 17, 2024
1 parent 270a493 commit 3c03f63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
21 changes: 10 additions & 11 deletions Tzkt.Api/Controllers/VotingController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;

using Tzkt.Api.Models;
using Tzkt.Api.Repositories;
using Tzkt.Api.Services.Cache;
Expand Down Expand Up @@ -112,13 +107,17 @@ public Task<Proposal> GetProposalByHash([Required][ProtocolHash] string hash)
/// <remarks>
/// Returns a list of voting periods.
/// </remarks>
/// <param name="firstLevel">Filter by level of the first block of the period.</param>
/// <param name="lastLevel">Filter by level of the last block of the period.</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 voting periods by specified field. Supported fields: `id` (default).</param>
/// <param name="offset">Specifies which or how many items should be skipped</param>
/// <param name="limit">Maximum number of items to return</param>
/// <returns></returns>
[HttpGet("periods")]
public async Task<ActionResult<IEnumerable<VotingPeriod>>> GetPeriods(
Int32Parameter firstLevel,
Int32Parameter lastLevel,
SelectParameter select,
SortParameter sort,
OffsetParameter offset,
Expand All @@ -130,25 +129,25 @@ public async Task<ActionResult<IEnumerable<VotingPeriod>>> GetPeriods(
#endregion

if (select == null)
return Ok(await Voting.GetPeriods(sort, offset, limit));
return Ok(await Voting.GetPeriods(firstLevel, lastLevel, sort, offset, limit));

if (select.Values != null)
{
if (select.Values.Length == 1)
return Ok(await Voting.GetPeriods(sort, offset, limit, select.Values[0]));
return Ok(await Voting.GetPeriods(firstLevel, lastLevel, sort, offset, limit, select.Values[0]));
else
return Ok(await Voting.GetPeriods(sort, offset, limit, select.Values));
return Ok(await Voting.GetPeriods(firstLevel, lastLevel, sort, offset, limit, select.Values));
}
else
{
if (select.Fields.Length == 1)
return Ok(await Voting.GetPeriods(sort, offset, limit, select.Fields[0]));
return Ok(await Voting.GetPeriods(firstLevel, lastLevel, sort, offset, limit, select.Fields[0]));
else
{
return Ok(new SelectionResponse
{
Cols = select.Fields,
Rows = await Voting.GetPeriods(sort, offset, limit, select.Fields)
Rows = await Voting.GetPeriods(firstLevel, lastLevel, sort, offset, limit, select.Fields)
});
}
}
Expand Down
20 changes: 10 additions & 10 deletions Tzkt.Api/Repositories/VotingRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Dapper;

using Dapper;
using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;

Expand Down Expand Up @@ -311,9 +305,11 @@ public async Task<VotingPeriod> GetPeriod(int index)
};
}

public async Task<IEnumerable<VotingPeriod>> GetPeriods(SortParameter sort, OffsetParameter offset, int limit)
public async Task<IEnumerable<VotingPeriod>> GetPeriods(Int32Parameter firstLevel, Int32Parameter lastLevel, SortParameter sort, OffsetParameter offset, int limit)
{
var sql = new SqlBuilder(@"SELECT * FROM ""VotingPeriods""")
.Filter("FirstLevel", firstLevel)
.Filter("LastLevel", lastLevel)
.Take(sort, offset, limit, x => ("Id", "Id"));

using var db = GetConnection();
Expand Down Expand Up @@ -347,7 +343,7 @@ public async Task<IEnumerable<VotingPeriod>> GetPeriods(SortParameter sort, Offs
});
}

public async Task<object[][]> GetPeriods(SortParameter sort, OffsetParameter offset, int limit, string[] fields)
public async Task<object[][]> GetPeriods(Int32Parameter firstLevel, Int32Parameter lastLevel, SortParameter sort, OffsetParameter offset, int limit, string[] fields)
{
var columns = new HashSet<string>(fields.Length);

Expand Down Expand Up @@ -385,6 +381,8 @@ public async Task<object[][]> GetPeriods(SortParameter sort, OffsetParameter off
return Array.Empty<object[]>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""VotingPeriods""")
.Filter("FirstLevel", firstLevel)
.Filter("LastLevel", lastLevel)
.Take(sort, offset, limit, x => ("Id", "Id"));

using var db = GetConnection();
Expand Down Expand Up @@ -496,7 +494,7 @@ public async Task<object[][]> GetPeriods(SortParameter sort, OffsetParameter off
return result;
}

public async Task<object[]> GetPeriods(SortParameter sort, OffsetParameter offset, int limit, string field)
public async Task<object[]> GetPeriods(Int32Parameter firstLevel, Int32Parameter lastLevel, SortParameter sort, OffsetParameter offset, int limit, string field)
{
var columns = new HashSet<string>(1);

Expand Down Expand Up @@ -531,6 +529,8 @@ public async Task<object[]> GetPeriods(SortParameter sort, OffsetParameter offse
return Array.Empty<object>();

var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""VotingPeriods""")
.Filter("FirstLevel", firstLevel)
.Filter("LastLevel", lastLevel)
.Take(sort, offset, limit, x => ("Id", "Id"));

using var db = GetConnection();
Expand Down

0 comments on commit 3c03f63

Please sign in to comment.