@@ -56,13 +56,10 @@ func ListPackages(ctx *context.APIContext) {
5656
5757 listOptions := utils .GetListOptions (ctx )
5858
59- packageType := ctx .FormTrim ("type" )
60- query := ctx .FormTrim ("q" )
61-
62- pvs , count , err := packages .SearchVersions (ctx , & packages.PackageSearchOptions {
59+ apiPackages , count , err := searchPackages (ctx , & packages.PackageSearchOptions {
6360 OwnerID : ctx .Package .Owner .ID ,
64- Type : packages .Type (packageType ),
65- Name : packages.SearchValue {Value : query },
61+ Type : packages .Type (ctx . FormTrim ( "type" ) ),
62+ Name : packages.SearchValue {Value : ctx . FormTrim ( "q" ) },
6663 IsInternal : optional .Some (false ),
6764 Paginator : & listOptions ,
6865 })
@@ -71,22 +68,6 @@ func ListPackages(ctx *context.APIContext) {
7168 return
7269 }
7370
74- pds , err := packages .GetPackageDescriptors (ctx , pvs )
75- if err != nil {
76- ctx .APIErrorInternal (err )
77- return
78- }
79-
80- apiPackages := make ([]* api.Package , 0 , len (pds ))
81- for _ , pd := range pds {
82- apiPackage , err := convert .ToPackage (ctx , pd , ctx .Doer )
83- if err != nil {
84- ctx .APIErrorInternal (err )
85- return
86- }
87- apiPackages = append (apiPackages , apiPackage )
88- }
89-
9071 ctx .SetLinkHeader (int (count ), listOptions .PageSize )
9172 ctx .SetTotalCountHeader (count )
9273 ctx .JSON (http .StatusOK , apiPackages )
@@ -217,6 +198,121 @@ func ListPackageFiles(ctx *context.APIContext) {
217198 ctx .JSON (http .StatusOK , apiPackageFiles )
218199}
219200
201+ // ListPackageVersions gets all versions of a package
202+ func ListPackageVersions (ctx * context.APIContext ) {
203+ // swagger:operation GET /packages/{owner}/{type}/{name} package listPackageVersions
204+ // ---
205+ // summary: Gets all versions of a package
206+ // produces:
207+ // - application/json
208+ // parameters:
209+ // - name: owner
210+ // in: path
211+ // description: owner of the package
212+ // type: string
213+ // required: true
214+ // - name: type
215+ // in: path
216+ // description: type of the package
217+ // type: string
218+ // required: true
219+ // - name: name
220+ // in: path
221+ // description: name of the package
222+ // type: string
223+ // required: true
224+ // - name: page
225+ // in: query
226+ // description: page number of results to return (1-based)
227+ // type: integer
228+ // - name: limit
229+ // in: query
230+ // description: page size of results
231+ // type: integer
232+ // responses:
233+ // "200":
234+ // "$ref": "#/responses/PackageList"
235+ // "404":
236+ // "$ref": "#/responses/notFound"
237+
238+ listOptions := utils .GetListOptions (ctx )
239+
240+ apiPackages , count , err := searchPackages (ctx , & packages.PackageSearchOptions {
241+ OwnerID : ctx .Package .Owner .ID ,
242+ Type : packages .Type (ctx .PathParam ("type" )),
243+ Name : packages.SearchValue {Value : ctx .PathParam ("name" ), ExactMatch : true },
244+ IsInternal : optional .Some (false ),
245+ Paginator : & listOptions ,
246+ })
247+ if err != nil {
248+ ctx .APIErrorInternal (err )
249+ return
250+ }
251+
252+ ctx .SetLinkHeader (int (count ), listOptions .PageSize )
253+ ctx .SetTotalCountHeader (count )
254+ ctx .JSON (http .StatusOK , apiPackages )
255+ }
256+
257+ // GetLatestPackageVersion gets the latest version of a package
258+ func GetLatestPackageVersion (ctx * context.APIContext ) {
259+ // swagger:operation GET /packages/{owner}/{type}/{name}/-/latest package getLatestPackageVersion
260+ // ---
261+ // summary: Gets the latest version of a package
262+ // produces:
263+ // - application/json
264+ // parameters:
265+ // - name: owner
266+ // in: path
267+ // description: owner of the package
268+ // type: string
269+ // required: true
270+ // - name: type
271+ // in: path
272+ // description: type of the package
273+ // type: string
274+ // required: true
275+ // - name: name
276+ // in: path
277+ // description: name of the package
278+ // type: string
279+ // required: true
280+ // responses:
281+ // "200":
282+ // "$ref": "#/responses/Package"
283+ // "404":
284+ // "$ref": "#/responses/notFound"
285+
286+ pvs , _ , err := packages .SearchLatestVersions (ctx , & packages.PackageSearchOptions {
287+ OwnerID : ctx .Package .Owner .ID ,
288+ Type : packages .Type (ctx .PathParam ("type" )),
289+ Name : packages.SearchValue {Value : ctx .PathParam ("name" ), ExactMatch : true },
290+ IsInternal : optional .Some (false ),
291+ })
292+ if err != nil {
293+ ctx .APIErrorInternal (err )
294+ return
295+ }
296+ if len (pvs ) == 0 {
297+ ctx .APIError (http .StatusNotFound , err )
298+ return
299+ }
300+
301+ pd , err := packages .GetPackageDescriptor (ctx , pvs [0 ])
302+ if err != nil {
303+ ctx .APIErrorInternal (err )
304+ return
305+ }
306+
307+ apiPackage , err := convert .ToPackage (ctx , pd , ctx .Doer )
308+ if err != nil {
309+ ctx .APIErrorInternal (err )
310+ return
311+ }
312+
313+ ctx .JSON (http .StatusOK , apiPackage )
314+ }
315+
220316// LinkPackage sets a repository link for a package
221317func LinkPackage (ctx * context.APIContext ) {
222318 // swagger:operation POST /packages/{owner}/{type}/{name}/-/link/{repo_name} package linkPackage
@@ -335,3 +431,26 @@ func UnlinkPackage(ctx *context.APIContext) {
335431 }
336432 ctx .Status (http .StatusNoContent )
337433}
434+
435+ func searchPackages (ctx * context.APIContext , opts * packages.PackageSearchOptions ) ([]* api.Package , int64 , error ) {
436+ pvs , count , err := packages .SearchVersions (ctx , opts )
437+ if err != nil {
438+ return nil , 0 , err
439+ }
440+
441+ pds , err := packages .GetPackageDescriptors (ctx , pvs )
442+ if err != nil {
443+ return nil , 0 , err
444+ }
445+
446+ apiPackages := make ([]* api.Package , 0 , len (pds ))
447+ for _ , pd := range pds {
448+ apiPackage , err := convert .ToPackage (ctx , pd , ctx .Doer )
449+ if err != nil {
450+ return nil , 0 , err
451+ }
452+ apiPackages = append (apiPackages , apiPackage )
453+ }
454+
455+ return apiPackages , count , nil
456+ }
0 commit comments