code.gitea.io/gitea@v1.21.7/models/packages/nuget/search.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package nuget 5 6 import ( 7 "context" 8 "strings" 9 10 "code.gitea.io/gitea/models/db" 11 packages_model "code.gitea.io/gitea/models/packages" 12 13 "xorm.io/builder" 14 ) 15 16 // SearchVersions gets all versions of packages matching the search options 17 func SearchVersions(ctx context.Context, opts *packages_model.PackageSearchOptions) ([]*packages_model.PackageVersion, int64, error) { 18 cond := toConds(opts) 19 20 e := db.GetEngine(ctx) 21 22 total, err := e. 23 Where(cond). 24 Count(&packages_model.Package{}) 25 if err != nil { 26 return nil, 0, err 27 } 28 29 inner := builder. 30 Dialect(db.BuilderDialect()). // builder needs the sql dialect to build the Limit() below 31 Select("*"). 32 From("package"). 33 Where(cond). 34 OrderBy("package.name ASC") 35 if opts.Paginator != nil { 36 skip, take := opts.GetSkipTake() 37 inner = inner.Limit(take, skip) 38 } 39 40 sess := e. 41 Where(opts.ToConds()). 42 Table("package_version"). 43 Join("INNER", inner, "package.id = package_version.package_id") 44 45 pvs := make([]*packages_model.PackageVersion, 0, 10) 46 return pvs, total, sess.Find(&pvs) 47 } 48 49 // CountPackages counts all packages matching the search options 50 func CountPackages(ctx context.Context, opts *packages_model.PackageSearchOptions) (int64, error) { 51 return db.GetEngine(ctx). 52 Where(toConds(opts)). 53 Count(&packages_model.Package{}) 54 } 55 56 func toConds(opts *packages_model.PackageSearchOptions) builder.Cond { 57 var cond builder.Cond = builder.Eq{ 58 "package.is_internal": opts.IsInternal.IsTrue(), 59 "package.owner_id": opts.OwnerID, 60 "package.type": packages_model.TypeNuGet, 61 } 62 if opts.Name.Value != "" { 63 if opts.Name.ExactMatch { 64 cond = cond.And(builder.Eq{"package.lower_name": strings.ToLower(opts.Name.Value)}) 65 } else { 66 cond = cond.And(builder.Like{"package.lower_name", strings.ToLower(opts.Name.Value)}) 67 } 68 } 69 return cond 70 }