dndex/storage/graph.go

50 lines
995 B
Go

package storage
import (
"context"
"local/whodunit/storage/entity"
"local/whodunit/storage/operator"
"go.mongodb.org/mongo-driver/bson"
)
type Graph struct {
mongo Mongo
}
func NewGraph() Graph {
mongo := NewMongo()
return Graph{
mongo: mongo,
}
}
func (g Graph) List(ctx context.Context, from ...string) ([]entity.One, error) {
filter := operator.NewFilterIn("_id", from)
ch, err := g.mongo.Find(ctx, filter)
if err != nil {
return nil, err
}
var ones []entity.One
for one := range ch {
var o entity.One
if err := bson.Unmarshal(one, &o); err != nil {
return nil, err
}
ones = append(ones, o)
}
return ones, nil
}
func (g Graph) Insert(ctx context.Context, one entity.One) error {
return g.mongo.Insert(ctx, one)
}
func (g Graph) Update(ctx context.Context, one entity.One, modify interface{}) error {
return g.mongo.Update(ctx, one, modify)
}
func (g Graph) Delete(ctx context.Context, filter interface{}) error {
return g.mongo.Delete(ctx, filter)
}