85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
"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 *models.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
|
|
}
|
|
|
|
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("database: created tag node")
|
|
|
|
return userTags, nil
|
|
}
|