From 20893879ca6599926788674619e53fb682fed8a2 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 14 Apr 2023 17:17:05 +0100 Subject: [PATCH] Allow multiple name parameters to optionally be included in GET /arbitrary/resources/search Also updated SEARCH_QDN_RESOURCES action, to allow multiple names to be optionally specified via the "names" parameter. --- Q-Apps.md | 18 ++++++++++++++++++ .../qortal/api/resource/ArbitraryResource.java | 4 ++-- .../qortal/repository/ArbitraryRepository.java | 2 +- .../hsqldb/HSQLDBArbitraryRepository.java | 18 ++++++++++++------ src/main/resources/q-apps/q-apps.js | 1 + 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Q-Apps.md b/Q-Apps.md index 3cc48b26..860a2a3f 100644 --- a/Q-Apps.md +++ b/Q-Apps.md @@ -323,6 +323,24 @@ let res = await qortalRequest({ }); ``` +### Search QDN resources (multiple names) +``` +let res = await qortalRequest({ + action: "SEARCH_QDN_RESOURCES", + service: "THUMBNAIL", + query: "search query goes here", // Optional - searches both "identifier" and "name" fields + identifier: "search query goes here", // Optional - searches only the "identifier" field + names: ["QortalDemo", "crowetic", "AlphaX"], // Optional - searches only the "name" field for any of the supplied names + prefix: false, // Optional - if true, only the beginning of fields are matched in all of the above filters + default: false, // Optional - if true, only resources without identifiers are returned + includeStatus: false, // Optional - will take time to respond, so only request if necessary + includeMetadata: false, // Optional - will take time to respond, so only request if necessary + limit: 100, + offset: 0, + reverse: true +}); +``` + ### Fetch QDN single file resource ``` let res = await qortalRequest({ diff --git a/src/main/java/org/qortal/api/resource/ArbitraryResource.java b/src/main/java/org/qortal/api/resource/ArbitraryResource.java index 5b839dce..7adf1cec 100644 --- a/src/main/java/org/qortal/api/resource/ArbitraryResource.java +++ b/src/main/java/org/qortal/api/resource/ArbitraryResource.java @@ -171,7 +171,7 @@ public class ArbitraryResource { @QueryParam("service") Service service, @Parameter(description = "Query (searches both name and identifier fields)") @QueryParam("query") String query, @Parameter(description = "Identifier (searches identifier field only)") @QueryParam("identifier") String identifier, - @Parameter(description = "Name (searches name field only)") @QueryParam("name") String name, + @Parameter(description = "Name (searches name field only)") @QueryParam("name") List names, @Parameter(description = "Prefix only (if true, only the beginning of fields are matched)") @QueryParam("prefix") Boolean prefixOnly, @Parameter(description = "Default resources (without identifiers) only") @QueryParam("default") Boolean defaultResource, @Parameter(ref = "limit") @QueryParam("limit") Integer limit, @@ -186,7 +186,7 @@ public class ArbitraryResource { boolean usePrefixOnly = Boolean.TRUE.equals(prefixOnly); List resources = repository.getArbitraryRepository() - .searchArbitraryResources(service, query, identifier, name, usePrefixOnly, defaultRes, limit, offset, reverse); + .searchArbitraryResources(service, query, identifier, names, usePrefixOnly, defaultRes, limit, offset, reverse); if (resources == null) { return new ArrayList<>(); diff --git a/src/main/java/org/qortal/repository/ArbitraryRepository.java b/src/main/java/org/qortal/repository/ArbitraryRepository.java index 5581bc59..cd1b582b 100644 --- a/src/main/java/org/qortal/repository/ArbitraryRepository.java +++ b/src/main/java/org/qortal/repository/ArbitraryRepository.java @@ -26,7 +26,7 @@ public interface ArbitraryRepository { public List getArbitraryResources(Service service, String identifier, List names, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException; - public List searchArbitraryResources(Service service, String query, String identifier, String name, boolean prefixOnly, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException; + public List searchArbitraryResources(Service service, String query, String identifier, List names, boolean prefixOnly, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException; public List getArbitraryResourceCreatorNames(Service service, String identifier, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException; diff --git a/src/main/java/org/qortal/repository/hsqldb/HSQLDBArbitraryRepository.java b/src/main/java/org/qortal/repository/hsqldb/HSQLDBArbitraryRepository.java index bbd7de9a..443f7c6b 100644 --- a/src/main/java/org/qortal/repository/hsqldb/HSQLDBArbitraryRepository.java +++ b/src/main/java/org/qortal/repository/hsqldb/HSQLDBArbitraryRepository.java @@ -360,7 +360,7 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository { } @Override - public List searchArbitraryResources(Service service, String query, String identifier, String name, boolean prefixOnly, + public List searchArbitraryResources(Service service, String query, String identifier, List names, boolean prefixOnly, boolean defaultResource, Integer limit, Integer offset, Boolean reverse) throws DataException { StringBuilder sql = new StringBuilder(512); List bindParams = new ArrayList<>(); @@ -404,11 +404,17 @@ public class HSQLDBArbitraryRepository implements ArbitraryRepository { } // Handle name matches - if (name != null) { - // Search anywhere in the identifier, unless "prefixOnly" has been requested - String queryWildcard = prefixOnly ? String.format("%s%%", name.toLowerCase()) : String.format("%%%s%%", name.toLowerCase()); - sql.append(" AND LCASE(name) LIKE ?"); - bindParams.add(queryWildcard); + if (names != null && !names.isEmpty()) { + sql.append(" AND ("); + + for (int i = 0; i < names.size(); ++i) { + // Search anywhere in the name, unless "prefixOnly" has been requested + String queryWildcard = prefixOnly ? String.format("%s%%", names.get(i).toLowerCase()) : String.format("%%%s%%", names.get(i).toLowerCase()); + if (i > 0) sql.append(" OR "); + sql.append("LCASE(name) LIKE ?"); + bindParams.add(queryWildcard); + } + sql.append(")"); } sql.append(" GROUP BY name, service, identifier ORDER BY date_created"); diff --git a/src/main/resources/q-apps/q-apps.js b/src/main/resources/q-apps/q-apps.js index 57ac70da..28b81692 100644 --- a/src/main/resources/q-apps/q-apps.js +++ b/src/main/resources/q-apps/q-apps.js @@ -201,6 +201,7 @@ window.addEventListener("message", (event) => { if (data.query != null) url = url.concat("&query=" + data.query); if (data.identifier != null) url = url.concat("&identifier=" + data.identifier); if (data.name != null) url = url.concat("&name=" + data.name); + if (data.names != null) data.names.forEach((x, i) => url = url.concat("&name=" + x)); if (data.prefix != null) url = url.concat("&prefix=" + new Boolean(data.prefix).toString()); if (data.default != null) url = url.concat("&default=" + new Boolean(data.default).toString()); if (data.includeStatus != null) url = url.concat("&includestatus=" + new Boolean(data.includeStatus).toString());