diff --git a/internal/post.go b/internal/post.go index 021a841..01872f5 100644 --- a/internal/post.go +++ b/internal/post.go @@ -8,6 +8,7 @@ import ( log "github.com/sirupsen/logrus" ) +// CreateAnthrovePostNode will always create a new node, it does not check if the node with the corresponding post_id and rating exists. func CreateAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *models.AnthrovePost) error { query := ` CREATE (newPostNode:AnthrovePost {post_id: $anthrove_post_id, rating: $anthrove_rating}) @@ -31,6 +32,29 @@ func CreateAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, return nil } +// DeleteAnthrovePostNode will always delete a node, it only needs the anthrovePost.PostID filled out. +func DeleteAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *models.AnthrovePost) error { + query := ` + MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id) + DELETE anthrovePost + ` + + params := map[string]any{ + "anthrove_post_id": anthrovePost.PostID, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + log.WithFields(log.Fields{ + "anthrove_post_id": anthrovePost.PostID, + }).Trace("graph: deleted anthrove post") + + return nil +} + func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) { query := ` OPTIONAL MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) diff --git a/pkg/graph/post.go b/pkg/graph/post.go index cdb7db3..d1d27c8 100644 --- a/pkg/graph/post.go +++ b/pkg/graph/post.go @@ -2,12 +2,13 @@ package graph import ( "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" ) type Post interface { CreatePost(ctx context.Context, anthrovePost *models.AnthrovePost) error - DeletePost() error + DeletePost(ctx context.Context, anthrovePost *models.AnthrovePost) error // LinkPostWithSource establishes a link between a post and a source in the OtterSpace graph. // It returns an error if the operation fails. diff --git a/pkg/graph/post_impl.go b/pkg/graph/post_impl.go index 6331a35..36ca715 100644 --- a/pkg/graph/post_impl.go +++ b/pkg/graph/post_impl.go @@ -2,17 +2,20 @@ package graph import ( "context" + "git.dragse.it/anthrove/otter-space-sdk/internal" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" ) +// CreatePost will always create a new node, it does not check if the node with the corresponding post_id and rating exists. func (g *graphConnection) CreatePost(ctx context.Context, anthrovePost *models.AnthrovePost) error { return internal.CreateAnthrovePostNode(ctx, g.driver, anthrovePost) } -func (g *graphConnection) DeletePost() error { - //TODO implement me - panic("implement me") +// DeletePost will always delete a node, it only needs the anthrovePost.PostID filled out. +func (g *graphConnection) DeletePost(ctx context.Context, anthrovePost *models.AnthrovePost) error { + return internal.DeleteAnthrovePostNode(ctx, g.driver, anthrovePost) + } func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) {