diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-11-24 11:49:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-24 03:49:41 +0000 |
commit | df1e7d0067bb39913eb681ccc920649884fb1938 (patch) | |
tree | 2419feab5c28658adb7f71878df646bdc9bdc50e /models/project | |
parent | d24a8223ce1e47a0c9b103aae07f67c3112ca048 (diff) | |
download | gitea-df1e7d0067bb39913eb681ccc920649884fb1938.tar.gz gitea-df1e7d0067bb39913eb681ccc920649884fb1938.zip |
Use db.Find instead of writing methods for every object (#28084)
For those simple objects, it's unnecessary to write the find and count
methods again and again.
Diffstat (limited to 'models/project')
-rw-r--r-- | models/project/project.go | 25 | ||||
-rw-r--r-- | models/project/project_test.go | 6 |
2 files changed, 7 insertions, 24 deletions
diff --git a/models/project/project.go b/models/project/project.go index 3a1bfe1dbd..becfcbea1e 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -192,16 +192,16 @@ func IsTypeValid(p Type) bool { // SearchOptions are options for GetProjects type SearchOptions struct { + db.ListOptions OwnerID int64 RepoID int64 - Page int IsClosed util.OptionalBool OrderBy db.SearchOrderBy Type Type Title string } -func (opts *SearchOptions) toConds() builder.Cond { +func (opts SearchOptions) ToConds() builder.Cond { cond := builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) @@ -226,9 +226,8 @@ func (opts *SearchOptions) toConds() builder.Cond { return cond } -// CountProjects counts projects -func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) { - return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project)) +func (opts SearchOptions) ToOrders() string { + return opts.OrderBy.String() } func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy { @@ -244,22 +243,6 @@ func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy { } } -// FindProjects returns a list of all projects that have been created in the repository -func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { - e := db.GetEngine(ctx).Where(opts.toConds()) - if opts.OrderBy.String() != "" { - e = e.OrderBy(opts.OrderBy.String()) - } - projects := make([]*Project, 0, setting.UI.IssuePagingNum) - - if opts.Page > 0 { - e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum) - } - - count, err := e.FindAndCount(&projects) - return projects, count, err -} - // NewProject creates a new Project func NewProject(ctx context.Context, p *Project) error { if !IsBoardTypeValid(p.BoardType) { diff --git a/models/project/project_test.go b/models/project/project_test.go index 6b5bd5b371..7a37c1faf2 100644 --- a/models/project/project_test.go +++ b/models/project/project_test.go @@ -34,13 +34,13 @@ func TestIsProjectTypeValid(t *testing.T) { func TestGetProjects(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - projects, _, err := FindProjects(db.DefaultContext, SearchOptions{RepoID: 1}) + projects, err := db.Find[Project](db.DefaultContext, SearchOptions{RepoID: 1}) assert.NoError(t, err) // 1 value for this repo exists in the fixtures assert.Len(t, projects, 1) - projects, _, err = FindProjects(db.DefaultContext, SearchOptions{RepoID: 3}) + projects, err = db.Find[Project](db.DefaultContext, SearchOptions{RepoID: 3}) assert.NoError(t, err) // 1 value for this repo exists in the fixtures @@ -109,7 +109,7 @@ func TestProjectsSort(t *testing.T) { } for _, tt := range tests { - projects, count, err := FindProjects(db.DefaultContext, SearchOptions{ + projects, count, err := db.FindAndCount[Project](db.DefaultContext, SearchOptions{ OrderBy: GetSearchOrderByBySortType(tt.sortType), }) assert.NoError(t, err) |