Skip to content

Commit

Permalink
Search group name / description
Browse files Browse the repository at this point in the history
  • Loading branch information
QuickMythril committed Feb 10, 2024
1 parent 9e001df commit 69daa34
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/org/qortal/api/resource/GroupsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,45 @@ public GroupData getGroupData(@PathParam("groupid") int groupId) {
}
}

@GET
@Path("/search")
@Operation(
summary = "Search group name and description fields",
responses = {
@ApiResponse(
description = "group info",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
array = @ArraySchema(schema = @Schema(implementation = GroupData.class))
)
)
}
)
@ApiErrors({ApiError.REPOSITORY_ISSUE})
public List<GroupData> searchGroups(@Parameter(
description = "Query (searches name and description fields)"
) @QueryParam("query") String query, @Parameter(
ref = "limit"
) @QueryParam("limit") Integer limit, @Parameter(
ref = "offset"
) @QueryParam("offset") Integer offset, @Parameter(
ref = "reverse"
) @QueryParam("reverse") Boolean reverse) {
try (final Repository repository = RepositoryManager.getRepository()) {
List<GroupData> searchResults = repository.getGroupRepository().searchGroups(query, limit, offset, reverse);
searchResults.forEach(result -> {
try {
result.memberCount = repository.getGroupRepository().countGroupMembers(result.getGroupId());
} catch (DataException e) {
// Exclude memberCount for this group
}
});
return searchResults;
} catch (DataException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}

@GET
@Path("/members/{groupid}")
@Operation(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/qortal/repository/GroupRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public default List<GroupData> getGroupsWithMember(String member) throws DataExc
return getGroupsWithMember(member, null, null, null);
}

public List<GroupData> searchGroups(String query, Integer limit, Integer offset, Boolean reverse) throws DataException;

public void save(GroupData groupData) throws DataException;

public void delete(int groupId) throws DataException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,69 @@ public List<GroupData> getGroupsWithMember(String member, Integer limit, Integer
}
}

@Override
public List<GroupData> searchGroups(String query, Integer limit, Integer offset, Boolean reverse) throws DataException {
StringBuilder sql = new StringBuilder(512);
List<Object> bindParams = new ArrayList<>();

sql.append("SELECT group_id, owner, group_name, description, created_when, updated_when, reference, is_open, "
+ "approval_threshold, min_block_delay, max_block_delay, creation_group_id, reduced_group_name "
+ "FROM Groups");

// Handle general query matches
if (query != null) {
String queryWildcard = String.format("%%%s%%", query.toLowerCase());
sql.append(" WHERE (LCASE(group_name) LIKE ? OR LCASE(description) LIKE ?)");
bindParams.add(queryWildcard); bindParams.add(queryWildcard);
}

sql.append(" ORDER BY group_name");

if (reverse != null && reverse)
sql.append(" DESC");

HSQLDBRepository.limitOffsetSql(sql, limit, offset);

List<GroupData> groups = new ArrayList<>();

try (ResultSet resultSet = this.repository.checkedExecute(sql.toString(), bindParams.toArray())) {
if (resultSet == null)
return groups;

do {
int groupId = resultSet.getInt(1);
String owner = resultSet.getString(2);
String groupName = resultSet.getString(3);
String description = resultSet.getString(4);
long created = resultSet.getLong(5);

// Special handling for possibly-NULL "updated" column
Long updated = resultSet.getLong(6);
if (updated == 0 && resultSet.wasNull())
updated = null;

byte[] reference = resultSet.getBytes(7);
boolean isOpen = resultSet.getBoolean(8);

ApprovalThreshold approvalThreshold = ApprovalThreshold.valueOf(resultSet.getInt(9));

int minBlockDelay = resultSet.getInt(10);
int maxBlockDelay = resultSet.getInt(11);

int creationGroupId = resultSet.getInt(12);
String reducedGroupName = resultSet.getString(13);

groups.add(new GroupData(groupId, owner, groupName, description, created, updated, isOpen,
approvalThreshold, minBlockDelay, maxBlockDelay,
reference, creationGroupId, reducedGroupName));
} while (resultSet.next());

return groups;
} catch (SQLException e) {
throw new DataException("Unable to fetch groups from repository", e);
}
}

@Override
public void save(GroupData groupData) throws DataException {
HSQLDBSaver saveHelper = new HSQLDBSaver("Groups");
Expand Down

0 comments on commit 69daa34

Please sign in to comment.