From 5962247e63c7aef5f510d3fd51e870695c8dd56b Mon Sep 17 00:00:00 2001 From: MDeLuise <66636702+MDeLuise@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:56:11 +0200 Subject: [PATCH] feat(api): add `sortDir` and `sortParam` to '/{scopeId}/credentials' endpoint Implemented sorting functionality for the '/{scopeId}/credentials' API by adding query parameters `sortDir` and `sortParam`. This feature allows sorting of credentials information based on the specified field (`sortParam`) in either ascending or descending order --- .../resources/v1/resources/Credentials.java | 35 +++++++++++-------- .../credential/credential-scopeId.yaml | 10 ++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/Credentials.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/Credentials.java index 9b312351308..c0132c7fa77 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/Credentials.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/Credentials.java @@ -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; @@ -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 { @@ -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); @@ -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); diff --git a/rest-api/resources/src/main/resources/openapi/credential/credential-scopeId.yaml b/rest-api/resources/src/main/resources/openapi/credential/credential-scopeId.yaml index b326bf00df8..94d597c0166 100644 --- a/rest-api/resources/src/main/resources/openapi/credential/credential-scopeId.yaml +++ b/rest-api/resources/src/main/resources/openapi/credential/credential-scopeId.yaml @@ -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'