Add "involved" optional query param to API call GET /address/proxying, for convenience

This commit is contained in:
catbref 2019-06-03 09:51:13 +01:00
parent e42c3e60bc
commit a2cc4983a0
4 changed files with 31 additions and 13 deletions

View File

@ -305,6 +305,7 @@ public class AddressesResource {
@ApiErrors({ApiError.INVALID_ADDRESS, ApiError.INVALID_CRITERIA, ApiError.REPOSITORY_ISSUE})
public List<ProxyForgerData> getProxying(@QueryParam("proxiedFor") List<String> recipients,
@QueryParam("proxiedBy") List<String> forgers,
@QueryParam("involving") List<String> addresses,
@Parameter(
ref = "limit"
) @QueryParam("limit") Integer limit, @Parameter(
@ -313,7 +314,7 @@ public class AddressesResource {
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
try (final Repository repository = RepositoryManager.getRepository()) {
return repository.getAccountRepository().findProxyAccounts(recipients, forgers, limit, offset, reverse);
return repository.getAccountRepository().findProxyAccounts(recipients, forgers, addresses, limit, offset, reverse);
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}

View File

@ -91,7 +91,7 @@ public interface AccountRepository {
public boolean isProxyPublicKey(byte[] publicKey) throws DataException;
public List<ProxyForgerData> findProxyAccounts(List<String> recipients, List<String> forgers, Integer limit, Integer offset, Boolean reverse) throws DataException;
public List<ProxyForgerData> findProxyAccounts(List<String> recipients, List<String> forgers, List<String> involvedAddresses, Integer limit, Integer offset, Boolean reverse) throws DataException;
public void save(ProxyForgerData proxyForgerData) throws DataException;

View File

@ -17,6 +17,7 @@ import org.qora.repository.AccountRepository;
import org.qora.repository.DataException;
import static org.qora.repository.hsqldb.HSQLDBRepository.nPlaceholders;
import static org.qora.repository.hsqldb.HSQLDBRepository.nValuesPlaceholders;
public class HSQLDBAccountRepository implements AccountRepository {
@ -362,24 +363,35 @@ public class HSQLDBAccountRepository implements AccountRepository {
}
@Override
public List<ProxyForgerData> findProxyAccounts(List<String> recipients, List<String> forgers, Integer limit, Integer offset, Boolean reverse) throws DataException {
String sql = "SELECT forger, recipient, share, proxy_public_key FROM ProxyForgers ";
public List<ProxyForgerData> findProxyAccounts(List<String> recipients, List<String> forgers, List<String> involvedAddresses,
Integer limit, Integer offset, Boolean reverse) throws DataException {
String sql = "SELECT DISTINCT forger, recipient, share, proxy_public_key FROM ProxyForgers ";
List<Object> args = new ArrayList<>();
if (!forgers.isEmpty()) {
sql += "JOIN Accounts ON Accounts.public_key = ProxyForgers.forger "
+ "WHERE Accounts.account IN (" + nPlaceholders(forgers.size()) + ") ";
args.addAll(forgers);
}
final boolean hasRecipients = recipients != null && !recipients.isEmpty();
final boolean hasForgers = forgers != null && !forgers.isEmpty();
final boolean hasInvolved = involvedAddresses != null && !involvedAddresses.isEmpty();
if (!recipients.isEmpty()) {
sql += forgers.isEmpty() ? "WHERE " : "AND ";
sql += "recipient IN (" + nPlaceholders(recipients.size()) + ") ";
if (hasForgers || hasInvolved)
sql += "JOIN Accounts ON Accounts.public_key = ProxyForgers.forger ";
if (hasRecipients) {
sql += "JOIN (VALUES " + nValuesPlaceholders(recipients.size()) + ") AS Recipients (address) ON ProxyForgers.recipient = Recipients.address ";
args.addAll(recipients);
}
sql += "ORDER BY recipient, share";
if (hasForgers) {
sql += "JOIN (VALUES " + nValuesPlaceholders(forgers.size()) + ") AS Forgers (address) ON Accounts.account = Forgers.address ";
args.addAll(forgers);
}
if (hasInvolved) {
sql += "JOIN (VALUES " + nValuesPlaceholders(involvedAddresses.size()) + ") AS Involved (address) ON Involved.address IN (ProxyForgers.recipient, Accounts.account) ";
args.addAll(involvedAddresses);
}
sql += "ORDER BY recipient, share";
if (reverse != null && reverse)
sql += " DESC";

View File

@ -534,4 +534,9 @@ public class HSQLDBRepository implements Repository {
return String.join(", ", Collections.nCopies(n, "?"));
}
/** Convenience method to return n comma-separated, bracketed, placeholders as a string. */
public static String nValuesPlaceholders(int n) {
return String.join(", ", Collections.nCopies(n, "(?)"));
}
}