This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/pkg/database/graph_impl.go

124 lines
5.2 KiB
Go
Raw Normal View History

package database
2024-02-16 14:16:50 +00:00
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/internal/graph"
2024-02-16 14:16:50 +00:00
2024-02-16 20:51:09 +00:00
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
2024-02-16 14:16:50 +00:00
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
2024-02-16 14:54:36 +00:00
"github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
2024-02-16 14:16:50 +00:00
)
type graphConnection struct {
driver neo4j.DriverWithContext
2024-02-16 14:54:36 +00:00
graphDebug bool
2024-02-16 14:16:50 +00:00
}
func NewGraphConnection(graphDebug bool) OtterSpace {
2024-02-16 14:16:50 +00:00
return &graphConnection{
driver: nil,
2024-02-16 14:54:36 +00:00
graphDebug: graphDebug,
2024-02-16 14:16:50 +00:00
}
}
func (g *graphConnection) Connect(ctx context.Context, endpoint string, username string, password string) error {
2024-02-16 14:54:36 +00:00
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
2024-02-16 14:16:50 +00:00
}
2024-02-17 15:36:04 +00:00
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)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error {
return graph.CreateSourceNode(ctx, g.driver, anthroveSource)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error {
return graph.CreateAnthrovePostNode(ctx, g.driver, anthrovePost)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.AnthroveTag) error {
return graph.CreateTagNodeWitRelation(ctx, g.driver, anthrovePostID, anthroveTag)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error {
return graph.EstablishAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error {
return graph.EstablishUserToPostLink(ctx, g.driver, anthroveUser, anthrovePost)
2024-02-16 14:16:50 +00:00
}
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)
2024-02-16 14:16:50 +00:00
}
2024-02-16 20:19:27 +00:00
func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) {
return graph.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, g.driver, anthrovePost)
2024-02-16 14:16:50 +00:00
}
2024-02-16 20:19:27 +00:00
func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error) {
return graph.CheckIfAnthrovePostNodeExistsBySourceURl(ctx, g.driver, sourceUrl)
2024-02-16 14:16:50 +00:00
}
2024-02-16 20:19:27 +00:00
func (g *graphConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) {
return graph.CheckIfAnthrovePostNodeExistsBySourceID(ctx, g.driver, sourcePostID)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
return graph.GetUserFavoritesCount(ctx, g.driver, anthroveUserID)
2024-02-16 14:16:50 +00:00
}
2024-02-16 20:33:46 +00:00
func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) {
return graph.GetUserSourceLink(ctx, g.driver, anthroveUserID)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) {
return graph.GetSpecifiedUserSourceLink(ctx, g.driver, anthroveUserID, sourceDisplayName)
}
func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) {
return graph.GetAnthroveUser(ctx, g.driver, anthroveUserID)
2024-02-16 14:16:50 +00:00
}
func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
return graph.GetAllAnthroveUserIDs(ctx, g.driver)
2024-02-16 14:16:50 +00:00
}
2024-02-16 14:54:36 +00:00
func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
return graph.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit)
}
2024-05-15 13:31:17 +00:00
func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return graph.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID)
2024-05-15 13:31:17 +00:00
}
2024-05-15 13:31:17 +00:00
func (g *graphConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) {
return graph.GetTags(ctx, g.driver)
}
2024-05-15 13:44:34 +00:00
func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) {
return graph.GetAllSourceNodes(ctx, g.driver)
2024-05-15 13:44:34 +00:00
}
2024-05-21 09:48:03 +00:00
func (g *graphConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.AnthroveSource, error) {
return graph.GetSourceNodesByURL(ctx, g.driver, sourceUrl)
2024-05-21 09:48:03 +00:00
}
2024-02-16 14:54:36 +00:00
func logger(graphDebug bool) func(config *config.Config) {
return func(config *config.Config) {
config.Log = graph.NewGraphLogger(graphDebug)
2024-02-16 14:54:36 +00:00
}
}