Skip to content

Commit

Permalink
Merge pull request #4095 from MDeLuise/improve-sortCredential
Browse files Browse the repository at this point in the history
✨ [REST API] Implement sorting functionality in `/{scopeId}/credentials` API
  • Loading branch information
Coduz authored Aug 6, 2024
2 parents fb1b9c9 + 5962247 commit d5b679e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@
*******************************************************************************/
package org.eclipse.kapua.app.api.resources.v1.resources;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.common.base.Strings;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.model.CountResult;
import org.eclipse.kapua.app.api.core.model.EntityId;
import org.eclipse.kapua.app.api.core.model.ScopeId;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.model.query.predicate.AndPredicate;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.authentication.credential.Credential;
Expand All @@ -27,20 +43,6 @@
import org.eclipse.kapua.service.authentication.credential.CredentialQuery;
import org.eclipse.kapua.service.authentication.credential.CredentialService;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("{scopeId}/credentials")
public class Credentials extends AbstractKapuaResource {

Expand All @@ -66,6 +68,8 @@ public class Credentials extends AbstractKapuaResource {
public CredentialListResult simpleQuery(
@PathParam("scopeId") ScopeId scopeId,
@QueryParam("userId") EntityId userId,
@QueryParam("sortParam") String sortParam,
@QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
@QueryParam("offset") @DefaultValue("0") int offset,
@QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
CredentialQuery query = credentialFactory.newQuery(scopeId);
Expand All @@ -74,6 +78,9 @@ public CredentialListResult simpleQuery(
if (userId != null) {
andPredicate.and(query.attributePredicate(CredentialAttributes.USER_ID, userId));
}
if (!Strings.isNullOrEmpty(sortParam)) {
query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
}
query.setPredicate(andPredicate);

query.setOffset(offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ paths:
description: The optional id to filter results
schema:
$ref: '../openapi.yaml#/components/schemas/kapuaId'
- $ref: '../openapi.yaml#/components/parameters/sortParam'
- name: sortDir
in: query
description: The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive (except for "clientId" parameter).
schema:
type: string
enum:
- ASCENDING
- DESCENDING
default: ASCENDING
- $ref: '../openapi.yaml#/components/parameters/limit'
- $ref: '../openapi.yaml#/components/parameters/offset'

Expand Down

0 comments on commit d5b679e

Please sign in to comment.