feat: added GetSourceByURL
This commit is contained in:
parent
1cce71e330
commit
604efc504f
@ -33,6 +33,7 @@ func CreateSourceNode(ctx context.Context, driver neo4j.DriverWithContext, anthr
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveSource, error) {
|
||||
var sources []models.AnthroveSource
|
||||
|
||||
@ -73,3 +74,40 @@ func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]m
|
||||
|
||||
return sources, nil
|
||||
}
|
||||
|
||||
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,
|
||||
}).Trace("graph: got source node")
|
||||
|
||||
return &source, nil
|
||||
}
|
||||
|
@ -110,4 +110,7 @@ type OtterSpace interface {
|
||||
|
||||
// GetAllSources returns a list of Sources in the database
|
||||
GetAllSources(ctx context.Context) ([]models.AnthroveSource, error)
|
||||
|
||||
// GetSourceByURL returns the Source Node based on the URL
|
||||
GetSourceByURL(ctx context.Context, sourceUrl string) (*models.AnthroveSource, error)
|
||||
}
|
||||
|
@ -112,6 +112,10 @@ func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveS
|
||||
return internal.GetAllSourceNodes(ctx, g.driver)
|
||||
}
|
||||
|
||||
func (g *graphConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.AnthroveSource, error) {
|
||||
return internal.GetSourceNodesByURL(ctx, g.driver, sourceUrl)
|
||||
}
|
||||
|
||||
func logger(graphDebug bool) func(config *config.Config) {
|
||||
return func(config *config.Config) {
|
||||
config.Log = internal.NewGraphLogger(graphDebug)
|
||||
|
Reference in New Issue
Block a user