organizations.go 2.6 KB

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