Add variety of results ordering options to API GET /assets/balances

This commit is contained in:
catbref 2019-03-25 12:24:22 +00:00
parent 0fd8ee3547
commit a4d28d52cc
3 changed files with 36 additions and 15 deletions

View File

@ -45,6 +45,7 @@ import org.qora.data.transaction.IssueAssetTransactionData;
import org.qora.data.transaction.TransactionData; import org.qora.data.transaction.TransactionData;
import org.qora.data.transaction.TransferAssetTransactionData; import org.qora.data.transaction.TransferAssetTransactionData;
import org.qora.data.transaction.UpdateAssetTransactionData; import org.qora.data.transaction.UpdateAssetTransactionData;
import org.qora.repository.AccountRepository.BalanceOrdering;
import org.qora.repository.DataException; import org.qora.repository.DataException;
import org.qora.repository.Repository; import org.qora.repository.Repository;
import org.qora.repository.RepositoryManager; import org.qora.repository.RepositoryManager;
@ -164,13 +165,11 @@ public class AssetsResource {
@ApiErrors({ @ApiErrors({
ApiError.INVALID_ADDRESS, ApiError.INVALID_CRITERIA, ApiError.INVALID_ASSET_ID, ApiError.REPOSITORY_ISSUE ApiError.INVALID_ADDRESS, ApiError.INVALID_CRITERIA, ApiError.INVALID_ASSET_ID, ApiError.REPOSITORY_ISSUE
}) })
public List<AccountBalanceData> getAssetBalances(@QueryParam("address") List<String> addresses, @QueryParam("assetid") List<Long> assetIds, @Parameter( public List<AccountBalanceData> getAssetBalances(@QueryParam("address") List<String> addresses, @QueryParam("assetid") List<Long> assetIds,
ref = "limit" @Parameter(required = true) @QueryParam("ordering") BalanceOrdering balanceOrdering,
) @QueryParam("limit") Integer limit, @Parameter( @Parameter( ref = "limit" ) @QueryParam("limit") Integer limit,
ref = "offset" @Parameter( ref = "offset" ) @QueryParam("offset") Integer offset,
) @QueryParam("offset") Integer offset, @Parameter( @Parameter( ref = "reverse" ) @QueryParam("reverse") Boolean reverse) {
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
if (addresses.isEmpty() && assetIds.isEmpty()) if (addresses.isEmpty() && assetIds.isEmpty())
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
@ -183,7 +182,7 @@ public class AssetsResource {
if (!repository.getAssetRepository().assetExists(assetId)) if (!repository.getAssetRepository().assetExists(assetId))
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_ASSET_ID); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_ASSET_ID);
return repository.getAccountRepository().getAssetBalances(addresses, assetIds, limit, offset, reverse); return repository.getAccountRepository().getAssetBalances(addresses, assetIds, balanceOrdering, limit, offset, reverse);
} catch (ApiException e) { } catch (ApiException e) {
throw e; throw e;
} catch (DataException e) { } catch (DataException e) {

View File

@ -45,7 +45,13 @@ public interface AccountRepository {
public AccountBalanceData getBalance(String address, long assetId) throws DataException; public AccountBalanceData getBalance(String address, long assetId) throws DataException;
public List<AccountBalanceData> getAssetBalances(List<String> addresses, List<Long> assetIds, Integer limit, Integer offset, Boolean reverse) throws DataException; public enum BalanceOrdering {
ASSET_BALANCE_ACCOUNT,
ACCOUNT_ASSET,
ASSET_ACCOUNT
}
public List<AccountBalanceData> getAssetBalances(List<String> addresses, List<Long> assetIds, BalanceOrdering balanceOrdering, Integer limit, Integer offset, Boolean reverse) throws DataException;
public void save(AccountBalanceData accountBalanceData) throws DataException; public void save(AccountBalanceData accountBalanceData) throws DataException;

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -143,7 +144,7 @@ public class HSQLDBAccountRepository implements AccountRepository {
} }
@Override @Override
public List<AccountBalanceData> getAssetBalances(List<String> addresses, List<Long> assetIds, Integer limit, Integer offset, Boolean reverse) public List<AccountBalanceData> getAssetBalances(List<String> addresses, List<Long> assetIds, BalanceOrdering balanceOrdering, Integer limit, Integer offset, Boolean reverse)
throws DataException { throws DataException {
String sql = "SELECT account, asset_id, IFNULL(balance, 0), asset_name FROM "; String sql = "SELECT account, asset_id, IFNULL(balance, 0), asset_name FROM ";
@ -165,13 +166,28 @@ public class HSQLDBAccountRepository implements AccountRepository {
sql += "balance != 0 "; sql += "balance != 0 ";
} }
sql += "ORDER BY account"; String[] orderingColumns;
if (reverse != null && reverse) switch (balanceOrdering) {
sql += " DESC"; case ACCOUNT_ASSET:
orderingColumns = new String[] { "account", "asset_id" };
break;
case ASSET_ACCOUNT:
orderingColumns = new String[] { "asset_id", "account" };
break;
case ASSET_BALANCE_ACCOUNT:
orderingColumns = new String[] { "asset_id", "balance", "account" };
break;
default:
throw new DataException(String.format("Unsupported asset balance result ordering: %s", balanceOrdering.name()));
}
sql += ", asset_id";
if (reverse != null && reverse) if (reverse != null && reverse)
sql += " DESC"; orderingColumns = Arrays.stream(orderingColumns).map(column -> column + " DESC").toArray(size -> new String[size]);
sql += "ORDER BY " + String.join(", ", orderingColumns);
sql += HSQLDBRepository.limitOffsetSql(limit, offset); sql += HSQLDBRepository.limitOffsetSql(limit, offset);