package internal import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "github.com/neo4j/neo4j-go-driver/v5/neo4j" 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}) ` params := map[string]any{ "anthrove_post_id": anthrovePost.PostID, "anthrove_rating": anthrovePost.Rating, } _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) if err != nil { return err } log.WithFields(log.Fields{ "anthrove_post_id": anthrovePost.PostID, "anthrove_post_rating": anthrovePost.Rating, }).Trace("graph: created anthrove post") 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}) RETURN postNode.post_id AS AnthrovePostID ` params := map[string]any{ "anthrove_post_id": anthrovePost.PostID, } anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) if err != nil { return nil, false, err } return anthrovePost, exists, nil } func CheckIfAnthrovePostNodeExistsBySourceURl(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl string) (*models.AnthrovePost, bool, error) { query := ` OPTIONAL MATCH (postNode:AnthrovePost)<-[:REFERENCE {url: $source_url}]-() RETURN postNode.post_id AS AnthrovePostID ` params := map[string]any{ "source_url": sourceUrl, } anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) if err != nil { return nil, false, err } return anthrovePost, exists, nil } func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, driver neo4j.DriverWithContext, sourcePostID string) (*models.AnthrovePost, bool, error) { query := ` OPTIONAL MATCH (postNode:AnthrovePost)<-[:REFERENCE {source_post_id: $source_post_id}]-() RETURN postNode.post_id AS AnthrovePostID ` params := map[string]any{ "source_post_id": sourcePostID, } anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) if err != nil { return nil, false, err } return anthrovePost, exists, nil } func executeCheckQuery(ctx context.Context, driver neo4j.DriverWithContext, query string, params map[string]any) (*models.AnthrovePost, bool, error) { var anthrovePost models.AnthrovePost result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) if err != nil { return &anthrovePost, false, err } record := result.Records anthrovePostID, isNil, err := neo4j.GetRecordValue[string](record[0], "AnthrovePostID") exists := !isNil if err != nil { return &anthrovePost, exists, err } anthrovePost.PostID = models.AnthrovePostID(anthrovePostID) log.WithFields(log.Fields{ "anthrove_post_id": anthrovePost.PostID, "anthrove_post_exists": exists, }).Trace("graph: checked if post exists") if !exists { return nil, exists, nil } return &anthrovePost, exists, nil }