Skip to content

Commit

Permalink
Merge pull request #75 from siemens/license_sorting
Browse files Browse the repository at this point in the history
feat(license_sorting): Enable sorting in licenses by spdx_id, fullname and shortname

Reviewed-by: mishra.gaurav@siemens.com, opensource@avinal.space
Tested-by: mishra.gaurav@siemens.com
  • Loading branch information
GMishx authored Jul 24, 2024
2 parents 9524e7b + e4c4f0e commit 27ea834
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
23 changes: 23 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,29 @@ const docTemplate = `{
"description": "External reference parameters",
"name": "externalRef",
"in": "query"
},
{
"enum": [
"rf_spdx_id",
"rf_shortname",
"rf_fullname"
],
"type": "string",
"default": "rf_shortname",
"description": "Sort by field",
"name": "sort_by",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"default": "asc",
"description": "Asc or desc ordering",
"name": "order_by",
"in": "query"
}
],
"responses": {
Expand Down
23 changes: 23 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@
"description": "External reference parameters",
"name": "externalRef",
"in": "query"
},
{
"enum": [
"rf_spdx_id",
"rf_shortname",
"rf_fullname"
],
"type": "string",
"default": "rf_shortname",
"description": "Sort by field",
"name": "sort_by",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"default": "asc",
"description": "Asc or desc ordering",
"name": "order_by",
"in": "query"
}
],
"responses": {
Expand Down
17 changes: 17 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,23 @@ paths:
in: query
name: externalRef
type: string
- default: rf_shortname
description: Sort by field
enum:
- rf_spdx_id
- rf_shortname
- rf_fullname
in: query
name: sort_by
type: string
- default: asc
description: Asc or desc ordering
enum:
- asc
- desc
in: query
name: order_by
type: string
produces:
- application/json
responses:
Expand Down
17 changes: 16 additions & 1 deletion pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
// @Param page query int false "Page number"
// @Param limit query int false "Limit of responses per page"
// @Param externalRef query string false "External reference parameters"
// @Param sort_by query string false "Sort by field" Enums(rf_spdx_id, rf_shortname, rf_fullname) default(rf_shortname)
// @Param order_by query string false "Asc or desc ordering" Enums(asc, desc) default(asc)
// @Success 200 {object} models.LicenseResponse "Filtered licenses"
// @Failure 400 {object} models.LicenseError "Invalid value"
// @Router /licenses [get]
Expand Down Expand Up @@ -160,7 +162,20 @@ func FilterLicense(c *gin.Context) {
query = query.Where(fmt.Sprintf("external_ref->>'%s' = ?", externalRefKey), externalRefValue)
}

query.Order("rf_shortname")
sortBy := c.Query("sort_by")
orderBy := c.Query("order_by")
queryOrderString := ""
if sortBy != "" && (sortBy == "rf_spdx_id" || sortBy == "rf_shortname" || sortBy == "rf_fullname") {
queryOrderString += sortBy
} else {
queryOrderString += "rf_shortname"
}

if orderBy != "" && orderBy == "desc" {
queryOrderString += " desc"
}

query.Order(queryOrderString)

_ = utils.PreparePaginateResponse(c, query, &models.LicenseResponse{})

Expand Down

0 comments on commit 27ea834

Please sign in to comment.