2024-06-03 17:32:25 +00:00
|
|
|
package graph
|
2024-02-16 15:00:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-16 20:51:09 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-02-16 15:00:14 +00:00
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateSourceNode(ctx context.Context, driver neo4j.DriverWithContext, anthroveSource *models.AnthroveSource) error {
|
|
|
|
query := `
|
|
|
|
MERGE (sourceNode:Source {domain: $source_url})
|
|
|
|
ON CREATE SET sourceNode.domain = $source_url, sourceNode.display_name = $source_display_name, sourceNode.icon = $source_icon
|
|
|
|
`
|
|
|
|
params := map[string]any{
|
|
|
|
"source_url": anthroveSource.Domain,
|
|
|
|
"source_display_name": anthroveSource.DisplayName,
|
|
|
|
"source_icon": anthroveSource.Icon,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
2024-06-03 17:31:25 +00:00
|
|
|
return fmt.Errorf("database: %w", err)
|
2024-02-16 15:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"node_source_url": anthroveSource.Domain,
|
|
|
|
"node_source_displayName": anthroveSource.DisplayName,
|
|
|
|
"node_source_icon": anthroveSource.Icon,
|
2024-06-03 17:31:25 +00:00
|
|
|
}).Trace("database: created source node")
|
2024-02-16 15:00:14 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-21 09:48:03 +00:00
|
|
|
|
2024-05-15 13:44:34 +00:00
|
|
|
func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveSource, error) {
|
|
|
|
var sources []models.AnthroveSource
|
|
|
|
|
|
|
|
query := `
|
|
|
|
MATCH (s:Source)
|
|
|
|
RETURN s as source
|
|
|
|
`
|
|
|
|
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]
|
|
|
|
|
|
|
|
source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "source")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sources = append(sources, models.AnthroveSource{
|
|
|
|
DisplayName: source.Props["display_name"].(string),
|
|
|
|
Domain: source.Props["domain"].(string),
|
|
|
|
Icon: source.Props["icon"].(string),
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_amount": len(sources),
|
2024-06-03 17:31:25 +00:00
|
|
|
}).Trace("database: created tag node")
|
2024-05-15 13:44:34 +00:00
|
|
|
|
|
|
|
return sources, nil
|
|
|
|
}
|
2024-05-21 09:48:03 +00:00
|
|
|
|
|
|
|
func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl string) (*models.AnthroveSource, error) {
|
|
|
|
|
|
|
|
var source models.AnthroveSource
|
|
|
|
|
|
|
|
query := `
|
|
|
|
MATCH (s:Source {domain: $source_url})
|
|
|
|
RETURN s as source
|
|
|
|
`
|
|
|
|
params := map[string]any{
|
|
|
|
"source_url": sourceUrl,
|
|
|
|
}
|
|
|
|
|
|
|
|
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("source not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
record, _, err := neo4j.GetRecordValue[neo4j.Node](result.Records[0], "source")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
source.DisplayName = record.Props["display_name"].(string)
|
|
|
|
source.Domain = record.Props["domain"].(string)
|
|
|
|
source.Icon = record.Props["icon"].(string)
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"source_url": sourceUrl,
|
2024-06-03 17:31:25 +00:00
|
|
|
}).Trace("database: got source node")
|
2024-05-21 09:48:03 +00:00
|
|
|
|
|
|
|
return &source, nil
|
|
|
|
}
|