feat: added GetTags

This commit is contained in:
SoXX 2024-05-15 15:31:17 +02:00
parent 4db345a444
commit 9f2ebe90ee
5 changed files with 61 additions and 6 deletions

View File

@ -33,3 +33,52 @@ func CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContex
return nil return nil
} }
func GetTags(ctx context.Context, driver neo4j.DriverWithContext) ([]models.TagsWithFrequency, error) {
var userTags []models.TagsWithFrequency
query := `
MATCH (:AnthrovePost)-[:HAS]->(t:Tag)
RETURN t as tag, COUNT(t) AS frequency
ORDER BY frequency DESC
`
params := map[string]any{}
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.TagsWithFrequency{
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
}

View File

@ -402,8 +402,8 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverW
} }
func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
var userTags []models.UserTagsTroughFavedPost var userTags []models.TagsWithFrequency
query := ` query := `
MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag) MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag)
@ -436,7 +436,7 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.Dri
return nil, err return nil, err
} }
userTags = append(userTags, models.UserTagsTroughFavedPost{ userTags = append(userTags, models.TagsWithFrequency{
Frequency: frequency, Frequency: frequency,
Tags: models.AnthroveTag{ Tags: models.AnthroveTag{
Name: tag.Props["name"].(string), Name: tag.Props["name"].(string),

View File

@ -103,5 +103,8 @@ type OtterSpace interface {
GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) 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 returns a list of Tags that the user hs favorites through a post
GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error)
// GetAllTags returns a list of Tags that the user hs favorites through a post
GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error)
} }

View File

@ -100,9 +100,12 @@ func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context
return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit)
} }
func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return internal.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID) return internal.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID)
}
func (g *graphConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) {
return internal.GetTags(ctx, g.driver)
} }
func logger(graphDebug bool) func(config *config.Config) { func logger(graphDebug bool) func(config *config.Config) {

View File

@ -14,7 +14,7 @@ type FavoriteList struct {
Posts []FavoritePost `json:"posts,omitempty"` Posts []FavoritePost `json:"posts,omitempty"`
} }
type UserTagsTroughFavedPost struct { type TagsWithFrequency struct {
Frequency int64 `json:"frequency"` Frequency int64 `json:"frequency"`
Tags AnthroveTag `json:"tags"` Tags AnthroveTag `json:"tags"`
} }