1
0
forked from baron/baron-sso
Files
baron-sso/backend/internal/repository/user_group_repository.go

54 lines
1.7 KiB
Go

package repository
import (
"baron-sso-backend/internal/domain"
"context"
"gorm.io/gorm"
)
type UserGroupRepository interface {
Create(ctx context.Context, group *domain.UserGroup) error
Update(ctx context.Context, group *domain.UserGroup) error
Delete(ctx context.Context, id string) error
FindByID(ctx context.Context, id string) (*domain.UserGroup, error)
ListByTenantID(ctx context.Context, tenantID string) ([]domain.UserGroup, error)
}
type userGroupRepository struct {
db *gorm.DB
}
func NewUserGroupRepository(db *gorm.DB) UserGroupRepository {
return &userGroupRepository{db: db}
}
func (r *userGroupRepository) Create(ctx context.Context, group *domain.UserGroup) error {
return r.db.WithContext(ctx).Create(group).Error
}
func (r *userGroupRepository) Update(ctx context.Context, group *domain.UserGroup) error {
return r.db.WithContext(ctx).Save(group).Error
}
func (r *userGroupRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Delete(&domain.UserGroup{}, "id = ?", id).Error
}
func (r *userGroupRepository) FindByID(ctx context.Context, id string) (*domain.UserGroup, error) {
var group domain.UserGroup
// Using Where to be more explicit and avoid issues with GORM's default primary key handling if ID is string/uuid
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&group).Error; err != nil {
return nil, err
}
return &group, nil
}
func (r *userGroupRepository) ListByTenantID(ctx context.Context, tenantID string) ([]domain.UserGroup, error) {
var groups []domain.UserGroup
if err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&groups).Error; err != nil {
return nil, err
}
return groups, nil
}