From 302b4b5f9cedb7f5201873a4648a45f4d88118f1 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 10 May 2024 10:57:51 +0200 Subject: [PATCH] feat: added GetUserFavoritePostsWithPagination function --- internal/user.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ pkg/graph/graph.go | 3 +++ pkg/graph/impl.go | 4 ++++ 3 files changed, 56 insertions(+) diff --git a/internal/user.go b/internal/user.go index 89c9aa2..377efcf 100644 --- a/internal/user.go +++ b/internal/user.go @@ -315,3 +315,52 @@ func GetAllAnthroveUserIDs(ctx context.Context, driver neo4j.DriverWithContext) return anthroveUsers, nil } + +func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error) { + var err error + var anthrovePosts []models.AnthrovePost + + query := ` + MATCH (user:User{user_id: $anthrove_user_id})-[r:FAV]->(p:AnthrovePost) + RETURN p as anthrovePost + ORDER BY id(r) DESC + SKIP $skip + LIMIT $limit; + ` + params := map[string]any{ + "anthrove_user_id": anthroveUserID, + "limit": limit, + "skip": skip, + } + + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return nil, err + } + + if len(result.Records) == 0 { + return nil, nil + } + + for i := range result.Records { + record := result.Records[i] + + post, _, err := neo4j.GetRecordValue[neo4j.Node](record, "anthrovePost") + if err != nil { + return nil, err + } + + anthrovePosts = append(anthrovePosts, models.AnthrovePost{ + PostID: models.AnthrovePostID(post.Props["post_id"].(string)), + Rating: models.AnthroveRating(post.Props["rating"].(string)), + }) + + } + + log.WithFields(log.Fields{ + "anthrove_user_fav_count": len(anthrovePosts), + }).Trace("graph: got al anthrove user favorites") + + return anthrovePosts, nil + +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index 140b8cd..17666cb 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -97,4 +97,7 @@ type OtterSpace interface { // GetAllAnthroveUserIDs retrieves all user IDs from the OtterSpace graph. // It returns a slice of user IDs and an error if the operation fails. GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) + + //TODO + GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index ca5fd15..3522a7e 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -96,6 +96,10 @@ func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.A return internal.GetAllAnthroveUserIDs(ctx, g.driver) } +func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error) { + return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) +} + func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = internal.NewGraphLogger(graphDebug)