mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Move more functions to db.Find (#28419)
Following #28220 This PR move more functions to use `db.Find`. --------- Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
@ -225,6 +225,7 @@ func GetReleaseForRepoByID(ctx context.Context, repoID, id int64) (*Release, err
|
||||
// FindReleasesOptions describes the conditions to Find releases
|
||||
type FindReleasesOptions struct {
|
||||
db.ListOptions
|
||||
RepoID int64
|
||||
IncludeDrafts bool
|
||||
IncludeTags bool
|
||||
IsPreRelease util.OptionalBool
|
||||
@ -233,9 +234,8 @@ type FindReleasesOptions struct {
|
||||
HasSha1 util.OptionalBool // useful to find draft releases which are created with existing tags
|
||||
}
|
||||
|
||||
func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
cond = cond.And(builder.Eq{"repo_id": repoID})
|
||||
func (opts FindReleasesOptions) ToConds() builder.Cond {
|
||||
var cond builder.Cond = builder.Eq{"repo_id": opts.RepoID}
|
||||
|
||||
if !opts.IncludeDrafts {
|
||||
cond = cond.And(builder.Eq{"is_draft": false})
|
||||
@ -262,18 +262,8 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
|
||||
return cond
|
||||
}
|
||||
|
||||
// GetReleasesByRepoID returns a list of releases of repository.
|
||||
func GetReleasesByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) ([]*Release, error) {
|
||||
sess := db.GetEngine(ctx).
|
||||
Desc("created_unix", "id").
|
||||
Where(opts.toConds(repoID))
|
||||
|
||||
if opts.PageSize != 0 {
|
||||
sess = db.SetSessionPagination(sess, &opts.ListOptions)
|
||||
}
|
||||
|
||||
rels := make([]*Release, 0, opts.PageSize)
|
||||
return rels, sess.Find(&rels)
|
||||
func (opts FindReleasesOptions) ToOrders() string {
|
||||
return "created_unix DESC, id DESC"
|
||||
}
|
||||
|
||||
// GetTagNamesByRepoID returns a list of release tag names of repository.
|
||||
@ -286,23 +276,19 @@ func GetTagNamesByRepoID(ctx context.Context, repoID int64) ([]string, error) {
|
||||
IncludeDrafts: true,
|
||||
IncludeTags: true,
|
||||
HasSha1: util.OptionalBoolTrue,
|
||||
RepoID: repoID,
|
||||
}
|
||||
|
||||
tags := make([]string, 0)
|
||||
sess := db.GetEngine(ctx).
|
||||
Table("release").
|
||||
Desc("created_unix", "id").
|
||||
Where(opts.toConds(repoID)).
|
||||
Where(opts.ToConds()).
|
||||
Cols("tag_name")
|
||||
|
||||
return tags, sess.Find(&tags)
|
||||
}
|
||||
|
||||
// CountReleasesByRepoID returns a number of releases matching FindReleaseOptions and RepoID.
|
||||
func CountReleasesByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) (int64, error) {
|
||||
return db.GetEngine(ctx).Where(opts.toConds(repoID)).Count(new(Release))
|
||||
}
|
||||
|
||||
// GetLatestReleaseByRepoID returns the latest release for a repository
|
||||
func GetLatestReleaseByRepoID(ctx context.Context, repoID int64) (*Release, error) {
|
||||
cond := builder.NewCond().
|
||||
@ -325,20 +311,6 @@ func GetLatestReleaseByRepoID(ctx context.Context, repoID int64) (*Release, erro
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
|
||||
func GetReleasesByRepoIDAndNames(ctx context.Context, repoID int64, tagNames []string) (rels []*Release, err error) {
|
||||
err = db.GetEngine(ctx).
|
||||
In("tag_name", tagNames).
|
||||
Desc("created_unix").
|
||||
Find(&rels, Release{RepoID: repoID})
|
||||
return rels, err
|
||||
}
|
||||
|
||||
// GetReleaseCountByRepoID returns the count of releases of repository
|
||||
func GetReleaseCountByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) (int64, error) {
|
||||
return db.GetEngine(ctx).Where(opts.toConds(repoID)).Count(&Release{})
|
||||
}
|
||||
|
||||
type releaseMetaSearch struct {
|
||||
ID []int64
|
||||
Rel []*Release
|
||||
|
Reference in New Issue
Block a user