12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package database
- import (
- "context"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- "gogs.io/gogs/internal/dbutil"
- )
- type OrganizationsStore struct {
- db *gorm.DB
- }
- func newOrganizationsStoreStore(db *gorm.DB) *OrganizationsStore {
- return &OrganizationsStore{db: db}
- }
- type ListOrgsOptions struct {
-
- MemberID int64
-
- IncludePrivateMembers bool
- }
- func (s *OrganizationsStore) List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error) {
- if opts.MemberID <= 0 {
- return nil, errors.New("MemberID must be greater than 0")
- }
-
- tx := s.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 (s *OrganizationsStore) SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error) {
- return searchUserByName(ctx, s.db, UserTypeOrganization, keyword, page, pageSize, orderBy)
- }
- func (s *OrganizationsStore) CountByUser(ctx context.Context, userID int64) (int64, error) {
- var count int64
- return count, s.db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
- }
- type Organization = User
- func (o *Organization) TableName() string {
- return "user"
- }
|