feat: added GetUserFavoritePostsWithPagination function

This commit is contained in:
SoXX 2024-05-10 10:57:51 +02:00
parent f54d7244c5
commit 302b4b5f9c
3 changed files with 56 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)