Add graph.Search for substr
This commit is contained in:
@@ -2,6 +2,7 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"local/dndex/storage/entity"
|
||||
"local/dndex/storage/operator"
|
||||
|
||||
@@ -19,12 +20,25 @@ func NewGraph() Graph {
|
||||
}
|
||||
}
|
||||
|
||||
func (g Graph) Search(ctx context.Context, namespace string, nameContains string) ([]entity.One, error) {
|
||||
filter := operator.Regex{Key: entity.Name, Value: fmt.Sprintf(".*%s.*", nameContains)}
|
||||
return g.find(ctx, namespace, filter)
|
||||
}
|
||||
|
||||
func (g Graph) List(ctx context.Context, namespace string, from ...string) ([]entity.One, error) {
|
||||
filter := operator.NewFilterIn("_id", from)
|
||||
filter := operator.NewFilterIn(entity.Name, from)
|
||||
return g.find(ctx, namespace, filter)
|
||||
}
|
||||
|
||||
func (g Graph) find(ctx context.Context, namespace string, filter interface{}) ([]entity.One, error) {
|
||||
ch, err := g.mongo.Find(ctx, namespace, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g.gatherOnes(ctx, ch)
|
||||
}
|
||||
|
||||
func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One, error) {
|
||||
var ones []entity.One
|
||||
for one := range ch {
|
||||
var o entity.One
|
||||
|
||||
Reference in New Issue
Block a user