From 1cce71e330842979db22b59d14d5b00c8ab36cda Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 15 May 2024 15:44:34 +0200 Subject: [PATCH] feat: added GetAllSources --- internal/source.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/graph/graph.go | 3 +++ pkg/graph/impl.go | 4 ++++ 3 files changed, 47 insertions(+) diff --git a/internal/source.go b/internal/source.go index d78ffe2..fa6e0a4 100644 --- a/internal/source.go +++ b/internal/source.go @@ -33,3 +33,43 @@ 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 + + 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), + }).Trace("graph: created tag node") + + return sources, nil +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index ac3786f..b933d11 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -107,4 +107,7 @@ type OtterSpace interface { // GetAllTags returns a list of Tags that the user hs favorites through a post GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) + + // GetAllSources returns a list of Sources in the database + GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 9a47593..573d153 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -108,6 +108,10 @@ func (g *graphConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFreq return internal.GetTags(ctx, g.driver) } +func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) { + return internal.GetAllSourceNodes(ctx, g.driver) +} + func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = internal.NewGraphLogger(graphDebug)