114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"code.30cm.net/digimon/app-cloudep-permission-server/pkg/domain/entity"
|
|
"code.30cm.net/digimon/app-cloudep-permission-server/pkg/domain/repository"
|
|
mgo "code.30cm.net/digimon/library-go/mongo"
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type RolePermissionRepositoryParam struct {
|
|
Conf *mgo.Conf
|
|
DBOpts []mon.Option
|
|
}
|
|
|
|
type RolePermissionRepository struct {
|
|
DB mgo.DocumentDBUseCase
|
|
}
|
|
|
|
func NewRolePermissionRepository(param RoleRepositoryParam) repository.RolePermissionRepository {
|
|
e := entity.RolePermission{}
|
|
db, err := mgo.NewDocumentDB(param.Conf, e.Collection(), param.DBOpts...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &RolePermissionRepository{
|
|
DB: db,
|
|
}
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) Get(ctx context.Context, roleID string) ([]*entity.RolePermission, error) {
|
|
var result []*entity.RolePermission
|
|
err := repo.DB.GetClient().Find(ctx, &result, bson.M{"role_id": roleID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) GetByPermissionID(ctx context.Context, permissionIDs []string) ([]*entity.RolePermission, error) {
|
|
var result []*entity.RolePermission // 修正 []*entity.RolePermission -> []entity.RolePermission
|
|
|
|
filter := bson.M{
|
|
"permission_id": bson.M{"$in": permissionIDs}, // 使用 $in 運算子來匹配多個 permission_id
|
|
}
|
|
|
|
err := repo.DB.GetClient().Find(ctx, &result, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) Create(ctx context.Context, roles []entity.RolePermission) error {
|
|
if len(roles) == 0 {
|
|
return nil // 如果 roles 是空的,則不執行任何操作
|
|
}
|
|
|
|
now := time.Now().UTC().UnixNano()
|
|
|
|
// 將 []entity.RolePermission 轉換為 []interface{}
|
|
roleInterfaces := make([]any, 0, len(roles))
|
|
for i := range roles {
|
|
if roles[i].ID.IsZero() {
|
|
roles[i].ID = primitive.NewObjectID()
|
|
roles[i].CreateAt = now
|
|
roles[i].UpdateAt = now
|
|
}
|
|
roleInterfaces = append(roleInterfaces, roles[i])
|
|
}
|
|
|
|
_, err := repo.DB.GetClient().InsertMany(ctx, roleInterfaces)
|
|
|
|
return err
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) Delete(ctx context.Context, roleID string, permissions []string) error {
|
|
if len(permissions) == 0 {
|
|
return nil // 如果 permissions 為空,則不執行刪除操作
|
|
}
|
|
|
|
filter := bson.M{
|
|
"role_id": roleID,
|
|
"permission_id": bson.M{"$in": permissions}, // 使用 $in 刪除多個 permission_id
|
|
}
|
|
|
|
_, err := repo.DB.GetClient().DeleteMany(ctx, filter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) Index20250225UP(ctx context.Context) (*mongo.Cursor, error) {
|
|
repo.DB.PopulateMultiIndex(ctx, []string{
|
|
"role_id",
|
|
"permission_id",
|
|
}, []int32{1, 1}, true)
|
|
|
|
repo.DB.PopulateIndex(ctx, "role_id", 1, false)
|
|
repo.DB.PopulateIndex(ctx, "permission_id", 1, false)
|
|
|
|
return repo.DB.GetClient().Indexes().List(ctx)
|
|
}
|