diff --git a/src/main/java/org/qora/api/resource/AssetsResource.java b/src/main/java/org/qora/api/resource/AssetsResource.java index cf71cdd4..ec266ff4 100644 --- a/src/main/java/org/qora/api/resource/AssetsResource.java +++ b/src/main/java/org/qora/api/resource/AssetsResource.java @@ -45,6 +45,7 @@ import org.qora.data.transaction.IssueAssetTransactionData; import org.qora.data.transaction.TransactionData; import org.qora.data.transaction.TransferAssetTransactionData; import org.qora.data.transaction.UpdateAssetTransactionData; +import org.qora.repository.AccountRepository.BalanceOrdering; import org.qora.repository.DataException; import org.qora.repository.Repository; import org.qora.repository.RepositoryManager; @@ -164,13 +165,11 @@ public class AssetsResource { @ApiErrors({ ApiError.INVALID_ADDRESS, ApiError.INVALID_CRITERIA, ApiError.INVALID_ASSET_ID, ApiError.REPOSITORY_ISSUE }) - public List getAssetBalances(@QueryParam("address") List addresses, @QueryParam("assetid") List assetIds, @Parameter( - ref = "limit" - ) @QueryParam("limit") Integer limit, @Parameter( - ref = "offset" - ) @QueryParam("offset") Integer offset, @Parameter( - ref = "reverse" - ) @QueryParam("reverse") Boolean reverse) { + public List getAssetBalances(@QueryParam("address") List addresses, @QueryParam("assetid") List assetIds, + @Parameter(required = true) @QueryParam("ordering") BalanceOrdering balanceOrdering, + @Parameter( ref = "limit" ) @QueryParam("limit") Integer limit, + @Parameter( ref = "offset" ) @QueryParam("offset") Integer offset, + @Parameter( ref = "reverse" ) @QueryParam("reverse") Boolean reverse) { if (addresses.isEmpty() && assetIds.isEmpty()) throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); @@ -183,7 +182,7 @@ public class AssetsResource { if (!repository.getAssetRepository().assetExists(assetId)) 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) { throw e; } catch (DataException e) { diff --git a/src/main/java/org/qora/repository/AccountRepository.java b/src/main/java/org/qora/repository/AccountRepository.java index 270df953..fcbceff4 100644 --- a/src/main/java/org/qora/repository/AccountRepository.java +++ b/src/main/java/org/qora/repository/AccountRepository.java @@ -45,7 +45,13 @@ public interface AccountRepository { public AccountBalanceData getBalance(String address, long assetId) throws DataException; - public List getAssetBalances(List addresses, List assetIds, Integer limit, Integer offset, Boolean reverse) throws DataException; + public enum BalanceOrdering { + ASSET_BALANCE_ACCOUNT, + ACCOUNT_ASSET, + ASSET_ACCOUNT + } + + public List getAssetBalances(List addresses, List assetIds, BalanceOrdering balanceOrdering, Integer limit, Integer offset, Boolean reverse) throws DataException; public void save(AccountBalanceData accountBalanceData) throws DataException; diff --git a/src/main/java/org/qora/repository/hsqldb/HSQLDBAccountRepository.java b/src/main/java/org/qora/repository/hsqldb/HSQLDBAccountRepository.java index d2646b30..5e933fb0 100644 --- a/src/main/java/org/qora/repository/hsqldb/HSQLDBAccountRepository.java +++ b/src/main/java/org/qora/repository/hsqldb/HSQLDBAccountRepository.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -143,7 +144,7 @@ public class HSQLDBAccountRepository implements AccountRepository { } @Override - public List getAssetBalances(List addresses, List assetIds, Integer limit, Integer offset, Boolean reverse) + public List getAssetBalances(List addresses, List assetIds, BalanceOrdering balanceOrdering, Integer limit, Integer offset, Boolean reverse) throws DataException { 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 += "ORDER BY account"; - if (reverse != null && reverse) - sql += " DESC"; + String[] orderingColumns; + switch (balanceOrdering) { + 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) - 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);