diff --git a/internal/user.go b/internal/user.go index a65200e..3df4d74 100644 --- a/internal/user.go +++ b/internal/user.go @@ -295,7 +295,6 @@ func GetAllAnthroveUserIDs(ctx context.Context, driver neo4j.DriverWithContext) if len(result.Records) == 0 { log.Warnf("No users found, this should not be happening!") return []models.AnthroveUserID{}, nil - } for i := range result.Records { @@ -402,3 +401,54 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverW return &models.FavoriteList{Posts: favoritePosts}, nil } + +func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { + var userTags []models.UserTagsTroughFavedPost + + query := ` + MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag) + RETURN t as tag, COUNT(t) AS frequency + ORDER BY frequency DESC + ` + 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, nil + } + + for i := range result.Records { + record := result.Records[i] + + tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag") + if err != nil { + return nil, err + } + + frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency") + if err != nil { + return nil, err + } + + userTags = append(userTags, models.UserTagsTroughFavedPost{ + Frequency: frequency, + Tags: models.AnthroveTag{ + Name: tag.Props["name"].(string), + Type: tag.Props["type"].(string), + }, + }) + + } + + log.WithFields(log.Fields{ + "tag_amount": len(userTags), + }).Trace("graph: created tag node") + + return userTags, nil +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index a824319..8acb1da 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -101,4 +101,7 @@ type OtterSpace interface { // GetUserFavoritePostsWithPagination gets all user favorites with relation and sources for the given user GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) + + // GetUserTagsTroughFavedPosts returns a list of Tags that the user hs favorites through a post + GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 4db1b3f..5f77618 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -100,6 +100,11 @@ func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) } +func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { + return internal.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID) + +} + func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = internal.NewGraphLogger(graphDebug) diff --git a/pkg/models/api.go b/pkg/models/api.go index 0c2da29..772a327 100644 --- a/pkg/models/api.go +++ b/pkg/models/api.go @@ -13,3 +13,8 @@ type FavoritePost struct { type FavoriteList struct { Posts []FavoritePost `json:"posts,omitempty"` } + +type UserTagsTroughFavedPost struct { + Frequency int64 `json:"frequency"` + Tags AnthroveTag `json:"tags"` +}