This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/internal/graph/tag.go

85 lines
2.1 KiB
Go
Raw Normal View History

package graph
import (
"context"
2024-02-16 20:51:09 +00:00
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
log "github.com/sirupsen/logrus"
)
func CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error {
query := `
MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id})
MERGE (tagNode:Tag {name: $tag_name, type: $tag_type})
MERGE (anthrovePost)-[:HAS]->(tagNode)
`
params := map[string]interface{}{
"tag_name": anthroveTag.Name,
"tag_type": anthroveTag.Type,
"anthrove_post_id": anthrovePostID,
}
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil {
return err
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"tag_name": anthroveTag.Name,
"tag_type": anthroveTag.Type,
}).Trace("database: created tag node")
return nil
}
2024-05-15 13:31:17 +00:00
func GetTags(ctx context.Context, driver neo4j.DriverWithContext) ([]graphModels.TagsWithFrequency, error) {
var userTags []graphModels.TagsWithFrequency
2024-05-15 13:31:17 +00:00
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, graphModels.TagsWithFrequency{
2024-05-15 13:31:17 +00:00
Frequency: frequency,
Tags: graphModels.AnthroveTag{
2024-05-15 13:31:17 +00:00
Name: tag.Props["name"].(string),
Type: tag.Props["type"].(string),
},
})
}
log.WithFields(log.Fields{
"tag_amount": len(userTags),
}).Trace("database: created tag node")
2024-05-15 13:31:17 +00:00
return userTags, nil
}