Skip to content

Commit

Permalink
Merge pull request #67 from siemens/license_update
Browse files Browse the repository at this point in the history
fix(license_list): Order licenses by shortname

Reviewed-by: [email protected]
Tested-by: [email protected]
  • Loading branch information
GMishx authored Jun 7, 2024
2 parents 3bbf2d8 + 62abda0 commit 69c39d1
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,6 @@ import (
"gorm.io/gorm/clause"
)

// GetAllLicense The get all License function returns all the license data present in the database.
func GetAllLicense(c *gin.Context) {

var licenses []models.LicenseDB
query := db.DB.Model(&models.LicenseDB{})

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

err := query.Find(&licenses).Error
if err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "Licenses not found",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return
}
res := models.LicenseResponse{
Data: licenses,
Status: http.StatusOK,
Meta: &models.PaginationMeta{
ResourceCount: len(licenses),
},
}

c.JSON(http.StatusOK, res)
}

// FilterLicense Get licenses from service based on different filters.
//
// @Summary Filter licenses
Expand Down Expand Up @@ -109,13 +78,9 @@ func FilterLicense(c *gin.Context) {
}
}

var license []models.LicenseDB
query := db.DB.Model(&license)
var licenses []models.LicenseDB
query := db.DB.Model(&licenses)

if SpdxId == "" && GPLv2compatible == "" && GPLv3compatible == "" && DetectorType == "" && marydone == "" && active == "" && fsffree == "" && OSIapproved == "" && copyleft == "" && externalRef == "" {
GetAllLicense(c)
return
}
if active != "" {
parsedActive, err := strconv.ParseBool(active)
if err != nil {
Expand Down Expand Up @@ -196,7 +161,11 @@ func FilterLicense(c *gin.Context) {
query = query.Where(fmt.Sprintf("external_ref->>'%s' = ?", externalRefKey), externalRefValue)
}

if err := query.Error; err != nil {
query.Order("rf_shortname")

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

if err := query.Find(&licenses).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "incorrect query to search in the database",
Expand All @@ -208,15 +177,11 @@ func FilterLicense(c *gin.Context) {
return
}

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

query.Find(&license)

res := models.LicenseResponse{
Data: license,
Data: licenses,
Status: http.StatusOK,
Meta: &models.PaginationMeta{
ResourceCount: len(license),
ResourceCount: len(licenses),
},
}
c.JSON(http.StatusOK, res)
Expand Down

0 comments on commit 69c39d1

Please sign in to comment.