@@ -97,21 +97,40 @@ 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
111
111
// in: query
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
}
0 commit comments