132 lines
3.0 KiB
Go
132 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"backend/pkg/library/mongo"
|
|
"backend/pkg/permission/domain"
|
|
"backend/pkg/permission/domain/entity"
|
|
"backend/pkg/permission/domain/repository"
|
|
"context"
|
|
"errors"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"strings"
|
|
)
|
|
|
|
type PermissionRepositoryParam struct {
|
|
Conf *mongo.Conf
|
|
CacheConf cache.CacheConf
|
|
DBOpts []mon.Option
|
|
CacheOpts []cache.Option
|
|
}
|
|
|
|
type PermissionRepository struct {
|
|
DB mongo.DocumentDBWithCacheUseCase
|
|
}
|
|
|
|
func NewAccountRepository(param PermissionRepositoryParam) repository.PermissionRepository {
|
|
e := entity.Permission{}
|
|
documentDB, err := mongo.MustDocumentDBWithCache(
|
|
param.Conf,
|
|
e.CollectionName(),
|
|
param.CacheConf,
|
|
param.DBOpts,
|
|
param.CacheOpts,
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &PermissionRepository{
|
|
DB: documentDB,
|
|
}
|
|
}
|
|
|
|
func (repo *PermissionRepository) FindOne(ctx context.Context, id string) (*entity.Permission, error) {
|
|
var data entity.Permission
|
|
rk := domain.GetPermissionIDRedisKey(id)
|
|
|
|
oid, err := bson.ObjectIDFromHex(id)
|
|
if err != nil {
|
|
return nil, ErrInvalidObjectID
|
|
}
|
|
|
|
err = repo.DB.FindOne(ctx, rk, &data, bson.M{"_id": oid})
|
|
switch {
|
|
case err == nil:
|
|
return &data, nil
|
|
case errors.Is(err, mon.ErrNotFound):
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (repo *PermissionRepository) FindByName(ctx context.Context, name string) (*entity.Permission, error) {
|
|
var data entity.Permission
|
|
rk := domain.GetPermissionNameRedisKey(name)
|
|
|
|
err := repo.DB.FindOne(ctx, rk, &data, bson.M{"name": name})
|
|
switch {
|
|
case err == nil:
|
|
return &data, nil
|
|
case errors.Is(err, mon.ErrNotFound):
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (repo *PermissionRepository) GetByNames(ctx context.Context, names []string) ([]*entity.Permission, error) {
|
|
var data []*entity.Permission
|
|
|
|
filter := bson.M{
|
|
"name": bson.M{"$in": names},
|
|
}
|
|
|
|
err := repo.DB.GetClient().Find(ctx, &data, filter)
|
|
if err != nil {
|
|
if errors.Is(err, mon.ErrNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (repo *PermissionRepository) FindByHTTP(ctx context.Context, path, method string) (*entity.Permission, error) {
|
|
var perm entity.Permission
|
|
|
|
filter := bson.M{
|
|
"path": path,
|
|
"method": strings.ToUpper(method), // 確保大小寫一致
|
|
}
|
|
|
|
err := repo.DB.GetClient().FindOne(ctx, &perm, filter)
|
|
switch {
|
|
case err == nil:
|
|
return &perm, nil
|
|
case errors.Is(err, mon.ErrNotFound):
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (repo *PermissionRepository) List(ctx context.Context, filter repository.PermissionFilter) ([]*entity.Permission, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (repo *PermissionRepository) ListActive(ctx context.Context) ([]*entity.Permission, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (repo *PermissionRepository) GetChildren(ctx context.Context, parentID int64) ([]*entity.Permission, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|