99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"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"
|
|
"context"
|
|
"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"
|
|
"time"
|
|
)
|
|
|
|
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, role entity.RolePermission) error {
|
|
if role.ID.IsZero() {
|
|
now := time.Now().UTC().UnixNano()
|
|
role.ID = primitive.NewObjectID()
|
|
role.CreateAt = now
|
|
role.UpdateAt = now
|
|
}
|
|
|
|
_, err := repo.DB.GetClient().InsertOne(ctx, role)
|
|
|
|
return err
|
|
}
|
|
|
|
func (repo *RolePermissionRepository) Delete(ctx context.Context, roleID string, permission string) error {
|
|
filter := bson.M{
|
|
"role_id": roleID,
|
|
"permission_id": permission,
|
|
}
|
|
|
|
_, err := repo.DB.GetClient().DeleteOne(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)
|
|
}
|