package database import ( "context" "git.dragse.it/anthrove/otter-space-sdk/internal/graph" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" "github.com/neo4j/neo4j-go-driver/v5/neo4j" "github.com/neo4j/neo4j-go-driver/v5/neo4j/config" ) type graphConnection struct { driver neo4j.DriverWithContext graphDebug bool } func NewGraphConnection(graphDebug bool) OtterSpace { return &graphConnection{ driver: nil, graphDebug: graphDebug, } } func (g *graphConnection) Connect(ctx context.Context, endpoint string, username string, password string, _ string, _ int, _ string, _ string) error { driver, err := neo4j.NewDriverWithContext(endpoint, neo4j.BasicAuth(username, password, ""), logger(g.graphDebug)) if err != nil { return err } err = driver.VerifyAuthentication(ctx, nil) if err != nil { return err } g.driver = driver return nil } func (g *graphConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { return graph.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username) } func (g *graphConnection) AddSource(ctx context.Context, anthroveSource *graphModels.AnthroveSource) error { return graph.CreateSourceNode(ctx, g.driver, anthroveSource) } func (g *graphConnection) AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error { return graph.CreateAnthrovePostNode(ctx, g.driver, anthrovePost) } func (g *graphConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error { return graph.CreateTagNodeWitRelation(ctx, g.driver, anthrovePostID, anthroveTag) } func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error { return graph.EstablishAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship) } func (g *graphConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error { return graph.EstablishUserToPostLink(ctx, g.driver, anthroveUser, anthrovePost) } func (g *graphConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { return graph.CheckUserToPostLink(ctx, g.driver, anthroveUserID, sourcePostID, sourceUrl) } func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) { return graph.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, g.driver, anthrovePost) } func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) { return graph.CheckIfAnthrovePostNodeExistsBySourceURl(ctx, g.driver, sourceUrl) } func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) { return graph.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID) } func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { return graph.GetUserFavoritesCount(ctx, g.driver, anthroveUserID) } func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) { return graph.GetUserSourceLink(ctx, g.driver, anthroveUserID) } func (g *graphConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) { return graph.GetSpecifiedUserSourceLink(ctx, g.driver, anthroveUserID, sourceDisplayName) } func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) { return graph.GetAnthroveUser(ctx, g.driver, anthroveUserID) } func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) { return graph.GetAllAnthroveUserIDs(ctx, g.driver) } func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) { return graph.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) } func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) { return graph.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID) } func (g *graphConnection) GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) { return graph.GetTags(ctx, g.driver) } func (g *graphConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) { return graph.GetAllSourceNodes(ctx, g.driver) } func (g *graphConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*graphModels.AnthroveSource, error) { return graph.GetSourceNodesByURL(ctx, g.driver, sourceUrl) } func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = graph.NewGraphLogger(graphDebug) } }