Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(license_list): Order licenses by shortname #67

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading