feat: added GetSourceByURL #5

Merged
fenpaws merged 1 commits from more-querys into main 2024-05-21 09:48:56 +00:00
3 changed files with 45 additions and 0 deletions
Showing only changes of commit 604efc504f - Show all commits

View File

@ -33,6 +33,7 @@ func CreateSourceNode(ctx context.Context, driver neo4j.DriverWithContext, anthr
return nil return nil
} }
func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveSource, error) { func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveSource, error) {
var sources []models.AnthroveSource var sources []models.AnthroveSource
@ -73,3 +74,40 @@ func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]m
return sources, nil 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
}

View File

@ -110,4 +110,7 @@ type OtterSpace interface {
// GetAllSources returns a list of Sources in the database // GetAllSources returns a list of Sources in the database
GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) 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)
} }

View File

@ -112,6 +112,10 @@ func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveS
return internal.GetAllSourceNodes(ctx, g.driver) 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) { func logger(graphDebug bool) func(config *config.Config) {
return func(config *config.Config) { return func(config *config.Config) {
config.Log = internal.NewGraphLogger(graphDebug) config.Log = internal.NewGraphLogger(graphDebug)