feat(post): added deletion of posts & added some documentation for the add function as well

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-05-31 14:42:53 +02:00
parent 6cabcf9beb
commit 6760948e73
3 changed files with 32 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus" 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 { func CreateAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *models.AnthrovePost) error {
query := ` query := `
CREATE (newPostNode:AnthrovePost {post_id: $anthrove_post_id, rating: $anthrove_rating}) 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 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) { func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) {
query := ` query := `
OPTIONAL MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) OPTIONAL MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id})

View File

@ -2,12 +2,13 @@ package graph
import ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
) )
type Post interface { type Post interface {
CreatePost(ctx context.Context, anthrovePost *models.AnthrovePost) error 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. // LinkPostWithSource establishes a link between a post and a source in the OtterSpace graph.
// It returns an error if the operation fails. // It returns an error if the operation fails.

View File

@ -2,17 +2,20 @@ package graph
import ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/internal" "git.dragse.it/anthrove/otter-space-sdk/internal"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "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 { func (g *graphConnection) CreatePost(ctx context.Context, anthrovePost *models.AnthrovePost) error {
return internal.CreateAnthrovePostNode(ctx, g.driver, anthrovePost) return internal.CreateAnthrovePostNode(ctx, g.driver, anthrovePost)
} }
func (g *graphConnection) DeletePost() error { // DeletePost will always delete a node, it only needs the anthrovePost.PostID filled out.
//TODO implement me func (g *graphConnection) DeletePost(ctx context.Context, anthrovePost *models.AnthrovePost) error {
panic("implement me") return internal.DeleteAnthrovePostNode(ctx, g.driver, anthrovePost)
} }
func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) { func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) {