2024-02-16 14:54:36 +00:00
|
|
|
package internal
|
2024-02-16 15:05:05 +00:00
|
|
|
|
|
|
|
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 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
|
|
|
|
}
|
2024-02-16 20:19:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|