feat: implemented getting user favorite count

This commit is contained in:
SoXX 2024-02-16 21:28:26 +01:00
parent 6930f05e0f
commit 9a96e16e63
3 changed files with 94 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package internal
import ( import (
"context" "context"
"fmt"
"git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models"
"github.com/neo4j/neo4j-go-driver/v5/neo4j" "github.com/neo4j/neo4j-go-driver/v5/neo4j"
@ -53,3 +54,93 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWi
return &anthroveUser, nil return &anthroveUser, nil
} }
func GetUserFavoritesCount(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (int64, error) {
var userFavoriteCount int64
query := `
MATCH (userNode:User {user_id: anthrove_user_id})
MATCH (userNode)-[:FAV]->(favPost:AnthrovePost)
MATCH (sourceNode)-[:REFERENCE]->(favPost)
RETURN count(favPost) AS FavoritePostsCount
`
params := map[string]any{
"anthrove_user_id": anthroveUserID,
}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil {
return 0, err
}
if len(result.Records) == 0 {
// no matches -> user does not exist, return count 0
return userFavoriteCount, err
}
record := result.Records[0]
userFavoriteCount, _, err = neo4j.GetRecordValue[int64](record, "FavoritePostsCount")
if err != nil {
return userFavoriteCount, err
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_user_fav_count": userFavoriteCount,
}).Trace("graph: get user favorite count")
return userFavoriteCount, nil
}
func GetUserWithRelationsAndSource(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) {
userSource := make(map[string]models.AnthroveUserRelationship)
query := `
MATCH (user:User{user_id: $anthrove_user_id})-[r:HAS_ACCOUNT_AT]->(s:Source)
RETURN toString(r.user_id) AS sourceUserID, toString(r.username) AS sourceUsername, s.display_name as sourceDisplayName;
`
params := map[string]any{
"anthrove_user_id": anthroveUserID,
}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil {
return nil, err
}
if len(result.Records) == 0 {
return nil, fmt.Errorf("user has no relations")
}
for i := range result.Records {
record := result.Records[i]
sourceName, _, err := neo4j.GetRecordValue[string](record, "sourceDisplayName")
if err != nil {
return nil, err
}
sourceUserID, _, err := neo4j.GetRecordValue[string](record, "sourceUserID")
if err != nil {
return nil, err
}
sourceUsername, _, err := neo4j.GetRecordValue[string](record, "sourceUsername")
if err != nil {
return nil, err
}
anthroveSourceUser := models.AnthroveUserRelationship{
UserID: sourceUserID,
Username: sourceUsername,
}
userSource[sourceName] = anthroveSourceUser
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_data": userSource,
}).Trace("graph: get user favorite count")
return userSource, nil
}

View File

@ -41,7 +41,7 @@ type Graph interface {
CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error)
// GetUserFavoriteCount retrieves the count of user's favorite posts // GetUserFavoriteCount retrieves the count of user's favorite posts
GetUserFavoriteCount(ctx context.Context, anthroveUser *models.AnthroveUser) (int64, error) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error)
// GetUserSourceLinks retrieves the links between a user and sources in the graph // GetUserSourceLinks retrieves the links between a user and sources in the graph
GetUserSourceLinks(ctx context.Context, anthroveUser *models.AnthroveUser) (map[string]models.AnthroveUserRelationship, error) GetUserSourceLinks(ctx context.Context, anthroveUser *models.AnthroveUser) (map[string]models.AnthroveUserRelationship, error)

View File

@ -76,9 +76,8 @@ func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sou
return internal.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID) return internal.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID)
} }
func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUser *models.AnthroveUser) (int64, error) { func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
//TODO implement me return internal.GetUserFavoritesCount(ctx, g.driver, anthroveUserID)
panic("implement me")
} }
func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUser *models.AnthroveUser) (map[string]models.AnthroveUserRelationship, error) { func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUser *models.AnthroveUser) (map[string]models.AnthroveUserRelationship, error) {