From 8350daf505b837984397679f07ccc2324b4d2451 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Wed, 8 Feb 2023 13:55:54 +0800 Subject: refactor(db): merge relation stores into entity stores (#7341) --- internal/db/orgs.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'internal/db/orgs.go') diff --git a/internal/db/orgs.go b/internal/db/orgs.go index db1078ba..753d8120 100644 --- a/internal/db/orgs.go +++ b/internal/db/orgs.go @@ -14,8 +14,6 @@ import ( ) // OrgsStore is the persistent interface for organizations. -// -// NOTE: All methods are sorted in alphabetical order. type OrgsStore interface { // List returns a list of organizations filtered by options. List(ctx context.Context, opts ListOrgsOptions) ([]*Organization, error) @@ -25,6 +23,9 @@ type OrgsStore interface { // count of all results is also returned. If the order is not given, it's up to // the database to decide. SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error) + + // CountByUser returns the number of organizations the user is a member of. + CountByUser(ctx context.Context, userID int64) (int64, error) } var Orgs OrgsStore @@ -79,6 +80,11 @@ func (db *orgs) SearchByName(ctx context.Context, keyword string, page, pageSize return searchUserByName(ctx, db.DB, UserTypeOrganization, keyword, page, pageSize, orderBy) } +func (db *orgs) CountByUser(ctx context.Context, userID int64) (int64, error) { + var count int64 + return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error +} + type Organization = User func (o *Organization) TableName() string { -- cgit v1.2.3