From 6930f05e0f3b3da25c3e24a8053eadebe5a0cafc Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 16 Feb 2024 21:19:27 +0100 Subject: [PATCH] feat: implemented post exists check --- internal/post.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/graph.go | 6 ++-- pkg/impl.go | 15 ++++----- 3 files changed, 93 insertions(+), 12 deletions(-) diff --git a/internal/post.go b/internal/post.go index 254cd78..731f39c 100644 --- a/internal/post.go +++ b/internal/post.go @@ -30,3 +30,87 @@ func CreateAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, 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 +} diff --git a/pkg/graph.go b/pkg/graph.go index fe3c66d..89e6f60 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -32,13 +32,13 @@ type Graph interface { CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourcePostUrl string) (bool, error) // CheckPostNodeExistsByAnthroveID checks if an Anthrove post node exists in the graph by its Anthrove ID - CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (bool, error) + CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) // CheckPostNodeExistsBySourceURL checks if an Anthrove post node exists in the graph by its source URL - CheckPostNodeExistsBySourceURL(ctx context.Context, anthroveSource *models.AnthroveSource) (bool, error) + CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error) // CheckPostNodeExistsBySourceID checks if an Anthrove post node exists in the graph by its source ID - CheckPostNodeExistsBySourceID(ctx context.Context, anthroveSource *models.AnthroveSource) (bool, error) + CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) // GetUserFavoriteCount retrieves the count of user's favorite posts GetUserFavoriteCount(ctx context.Context, anthroveUser *models.AnthroveUser) (int64, error) diff --git a/pkg/impl.go b/pkg/impl.go index 07127c4..2455cb6 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -64,19 +64,16 @@ func (g *graphConnection) CheckUserPostLink(ctx context.Context, anthroveUserID return internal.CheckUserToPostLink(ctx, g.driver, anthroveUserID, sourcePostID, sourcePostUrl) } -func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (bool, error) { - //TODO implement me - panic("implement me") +func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) { + return internal.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, g.driver, anthrovePost) } -func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, anthroveSource *models.AnthroveSource) (bool, error) { - //TODO implement me - panic("implement me") +func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error) { + return internal.CheckIfAnthrovePostNodeExistsBySourceURl(ctx, g.driver, sourceUrl) } -func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, anthroveSource *models.AnthroveSource) (bool, error) { - //TODO implement me - panic("implement me") +func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) { + return internal.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID) } func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUser *models.AnthroveUser) (int64, error) {