@@ -1030,14 +1030,19 @@ func CountUserRepositories(userID int64, private bool) int64 {
1030
1030
return countRepositories (userID , private )
1031
1031
}
1032
1032
1033
- func Repositories (page , pageSize int ) (_ []* Repository , err error ) {
1034
- repos := make ([]* Repository , 0 , pageSize )
1035
- return repos , x .Limit (pageSize , (page - 1 )* pageSize ).Asc ("id" ).Find (& repos )
1033
+ // Repositories returns all repositories
1034
+ func Repositories (opts * SearchRepoOptions ) (_ []* Repository , err error ) {
1035
+ if len (opts .OrderBy ) == 0 {
1036
+ opts .OrderBy = "id ASC"
1037
+ }
1038
+
1039
+ repos := make ([]* Repository , 0 , opts .PageSize )
1040
+ return repos , x .Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize ).OrderBy (opts .OrderBy ).Find (& repos )
1036
1041
}
1037
1042
1038
1043
// RepositoriesWithUsers returns number of repos in given page.
1039
- func RepositoriesWithUsers (page , pageSize int ) (_ []* Repository , err error ) {
1040
- repos , err := Repositories (page , pageSize )
1044
+ func RepositoriesWithUsers (opts * SearchRepoOptions ) (_ []* Repository , err error ) {
1045
+ repos , err := Repositories (opts )
1041
1046
if err != nil {
1042
1047
return nil , fmt .Errorf ("Repositories: %v" , err )
1043
1048
}
@@ -1484,12 +1489,31 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
1484
1489
}
1485
1490
1486
1491
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
1487
- func GetRecentUpdatedRepositories (page , pageSize int ) (repos []* Repository , err error ) {
1488
- return repos , x .
1489
- Limit (pageSize , (page - 1 )* pageSize ).
1490
- Where ("is_private=?" , false ).
1491
- Limit (pageSize ).
1492
- Desc ("updated_unix" ).
1492
+ func GetRecentUpdatedRepositories (opts * SearchRepoOptions ) (repos []* Repository , err error ) {
1493
+ if len (opts .OrderBy ) == 0 {
1494
+ opts .OrderBy = "updated_unix DESC"
1495
+ }
1496
+
1497
+ sess := x .Where ("is_private=?" , false ).
1498
+ Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize ).
1499
+ Limit (opts .PageSize )
1500
+
1501
+ if opts .Searcher != nil {
1502
+ sess .Or ("owner_id = ?" , opts .Searcher .ID )
1503
+
1504
+ err := opts .Searcher .GetOrganizations (true )
1505
+
1506
+ if err != nil {
1507
+ return nil , fmt .Errorf ("Organization: %v" , err )
1508
+ }
1509
+
1510
+ for _ , org := range opts .Searcher .Orgs {
1511
+ sess .Or ("owner_id = ?" , org .ID )
1512
+ }
1513
+ }
1514
+
1515
+ return repos , sess .
1516
+ OrderBy (opts .OrderBy ).
1493
1517
Find (& repos )
1494
1518
}
1495
1519
@@ -1502,9 +1526,11 @@ func GetRepositoryCount(u *User) (int64, error) {
1502
1526
return getRepositoryCount (x , u )
1503
1527
}
1504
1528
1529
+ // SearchRepoOptions holds the search options
1505
1530
type SearchRepoOptions struct {
1506
1531
Keyword string
1507
1532
OwnerID int64
1533
+ Searcher * User //ID of the person who's seeking
1508
1534
OrderBy string
1509
1535
Private bool // Include private repositories in results
1510
1536
Page int
@@ -1534,17 +1560,36 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
1534
1560
sess .And ("is_private=?" , false )
1535
1561
}
1536
1562
1563
+ if opts .Searcher != nil {
1564
+
1565
+ sess .Or ("owner_id = ?" , opts .Searcher .ID )
1566
+
1567
+ err := opts .Searcher .GetOrganizations (true )
1568
+
1569
+ if err != nil {
1570
+ return nil , 0 , fmt .Errorf ("Organization: %v" , err )
1571
+ }
1572
+
1573
+ for _ , org := range opts .Searcher .Orgs {
1574
+ sess .Or ("owner_id = ?" , org .ID )
1575
+ }
1576
+ }
1577
+
1578
+ if len (opts .OrderBy ) == 0 {
1579
+ opts .OrderBy = "name ASC"
1580
+ }
1581
+
1537
1582
var countSess xorm.Session
1538
1583
countSess = * sess
1539
1584
count , err := countSess .Count (new (Repository ))
1540
1585
if err != nil {
1541
1586
return nil , 0 , fmt .Errorf ("Count: %v" , err )
1542
1587
}
1543
1588
1544
- if len ( opts . OrderBy ) > 0 {
1545
- sess . OrderBy (opts .OrderBy )
1546
- }
1547
- return repos , count , sess . Limit ( opts . PageSize , ( opts . Page - 1 ) * opts . PageSize ). Find (& repos )
1589
+ return repos , count , sess .
1590
+ Limit ( opts . PageSize , (opts .Page - 1 ) * opts . PageSize ).
1591
+ OrderBy ( opts . OrderBy ).
1592
+ Find (& repos )
1548
1593
}
1549
1594
1550
1595
// DeleteRepositoryArchives deletes all repositories' archives.
0 commit comments