org_users.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "gorm.io/gorm"
  8. )
  9. // OrgUsersStore is the persistent interface for organization-user relations.
  10. //
  11. // NOTE: All methods are sorted in alphabetical order.
  12. type OrgUsersStore interface {
  13. // CountByUser returns the number of organizations the user is a member of.
  14. CountByUser(ctx context.Context, userID int64) (int64, error)
  15. }
  16. var OrgUsers OrgUsersStore
  17. var _ OrgUsersStore = (*orgUsers)(nil)
  18. type orgUsers struct {
  19. *gorm.DB
  20. }
  21. // NewOrgUsersStore returns a persistent interface for organization-user
  22. // relations with given database connection.
  23. func NewOrgUsersStore(db *gorm.DB) OrgUsersStore {
  24. return &orgUsers{DB: db}
  25. }
  26. func (db *orgUsers) CountByUser(ctx context.Context, userID int64) (int64, error) {
  27. var count int64
  28. return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
  29. }