Skip to content

Commit

Permalink
feat: repo and snapshots packages filter api add 'maximumVersion' que…
Browse files Browse the repository at this point in the history
…ry parameter support

example: `curl http://localhost:8080/api/repos/test/packages\?maximumVersion\=1`

Change-Id: Ie9ffd36146bf017bbb353737f32360f7b73d6b0a
  • Loading branch information
hudeng-go committed Jun 22, 2022
1 parent 2aca913 commit b3dd947
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory
}
}

// filter packages by version
if c.Request.URL.Query().Get("maximumVersion") == "1" {
list.PrepareIndex()
list.ForEach(func(p *deb.Package) error {
versionQ, err := query.Parse(fmt.Sprintf("Name (%s), $Version (<= %s)", p.Name, p.Version))
if err != nil {
fmt.Println("filter packages by version, query string parse err: ", err)
c.AbortWithError(500, fmt.Errorf("unable to parse %s maximum version query string: %s", p.Name, err))
} else {

tmpList, err := list.Filter([]deb.PackageQuery{versionQ}, false,
nil, 0, []string{})

if err == nil {
if tmpList.Len() > 0 {
tmpList.ForEach(func(tp *deb.Package) error {
list.Remove(tp)
return nil
})
list.Add(p)

Check warning on line 230 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L212-L230

Added lines #L212 - L230 were not covered by tests
}
} else {
fmt.Println("filter packages by version, filter err: ", err)
c.AbortWithError(500, fmt.Errorf("unable to get %s maximum version: %s", p.Name, err))
}

Check warning on line 235 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L232-L235

Added lines #L232 - L235 were not covered by tests
}

return nil

Check warning on line 238 in api/api.go

View check run for this annotation

Codecov / codecov/patch

api/api.go#L238

Added line #L238 was not covered by tests
})
}

if c.Request.URL.Query().Get("format") == "details" {
list.ForEach(func(p *deb.Package) error {
result = append(result, p)
Expand Down

0 comments on commit b3dd947

Please sign in to comment.