@@ -440,24 +440,6 @@ func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*User, error) {
440
440
And ("team_user.org_id = ?" , orgID ).Asc ("`user`.name" ).Find (& users )
441
441
}
442
442
443
- func getOrgsByUserID (e db.Engine , userID int64 , showAll bool ) ([]* Organization , error ) {
444
- orgs := make ([]* Organization , 0 , 10 )
445
- sess := e .Where ("`org_user`.uid=?" , userID )
446
- if ! showAll {
447
- sess = sess .And ("`org_user`.is_public=?" , true )
448
- }
449
- return orgs , sess .
450
- Join ("INNER" , "`org_user`" , "`org_user`.org_id=`user`.id" ).
451
- Asc ("`user`.name" ).
452
- Find (& orgs )
453
- }
454
-
455
- // GetOrgsByUserID returns a list of organizations that the given user ID
456
- // has joined.
457
- func GetOrgsByUserID (userID int64 , showAll bool ) ([]* Organization , error ) {
458
- return getOrgsByUserID (db .GetEngine (db .DefaultContext ), userID , showAll )
459
- }
460
-
461
443
// MinimalOrg represents a simple orgnization with only needed columns
462
444
type MinimalOrg = Organization
463
445
@@ -519,6 +501,51 @@ func GetUserOrgsList(user *User) ([]*MinimalOrg, error) {
519
501
return orgs , nil
520
502
}
521
503
504
+ // FindOrgOptions finds orgs options
505
+ type FindOrgOptions struct {
506
+ db.ListOptions
507
+ UserID int64
508
+ IncludePrivate bool
509
+ }
510
+
511
+ func queryUserOrgIDs (userID int64 , includePrivate bool ) * builder.Builder {
512
+ var cond = builder.Eq {"uid" : userID }
513
+ if ! includePrivate {
514
+ cond ["is_public" ] = true
515
+ }
516
+ return builder .Select ("org_id" ).From ("org_user" ).Where (cond )
517
+ }
518
+
519
+ func (opts FindOrgOptions ) toConds () builder.Cond {
520
+ var cond = builder .NewCond ()
521
+ if opts .UserID > 0 {
522
+ cond = cond .And (builder .In ("`user`.`id`" , queryUserOrgIDs (opts .UserID , opts .IncludePrivate )))
523
+ }
524
+ if ! opts .IncludePrivate {
525
+ cond = cond .And (builder.Eq {"`user`.visibility" : structs .VisibleTypePublic })
526
+ }
527
+ return cond
528
+ }
529
+
530
+ // FindOrgs returns a list of organizations according given conditions
531
+ func FindOrgs (opts FindOrgOptions ) ([]* Organization , error ) {
532
+ orgs := make ([]* Organization , 0 , 10 )
533
+ sess := db .GetEngine (db .DefaultContext ).
534
+ Where (opts .toConds ()).
535
+ Asc ("`user`.name" )
536
+ if opts .Page > 0 && opts .PageSize > 0 {
537
+ sess .Limit (opts .PageSize , opts .PageSize * (opts .Page - 1 ))
538
+ }
539
+ return orgs , sess .Find (& orgs )
540
+ }
541
+
542
+ // CountOrgs returns total count organizations according options
543
+ func CountOrgs (opts FindOrgOptions ) (int64 , error ) {
544
+ return db .GetEngine (db .DefaultContext ).
545
+ Where (opts .toConds ()).
546
+ Count (new (User ))
547
+ }
548
+
522
549
func getOwnedOrgsByUserID (sess db.Engine , userID int64 ) ([]* User , error ) {
523
550
orgs := make ([]* User , 0 , 10 )
524
551
return orgs , sess .
0 commit comments