diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index 2b89f41..bb47ccd 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -974,6 +974,17 @@ const docTemplate = `{ "description": "Number of records per page", "name": "limit", "in": "query" + }, + { + "enum": [ + "asc", + "desc" + ], + "type": "string", + "default": "asc", + "description": "Asc or desc ordering", + "name": "order_by", + "in": "query" } ], "responses": { diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index 1af8a65..553e0b0 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -967,6 +967,17 @@ "description": "Number of records per page", "name": "limit", "in": "query" + }, + { + "enum": [ + "asc", + "desc" + ], + "type": "string", + "default": "asc", + "description": "Asc or desc ordering", + "name": "order_by", + "in": "query" } ], "responses": { diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index f207559..eecbb95 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -1377,6 +1377,14 @@ paths: in: query name: limit type: integer + - default: asc + description: Asc or desc ordering + enum: + - asc + - desc + in: query + name: order_by + type: string produces: - application/json responses: diff --git a/pkg/api/obligations.go b/pkg/api/obligations.go index c215ff8..d440adb 100644 --- a/pkg/api/obligations.go +++ b/pkg/api/obligations.go @@ -34,11 +34,12 @@ import ( // @Tags Obligations // @Accept json // @Produce json -// @Param active query bool true "Active obligation only" -// @Param page query int false "Page number" -// @Param limit query int false "Number of records per page" -// @Success 200 {object} models.ObligationResponse -// @Failure 404 {object} models.LicenseError "No obligations in DB" +// @Param active query bool true "Active obligation only" +// @Param page query int false "Page number" +// @Param limit query int false "Number of records per page" +// @Param order_by query string false "Asc or desc ordering" Enums(asc, desc) default(asc) +// @Success 200 {object} models.ObligationResponse +// @Failure 404 {object} models.LicenseError "No obligations in DB" // @Router /obligations [get] func GetAllObligation(c *gin.Context) { var obligations []models.Obligation @@ -64,6 +65,15 @@ func GetAllObligation(c *gin.Context) { _ = utils.PreparePaginateResponse(c, query, &models.ObligationResponse{}) + orderBy := c.Query("order_by") + queryOrderString := "topic" + + if orderBy != "" && orderBy == "desc" { + queryOrderString += " desc" + } + + query.Order(queryOrderString) + if err = query.Find(&obligations).Error; err != nil { er := models.LicenseError{ Status: http.StatusInternalServerError,