From 604efc504f30f80d7fdcc2d2b77bb229f99980c2 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 21 May 2024 11:48:03 +0200 Subject: [PATCH] feat: added GetSourceByURL --- internal/source.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/graph/graph.go | 3 +++ pkg/graph/impl.go | 4 ++++ 3 files changed, 45 insertions(+) diff --git a/internal/source.go b/internal/source.go index fa6e0a4..bb91fdf 100644 --- a/internal/source.go +++ b/internal/source.go @@ -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 +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index b933d11..3cd7630 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -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) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 573d153..6b1b3bb 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -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) -- 2.45.2