forked from Qortal/qortal
Add variety of results ordering options to API GET /assets/balances
This commit is contained in:
parent
0fd8ee3547
commit
a4d28d52cc
@ -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<AccountBalanceData> getAssetBalances(@QueryParam("address") List<String> addresses, @QueryParam("assetid") List<Long> assetIds, @Parameter(
|
||||
ref = "limit"
|
||||
) @QueryParam("limit") Integer limit, @Parameter(
|
||||
ref = "offset"
|
||||
) @QueryParam("offset") Integer offset, @Parameter(
|
||||
ref = "reverse"
|
||||
) @QueryParam("reverse") Boolean reverse) {
|
||||
public List<AccountBalanceData> getAssetBalances(@QueryParam("address") List<String> addresses, @QueryParam("assetid") List<Long> 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) {
|
||||
|
@ -45,7 +45,13 @@ public interface AccountRepository {
|
||||
|
||||
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;
|
||||
|
||||
|
@ -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<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 {
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user