From d9f25a5a87c01d46713bc0087323b3f751bf9a5a Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 16 Feb 2024 16:16:57 +0100 Subject: [PATCH] feat: implemented tag creation & renamed function to be more clear what they actually do --- internal/tag.go | 34 ++++++++++++++++++++++++++++++++++ pkg/graph.go | 16 ++++++++-------- pkg/impl.go | 11 +++++------ 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/internal/tag.go b/internal/tag.go index 5bf0569..dcd0654 100644 --- a/internal/tag.go +++ b/internal/tag.go @@ -1 +1,35 @@ 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 CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID *models.AnthrovePostID, anthroveTag *models.AnthroveTag) error { + query := ` + MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id}) + MERGE (tagNode:Tag {name: $tag_name, type: $tag_type}) + MERGE (anthrovePost)-[:HAS]->(tagNode) + ` + params := map[string]interface{}{ + "tag_name": anthroveTag.Name, + "tag_type": anthroveTag.Type, + "anthrove_post_id": anthrovePostID, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + log.WithFields(log.Fields{ + "anthrove_post_id": anthrovePostID, + "tag_name": anthroveTag.Name, + "tag_type": anthroveTag.Type, + }).Trace("graph: created tag node") + + return nil +} diff --git a/pkg/graph.go b/pkg/graph.go index 180c501..248ce09 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -10,17 +10,17 @@ type Graph interface { // Connect sets up a connection to the endpoint using the provided username and password Connect(ctx context.Context, endpoint string, username string, password string) error - // AddUserToGraph uploads an Anthrove user to the graph - AddUserToGraph(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) + // AddUserWithRelationToSource uploads an Anthrove user to the graph + AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) - // AddSourceToGraph uploads an Anthrove source to the graph - AddSourceToGraph(ctx context.Context, anthroveSource *models.AnthroveSource) error + // AddSource uploads an Anthrove source to the graph + AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error - // AddPostToGraph uploads an Anthrove post to the graph - AddPostToGraph(ctx context.Context, anthrovePost *models.AnthrovePost) error + // AddPost uploads an Anthrove post to the graph + AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error - // AddTagToGraph uploads a tag associated with an Anthrove post to the graph - AddTagToGraph(ctx context.Context, anthrovePost *models.AnthrovePost, anthroveTag *models.AnthroveTag) error + // AddTagWithRelationToPost uploads a tag associated with an Anthrove post to the graph + 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 diff --git a/pkg/impl.go b/pkg/impl.go index 8d2e8e2..92763c1 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -36,21 +36,20 @@ func (g *graphConnection) Connect(ctx context.Context, endpoint string, username return nil } -func (g *graphConnection) AddUserToGraph(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) { +func (g *graphConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) { return internal.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username) } -func (g *graphConnection) AddSourceToGraph(ctx context.Context, anthroveSource *models.AnthroveSource) error { +func (g *graphConnection) AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error { return internal.CreateSourceNode(ctx, g.driver, anthroveSource) } -func (g *graphConnection) AddPostToGraph(ctx context.Context, anthrovePost *models.AnthrovePost) error { +func (g *graphConnection) AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error { return internal.CreateAnthrovePostNode(ctx, g.driver, anthrovePost) } -func (g *graphConnection) AddTagToGraph(ctx context.Context, anthrovePost *models.AnthrovePost, anthroveTag *models.AnthroveTag) error { - //TODO implement me - panic("implement me") +func (g *graphConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID *models.AnthrovePostID, anthroveTag *models.AnthroveTag) error { + 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 {