diff --git a/storage/driver/boltdb_test.go b/storage/driver/boltdb_test.go index 04c1a2d..1a73627 100644 --- a/storage/driver/boltdb_test.go +++ b/storage/driver/boltdb_test.go @@ -129,6 +129,9 @@ func TestBoltDBFind(t *testing.T) { if o.Modified == 0 { t.Error(o.Modified) } + if len(o.Attachments) == 0 { + t.Error(o.Attachments) + } if len(o.Connections) == 0 { t.Error(o.Connections) } @@ -367,6 +370,12 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) { return err } for i := 0; i < testN; i++ { + p := entity.One{ + Name: "name-" + uuid.New().String()[:5], + Type: "type-" + uuid.New().String()[:5], + Relationship: "rshp-" + uuid.New().String()[:5], + Title: "titl-" + uuid.New().String()[:5], + } o := entity.One{ Name: "name-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5], @@ -374,15 +383,9 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) { Image: "imge-" + uuid.New().String()[:5], Text: "text-" + uuid.New().String()[:5], Modified: time.Now().UnixNano(), - Connections: map[string]entity.One{}, + Connections: map[string]entity.One{p.Name: p}, + Attachments: map[string]string{"filename": "/path/to/file"}, } - p := entity.One{ - Name: "name-" + uuid.New().String()[:5], - Type: "type-" + uuid.New().String()[:5], - Relationship: "rshp-" + uuid.New().String()[:5], - Title: "titl-" + uuid.New().String()[:5], - } - o.Connections[p.Name] = p b, err := bson.Marshal(o) if err != nil { return err diff --git a/storage/entity/one.go b/storage/entity/one.go index 2756595..50d942e 100644 --- a/storage/entity/one.go +++ b/storage/entity/one.go @@ -18,17 +18,19 @@ const ( Text = "text" Modified = "modified" Connections = "connections" + Attachments = "attachments" ) type One struct { - Name string `bson:"_id,omitempty" json:"name,omitempty"` - Type string `bson:"type,omitempty" json:"type,omitempty"` - Title string `bson:"title,omitempty" json:"title,omitempty"` - Image string `bson:"image,omitempty" json:"image,omitempty"` - Text string `bson:"text,omitempty" json:"text,omitempty"` - Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"` - Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"` - Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"` + Name string `bson:"_id,omitempty" json:"name,omitempty"` + Type string `bson:"type,omitempty" json:"type,omitempty"` + Title string `bson:"title,omitempty" json:"title,omitempty"` + Image string `bson:"image,omitempty" json:"image,omitempty"` + Text string `bson:"text,omitempty" json:"text,omitempty"` + Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"` + Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"` + Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"` + Attachments map[string]string `bson:"attachments,omitempty" json:"attachments,omitempty"` } func (o One) Query() One { diff --git a/storage/entity/one_test.go b/storage/entity/one_test.go index 667f9d7..ceb852e 100644 --- a/storage/entity/one_test.go +++ b/storage/entity/one_test.go @@ -28,10 +28,13 @@ func TestOneMarshalBSON(t *testing.T) { one: (One{Name: "hello", Type: "world", Modified: 1}).Query(), }, "modified changes": { - one: (One{Name: "hello", Type: "world", Modified: 1}), + one: One{Name: "hello", Type: "world", Modified: 1}, }, "w/ connections": { - one: (One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}}), + one: One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}}, + }, + "w/ attachments": { + one: One{Name: "hello", Type: "world", Modified: 1, Attachments: map[string]string{"hello": "/world"}}, }, } diff --git a/storage/graph_test.go b/storage/graph_test.go index 33e1dac..bef7502 100644 --- a/storage/graph_test.go +++ b/storage/graph_test.go @@ -140,6 +140,46 @@ func TestIntegration(t *testing.T) { t.Fatal(some2[0].Peers()) } }) + + t.Run("graph.Update(new attachment), Update(--new attachment)", func(t *testing.T) { + err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{Key: fmt.Sprintf("%s.new attachment", entity.Attachments), Value: "my new attachment"}) + if err != nil { + t.Fatal(err) + } + + some1, err := graph.List(ctx, "col", ones[0].Name) + if err != nil { + t.Fatal(err) + } + t.Logf("sm1 = %+v", some1[0]) + if len(some1) != 1 { + t.Fatal(len(some1)) + } + if v, ok := some1[0].Attachments["new attachment"]; !ok { + t.Fatal(ok, some1[0].Attachments) + } else if v != "my new attachment" { + t.Fatal(v, some1[0].Attachments) + } + + err = graph.Update(ctx, "col", ones[0].Query(), operator.Unset(fmt.Sprintf("%s.new attachment", entity.Attachments))) + if err != nil { + t.Fatal(err) + } + + some2, err := graph.List(ctx, "col", ones[0].Name) + if err != nil { + t.Fatal(err) + } + t.Logf("sm1 = %+v", some2[0]) + if len(some2) != 1 { + t.Fatal(len(some2)) + } + if _, ok := some2[0].Attachments["new attachment"]; ok { + t.Fatal(ok, some2[0].Attachments) + } else if len(some2[0].Attachments) == 0 { + t.Fatal(len(some2[0].Attachments), some2[0].Attachments) + } + }) } func randomOne() entity.One { @@ -151,5 +191,9 @@ func randomOne() entity.One { Text: "tee hee xd", Modified: time.Now().UnixNano(), Connections: map[string]entity.One{}, + Attachments: map[string]string{ + "pdf file": "/path/to.pdf", + "png file": "/path/to.png", + }, } } diff --git a/view/who.go b/view/who.go index 53b58bc..a377ef2 100644 --- a/view/who.go +++ b/view/who.go @@ -3,6 +3,7 @@ package view import ( "encoding/json" "errors" + "fmt" "io/ioutil" "local/dndex/storage" "local/dndex/storage/entity" @@ -176,7 +177,7 @@ func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http. return err } one.Relationship = relationship - if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: "connections." + one.Name, Value: one.Peer()}); err != nil { + if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: fmt.Sprintf("%s.%s", entity.Connections, one.Name), Value: one.Peer()}); err != nil { return err }