From 924416dd06d3498b00af19a7a66997c9420cbd54 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 16 Feb 2024 17:21:26 +0100 Subject: [PATCH] feat: implemented post to source relationship creation --- internal/relationships.go | 37 +++++++++++++++++++++++++++++++++++++ pkg/graph.go | 2 +- pkg/impl.go | 5 ++--- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/internal/relationships.go b/internal/relationships.go index 5bf0569..e587587 100644 --- a/internal/relationships.go +++ b/internal/relationships.go @@ -1 +1,38 @@ package internal + +import ( + "context" + + "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" + log "github.com/sirupsen/logrus" +) + +func EstablishAnthrovePostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error { + query := ` + MATCH (sourceNode:Source {domain: $source_url}) + MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) + MERGE (sourceNode)-[:REFERENCE {url: $source_post_url, source_post_id: $source_post_id}]->(postNode) + ` + + params := map[string]any{ + "source_url": anthroveSourceDomain, + "anthrove_post_id": anthrovePostID, + "source_post_url": anthrovePostRelationship.Url, + "source_post_id": anthrovePostRelationship.PostID, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + log.WithFields(log.Fields{ + "source_url": anthroveSourceDomain, + "anthrove_post_id": anthrovePostID, + "source_post_url": anthrovePostRelationship.Url, + "source_post_id": anthrovePostRelationship.PostID, + }).Trace("graph: creating anthrove post to source link") + + return nil +} diff --git a/pkg/graph.go b/pkg/graph.go index 248ce09..9a5dcb9 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -23,7 +23,7 @@ type Graph interface { AddTagWithRelationToPost(ctx context.Context, anthrovePostID *models.AnthrovePostID, anthroveTag *models.AnthroveTag) error // LinkPostWithSource establishes a link between a post and a source in the graph - LinkPostWithSource(ctx context.Context, anthrovePost *models.AnthrovePost, anthrovePostRelationship *models.AnthrovePostRelationship, anthroveSource *models.AnthroveSource) error + LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error // LinkUserWithPost establishes a link between a user and a post in the graph LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error diff --git a/pkg/impl.go b/pkg/impl.go index 92763c1..9afe702 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -52,9 +52,8 @@ func (g *graphConnection) AddTagWithRelationToPost(ctx context.Context, anthrove return internal.CreateTagNodeWitRelation(ctx, g.driver, anthrovePostID, anthroveTag) } -func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePost *models.AnthrovePost, anthrovePostRelationship *models.AnthrovePostRelationship, anthroveSource *models.AnthroveSource) error { - //TODO implement me - panic("implement me") +func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error { + return internal.EstablishAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship) } func (g *graphConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error {