Skip to content

Commit

Permalink
Add sorting to Device Channels REST API
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Jul 29, 2021
1 parent 72dd38f commit bbf6bdf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
*******************************************************************************/
package org.eclipse.kapua.app.api.resources.v1.resources;

import java.util.Collections;

import com.google.common.base.Strings;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
import org.eclipse.kapua.app.api.core.model.CountResult;
Expand All @@ -29,6 +32,8 @@
import org.eclipse.kapua.service.datastore.model.query.ChannelInfoQuery;
import org.eclipse.kapua.service.datastore.model.query.predicate.ChannelMatchPredicate;
import org.eclipse.kapua.service.datastore.model.query.predicate.DatastorePredicateFactory;
import org.eclipse.kapua.service.storable.model.query.SortDirection;
import org.eclipse.kapua.service.storable.model.query.SortField;
import org.eclipse.kapua.service.storable.model.query.predicate.AndPredicate;
import org.eclipse.kapua.service.storable.model.query.predicate.TermPredicate;

Expand All @@ -53,20 +58,24 @@ public class DataChannels extends AbstractKapuaResource {
/**
* Gets the {@link ChannelInfo} list in the scope.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param clientId The client id to filter results.
* @param name The channel name to filter results. It allows '#' wildcard in last channel level
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link ChannelInfoListResult} of all the channelInfos associated to the current selected scope.
* @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
* @param scopeId The {@link ScopeId} in which to search results.
* @param clientId The client id to filter results.
* @param name The channel name to filter results. It allows '#' wildcard in last channel level
* @param sortParam The name of the parameter that will be used as a sorting key
* @param sortDir The sort direction. Can be DESC (default), ASC. Case-insensitive.
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link ChannelInfoListResult} of all the channelInfos associated to the current selected scope.
* @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public ChannelInfoListResult simpleQuery(@PathParam("scopeId") ScopeId scopeId,
@QueryParam("clientId") String clientId,
@QueryParam("name") String name,
@QueryParam("sortParam") String sortParam,
@QueryParam("sortDir") @DefaultValue("DESC") SortDirection sortDir,
@QueryParam("offset") @DefaultValue("0") int offset,
@QueryParam("limit") @DefaultValue("50") int limit)
throws KapuaException {
Expand All @@ -85,7 +94,9 @@ public ChannelInfoListResult simpleQuery(@PathParam("scopeId") ScopeId scopeId,
query.setPredicate(andPredicate);
query.setOffset(offset);
query.setLimit(limit);

if (!Strings.isNullOrEmpty(sortParam)) {
query.setSortFields(Collections.singletonList(SortField.of(sortParam, sortDir)));
}
return query(scopeId, query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ paths:
schema:
type: string
description: The channel name to filter results. It allows '#' wildcard in last channel level
- description: The sort parameter
name: sortParam
in: query
schema:
type: string
- description: The sort direction. Can be ascending or descending (default).
name: sortDir
in: query
schema:
type: string
enum:
- ASC
- DESC
default: DESC
- $ref: '../openapi.yaml#/components/parameters/limit'
- $ref: '../openapi.yaml#/components/parameters/offset'
responses:
Expand Down

0 comments on commit bbf6bdf

Please sign in to comment.