Browse Source

Use custom type and constants to hold order by options (#2572)

Morlinest 2 years ago
parent
commit
9a75a5d59b
3 changed files with 51 additions and 32 deletions
  1. 32 13
      models/repo_list.go
  2. 9 9
      routers/home.go
  3. 10 10
      routers/user/profile.go

+ 32 - 13
models/repo_list.go

@@ -97,14 +97,14 @@ type SearchRepoOptions struct {
97 97
 	// Owner in we search search
98 98
 	//
99 99
 	// in: query
100
-	OwnerID     int64  `json:"uid"`
101
-	Searcher    *User  `json:"-"` //ID of the person who's seeking
102
-	OrderBy     string `json:"-"`
103
-	Private     bool   `json:"-"` // Include private repositories in results
104
-	Collaborate bool   `json:"-"` // Include collaborative repositories
105
-	Starred     bool   `json:"-"`
106
-	Page        int    `json:"-"`
107
-	IsProfile   bool   `json:"-"`
100
+	OwnerID     int64         `json:"uid"`
101
+	Searcher    *User         `json:"-"` //ID of the person who's seeking
102
+	OrderBy     SearchOrderBy `json:"-"`
103
+	Private     bool          `json:"-"` // Include private repositories in results
104
+	Collaborate bool          `json:"-"` // Include collaborative repositories
105
+	Starred     bool          `json:"-"`
106
+	Page        int           `json:"-"`
107
+	IsProfile   bool          `json:"-"`
108 108
 	// Limit of result
109 109
 	//
110 110
 	// maximum: setting.ExplorePagingNum
@@ -112,6 +112,25 @@ type SearchRepoOptions struct {
112 112
 	PageSize int `json:"limit"` // Can be smaller than or equal to setting.ExplorePagingNum
113 113
 }
114 114
 
115
+//SearchOrderBy is used to sort the result
116
+type SearchOrderBy string
117
+
118
+func (s SearchOrderBy) String() string {
119
+	return string(s)
120
+}
121
+
122
+// Strings for sorting result
123
+const (
124
+	SearchOrderByAlphabetically        SearchOrderBy = "name ASC"
125
+	SearchOrderByAlphabeticallyReverse               = "name DESC"
126
+	SearchOrderByLeastUpdated                        = "updated_unix ASC"
127
+	SearchOrderByRecentUpdated                       = "updated_unix DESC"
128
+	SearchOrderByOldest                              = "created_unix ASC"
129
+	SearchOrderByNewest                              = "created_unix DESC"
130
+	SearchOrderBySize                                = "size ASC"
131
+	SearchOrderBySizeReverse                         = "size DESC"
132
+)
133
+
115 134
 // SearchRepositoryByName takes keyword and part of repository name to search,
116 135
 // it returns results in given range and number of total results.
117 136
 func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
@@ -164,7 +183,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
164 183
 	}
165 184
 
166 185
 	if len(opts.OrderBy) == 0 {
167
-		opts.OrderBy = "name ASC"
186
+		opts.OrderBy = SearchOrderByAlphabetically
168 187
 	}
169 188
 
170 189
 	sess := x.NewSession()
@@ -193,7 +212,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
193 212
 	if err = sess.
194 213
 		Where(cond).
195 214
 		Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
196
-		OrderBy(opts.OrderBy).
215
+		OrderBy(opts.OrderBy.String()).
197 216
 		Find(&repos); err != nil {
198 217
 		return nil, 0, fmt.Errorf("Repo: %v", err)
199 218
 	}
@@ -217,7 +236,7 @@ func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err e
217 236
 
218 237
 	if err = x.
219 238
 		Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
220
-		OrderBy(opts.OrderBy).
239
+		OrderBy(opts.OrderBy.String()).
221 240
 		Find(&repos); err != nil {
222 241
 		return nil, 0, fmt.Errorf("Repo: %v", err)
223 242
 	}
@@ -236,7 +255,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList
236 255
 	var cond = builder.NewCond()
237 256
 
238 257
 	if len(opts.OrderBy) == 0 {
239
-		opts.OrderBy = "updated_unix DESC"
258
+		opts.OrderBy = SearchOrderByRecentUpdated
240 259
 	}
241 260
 
242 261
 	if !opts.Private {
@@ -270,7 +289,7 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList
270 289
 	if err = x.Where(cond).
271 290
 		Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
272 291
 		Limit(opts.PageSize).
273
-		OrderBy(opts.OrderBy).
292
+		OrderBy(opts.OrderBy.String()).
274 293
 		Find(&repos); err != nil {
275 294
 		return nil, 0, fmt.Errorf("Repo: %v", err)
276 295
 	}

+ 9 - 9
routers/home.go

@@ -86,27 +86,27 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
86 86
 		repos   []*models.Repository
87 87
 		count   int64
88 88
 		err     error
89
-		orderBy string
89
+		orderBy models.SearchOrderBy
90 90
 	)
91 91
 	ctx.Data["SortType"] = ctx.Query("sort")
92 92
 
93 93
 	switch ctx.Query("sort") {
94 94
 	case "oldest":
95
-		orderBy = "created_unix ASC"
95
+		orderBy = models.SearchOrderByOldest
96 96
 	case "recentupdate":
97
-		orderBy = "updated_unix DESC"
97
+		orderBy = models.SearchOrderByRecentUpdated
98 98
 	case "leastupdate":
99
-		orderBy = "updated_unix ASC"
99
+		orderBy = models.SearchOrderByLeastUpdated
100 100
 	case "reversealphabetically":
101
-		orderBy = "name DESC"
101
+		orderBy = models.SearchOrderByAlphabeticallyReverse
102 102
 	case "alphabetically":
103
-		orderBy = "name ASC"
103
+		orderBy = models.SearchOrderByAlphabetically
104 104
 	case "reversesize":
105
-		orderBy = "size DESC"
105
+		orderBy = models.SearchOrderBySizeReverse
106 106
 	case "size":
107
-		orderBy = "size ASC"
107
+		orderBy = models.SearchOrderBySize
108 108
 	default:
109
-		orderBy = "created_unix DESC"
109
+		orderBy = models.SearchOrderByNewest
110 110
 	}
111 111
 
112 112
 	keyword := strings.Trim(ctx.Query("q"), " ")

+ 10 - 10
routers/user/profile.go

@@ -107,26 +107,26 @@ func Profile(ctx *context.Context) {
107 107
 	var (
108 108
 		repos   []*models.Repository
109 109
 		count   int64
110
-		orderBy string
110
+		orderBy models.SearchOrderBy
111 111
 	)
112 112
 
113 113
 	ctx.Data["SortType"] = ctx.Query("sort")
114 114
 	switch ctx.Query("sort") {
115 115
 	case "newest":
116
-		orderBy = "created_unix DESC"
116
+		orderBy = models.SearchOrderByNewest
117 117
 	case "oldest":
118
-		orderBy = "created_unix ASC"
118
+		orderBy = models.SearchOrderByOldest
119 119
 	case "recentupdate":
120
-		orderBy = "updated_unix DESC"
120
+		orderBy = models.SearchOrderByRecentUpdated
121 121
 	case "leastupdate":
122
-		orderBy = "updated_unix ASC"
122
+		orderBy = models.SearchOrderByLeastUpdated
123 123
 	case "reversealphabetically":
124
-		orderBy = "name DESC"
124
+		orderBy = models.SearchOrderByAlphabeticallyReverse
125 125
 	case "alphabetically":
126
-		orderBy = "name ASC"
126
+		orderBy = models.SearchOrderByAlphabetically
127 127
 	default:
128 128
 		ctx.Data["SortType"] = "recentupdate"
129
-		orderBy = "updated_unix DESC"
129
+		orderBy = models.SearchOrderByNewest
130 130
 	}
131 131
 
132 132
 	// set default sort value if sort is empty.
@@ -149,7 +149,7 @@ func Profile(ctx *context.Context) {
149 149
 	case "stars":
150 150
 		ctx.Data["PageIsProfileStarList"] = true
151 151
 		if len(keyword) == 0 {
152
-			repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
152
+			repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
153 153
 			if err != nil {
154 154
 				ctx.Handle(500, "GetStarredRepos", err)
155 155
 				return
@@ -182,7 +182,7 @@ func Profile(ctx *context.Context) {
182 182
 	default:
183 183
 		if len(keyword) == 0 {
184 184
 			var total int
185
-			repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
185
+			repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
186 186
 			if err != nil {
187 187
 				ctx.Handle(500, "GetRepositories", err)
188 188
 				return