aboutsummaryrefslogtreecommitdiff
path: root/internal/db/org_users.go
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-10-30 23:04:24 +0800
committerGitHub <noreply@github.com>2022-10-30 23:04:24 +0800
commit1905b19ee7fcc5622ad3ad4d85cc8fc2e81a0b56 (patch)
tree11c6c7bd33837733167f5f3adc9c09dacfbdd19f /internal/db/org_users.go
parent131be6e074039e590488892d7a99ebdbe6eb4668 (diff)
refactor(db): migrate methods off `user.go` (#7219)
Diffstat (limited to 'internal/db/org_users.go')
-rw-r--r--internal/db/org_users.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/db/org_users.go b/internal/db/org_users.go
new file mode 100644
index 00000000..5c4add26
--- /dev/null
+++ b/internal/db/org_users.go
@@ -0,0 +1,38 @@
+// Copyright 2022 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package db
+
+import (
+ "context"
+
+ "gorm.io/gorm"
+)
+
+// OrgUsersStore is the persistent interface for organization-user relations.
+//
+// NOTE: All methods are sorted in alphabetical order.
+type OrgUsersStore interface {
+ // CountByUser returns the number of organizations the user is a member of.
+ CountByUser(ctx context.Context, userID int64) (int64, error)
+}
+
+var OrgUsers OrgUsersStore
+
+var _ OrgUsersStore = (*orgUsers)(nil)
+
+type orgUsers struct {
+ *gorm.DB
+}
+
+// NewOrgUsersStore returns a persistent interface for organization-user
+// relations with given database connection.
+func NewOrgUsersStore(db *gorm.DB) OrgUsersStore {
+ return &orgUsers{DB: db}
+}
+
+func (db *orgUsers) CountByUser(ctx context.Context, userID int64) (int64, error) {
+ var count int64
+ return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
+}