101 lines
4.1 KiB
Go
101 lines
4.1 KiB
Go
package pkg
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.dragse.it/anthrove/anthrove-graph-sdk.git/internal"
|
|
"git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models"
|
|
"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) Graph {
|
|
return &graphConnection{
|
|
driver: nil,
|
|
graphDebug: graphDebug,
|
|
}
|
|
}
|
|
|
|
func (g *graphConnection) Connect(ctx context.Context, endpoint string, username string, password 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) (*models.AnthroveUser, error) {
|
|
return internal.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username)
|
|
}
|
|
|
|
func (g *graphConnection) AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error {
|
|
return internal.CreateSourceNode(ctx, g.driver, anthroveSource)
|
|
}
|
|
|
|
func (g *graphConnection) AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error {
|
|
return internal.CreateAnthrovePostNode(ctx, g.driver, anthrovePost)
|
|
}
|
|
|
|
func (g *graphConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID *models.AnthrovePostID, anthroveTag *models.AnthroveTag) error {
|
|
return internal.CreateTagNodeWitRelation(ctx, g.driver, anthrovePostID, anthroveTag)
|
|
}
|
|
|
|
func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error {
|
|
return internal.EstablishAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship)
|
|
}
|
|
|
|
func (g *graphConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error {
|
|
return internal.EstablishUserToPostLink(ctx, g.driver, anthroveUser, anthrovePost)
|
|
}
|
|
|
|
func (g *graphConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourcePostUrl string) (bool, error) {
|
|
return internal.CheckUserToPostLink(ctx, g.driver, anthroveUserID, sourcePostID, sourcePostUrl)
|
|
}
|
|
|
|
func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) {
|
|
return internal.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, g.driver, anthrovePost)
|
|
}
|
|
|
|
func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error) {
|
|
return internal.CheckIfAnthrovePostNodeExistsBySourceURl(ctx, g.driver, sourceUrl)
|
|
}
|
|
|
|
func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) {
|
|
return internal.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID)
|
|
}
|
|
|
|
func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
return internal.GetUserFavoritesCount(ctx, g.driver, anthroveUserID)
|
|
}
|
|
|
|
func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) {
|
|
return internal.GetUserSourceLink(ctx, g.driver, anthroveUserID)
|
|
}
|
|
|
|
func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) {
|
|
return internal.GetAnthroveUser(ctx, g.driver, anthroveUserID)
|
|
}
|
|
|
|
func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func logger(graphDebug bool) func(config *config.Config) {
|
|
return func(config *config.Config) {
|
|
config.Log = internal.NewGraphLogger(graphDebug)
|
|
}
|
|
}
|