1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package db
- import (
- "context"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- "gogs.io/gogs/internal/dbutil"
- )
- type OrgsStore interface {
-
- List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error)
-
-
-
-
-
- SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error)
-
- CountByUser(ctx context.Context, userID int64) (int64, error)
- }
- var Orgs OrgsStore
- var _ OrgsStore = (*orgs)(nil)
- type orgs struct {
- *gorm.DB
- }
- func NewOrgsStore(db *gorm.DB) OrgsStore {
- return &orgs{DB: db}
- }
- type ListOrgsOptions struct {
-
- MemberID int64
-
- IncludePrivateMembers bool
- }
- func (db *orgs) List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error) {
- if opts.MemberID <= 0 {
- return nil, errors.New("MemberID must be greater than 0")
- }
-
- tx := db.WithContext(ctx).
- Joins(dbutil.Quote("JOIN org_user ON org_user.org_id = %s.id", "user")).
- Where("org_user.uid = ?", opts.MemberID).
- Order(dbutil.Quote("%s.id ASC", "user"))
- if !opts.IncludePrivateMembers {
- tx = tx.Where("org_user.is_public = ?", true)
- }
- var orgs []*Organization
- return orgs, tx.Find(&orgs).Error
- }
- func (db *orgs) SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error) {
- return searchUserByName(ctx, db.DB, UserTypeOrganization, keyword, page, pageSize, orderBy)
- }
- func (db *orgs) CountByUser(ctx context.Context, userID int64) (int64, error) {
- var count int64
- return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
- }
- type Organization = User
- func (o *Organization) TableName() string {
- return "user"
- }
|