orgs.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "github.com/pkg/errors"
  8. "gorm.io/gorm"
  9. "gogs.io/gogs/internal/dbutil"
  10. )
  11. // OrgsStore is the persistent interface for organizations.
  12. type OrgsStore interface {
  13. // List returns a list of organizations filtered by options.
  14. List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error)
  15. // SearchByName returns a list of organizations whose username or full name
  16. // matches the given keyword case-insensitively. Results are paginated by given
  17. // page and page size, and sorted by the given order (e.g. "id DESC"). A total
  18. // count of all results is also returned. If the order is not given, it's up to
  19. // the database to decide.
  20. SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error)
  21. // CountByUser returns the number of organizations the user is a member of.
  22. CountByUser(ctx context.Context, userID int64) (int64, error)
  23. }
  24. var Orgs OrgsStore
  25. var _ OrgsStore = (*orgs)(nil)
  26. type orgs struct {
  27. *gorm.DB
  28. }
  29. // NewOrgsStore returns a persistent interface for orgs with given database
  30. // connection.
  31. func NewOrgsStore(db *gorm.DB) OrgsStore {
  32. return &orgs{DB: db}
  33. }
  34. type ListOrgsOptions struct {
  35. // Filter by the membership with the given user ID.
  36. MemberID int64
  37. // Whether to include private memberships.
  38. IncludePrivateMembers bool
  39. }
  40. func (db *orgs) List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error) {
  41. if opts.MemberID <= 0 {
  42. return nil, errors.New("MemberID must be greater than 0")
  43. }
  44. /*
  45. Equivalent SQL for PostgreSQL:
  46. SELECT * FROM "org"
  47. JOIN org_user ON org_user.org_id = org.id
  48. WHERE
  49. org_user.uid = @memberID
  50. [AND org_user.is_public = @includePrivateMembers]
  51. ORDER BY org.id ASC
  52. */
  53. tx := db.WithContext(ctx).
  54. Joins(dbutil.Quote("JOIN org_user ON org_user.org_id = %s.id", "user")).
  55. Where("org_user.uid = ?", opts.MemberID).
  56. Order(dbutil.Quote("%s.id ASC", "user"))
  57. if !opts.IncludePrivateMembers {
  58. tx = tx.Where("org_user.is_public = ?", true)
  59. }
  60. var orgs []*Organization
  61. return orgs, tx.Find(&orgs).Error
  62. }
  63. func (db *orgs) SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error) {
  64. return searchUserByName(ctx, db.DB, UserTypeOrganization, keyword, page, pageSize, orderBy)
  65. }
  66. func (db *orgs) CountByUser(ctx context.Context, userID int64) (int64, error) {
  67. var count int64
  68. return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
  69. }
  70. type Organization = User
  71. func (o *Organization) TableName() string {
  72. return "user"
  73. }