Compare commits

..

No commits in common. "1679c104bdad7169ccd178422976765f0f5e92d8" and "7fdc5d15c7f85a280ee420d76932dae6dc66535b" have entirely different histories.

3 changed files with 26 additions and 63 deletions

View File

@ -10,7 +10,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) {
query := ` query := `
MATCH (userNode:User {user_id: $anthrove_user_id}) MATCH (userNode:User {user_id: $anthrove_user_id})
MATCH (sourceNode:Source {domain: $source_domain}) MATCH (sourceNode:Source {domain: $source_domain})
@ -25,7 +25,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWi
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
if err != nil { if err != nil {
return err return nil, err
} }
var anthroveUserRelationship []models.AnthroveUserRelationship var anthroveUserRelationship []models.AnthroveUserRelationship
@ -41,6 +41,11 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWi
}, },
}) })
anthroveUser := models.AnthroveUser{
UserID: anthroveUserID,
Relationship: anthroveUserRelationship,
}
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_user_id": userID, "source_user_id": userID,
@ -48,7 +53,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWi
"source_domain": sourceDomain, "source_domain": sourceDomain,
}).Trace("graph: crated user with relationship") }).Trace("graph: crated user with relationship")
return nil return &anthroveUser, nil
} }
func GetUserFavoritesCount(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (int64, error) { func GetUserFavoritesCount(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (int64, error) {

View File

@ -1,96 +1,54 @@
// Package graph provides a client for using the OtterSpace API.
//
// This package provides a client to interact with the OtterSpace API. It includes
// methods for all API endpoints, and convenience methods for common tasks.
//
// This is a simple usage example:
//
// package main
//
// import (
// "context"
// "fmt"
// "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
// "git.dragse.it/anthrove/otter-space-sdk/pkg/graph"
// )
//
// func main() {
// client := graph.NewGraphConnection()
// err := client.Connect(context.Background(), "your-endpoint", "your-username", "your-password")
// if err != nil {
// fmt.Println(err)
// return
// }
// // further usage of the client...
// }
package graph package graph
import ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
) )
// OtterSpace provides an interface for interacting with the OtterSpace API.
// It includes methods for connecting to the API, adding and linking users, posts, and sources,
// and retrieving information about users and posts.
type OtterSpace interface { type OtterSpace interface {
// Connect sets up a connection to the OtterSpace API endpoint using the provided username and password. // Connect sets up a connection to the endpoint using the provided username and password
// It returns an error if the connection cannot be established.
Connect(ctx context.Context, endpoint string, username string, password string) error Connect(ctx context.Context, endpoint string, username string, password string) error
// AddUserWithRelationToSource adds a new user to the OtterSpace graph and associates them with a source. // AddUserWithRelationToSource uploads an Anthrove user to the graph
// It returns the newly created user and an error if the operation fails. AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error)
AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error
// AddSource adds a new source to the OtterSpace graph. // AddSource uploads an Anthrove source to the graph
// It returns an error if the operation fails.
AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error AddSource(ctx context.Context, anthroveSource *models.AnthroveSource) error
// AddPost adds a new post to the OtterSpace graph. // AddPost uploads an Anthrove post to the graph
// It returns an error if the operation fails.
AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error AddPost(ctx context.Context, anthrovePost *models.AnthrovePost) error
// AddTagWithRelationToPost adds a new tag to the OtterSpace graph and associates it with a post. // AddTagWithRelationToPost uploads a tag associated with an Anthrove post to the graph
// It returns an error if the operation fails.
AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.AnthroveTag) error AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.AnthroveTag) error
// LinkPostWithSource establishes a link between a post and a source in the OtterSpace graph. // LinkPostWithSource establishes a link between a post and a source in the graph
// It returns an error if the operation fails.
LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error
// LinkUserWithPost establishes a link between a user and a post in the OtterSpace graph. // LinkUserWithPost establishes a link between a user and a post in the graph
// It returns an error if the operation fails.
LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error
// CheckUserPostLink checks if a link between a user and a post exists in the OtterSpace graph. // CheckUserPostLink checks if a link between a user and a post exists in the graph
// It returns true if the link exists, false otherwise, and an error if the operation fails.
CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error)
// CheckPostNodeExistsByAnthroveID checks if a post node exists in the OtterSpace graph by its Anthrove ID. // CheckPostNodeExistsByAnthroveID checks if an Anthrove post node exists in the graph by its Anthrove ID
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (*models.AnthrovePost, bool, error)
// CheckPostNodeExistsBySourceURL checks if a post node exists in the OtterSpace graph by its source URL. // CheckPostNodeExistsBySourceURL checks if an Anthrove post node exists in the graph by its source URL
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.AnthrovePost, bool, error)
// CheckPostNodeExistsBySourceID checks if a post node exists in the OtterSpace graph by its source ID. // CheckPostNodeExistsBySourceID checks if an Anthrove post node exists in the graph by its source ID
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.AnthrovePost, bool, error)
// GetUserFavoriteCount retrieves the count of a user's favorite posts from the OtterSpace graph. // GetUserFavoriteCount retrieves the count of user's favorite posts
// It returns the count and an error if the operation fails.
GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error)
// GetUserSourceLinks retrieves the links between a user and sources in the OtterSpace graph. // GetUserSourceLinks retrieves the links between a user and sources in the graph
// It returns a map of source domains to user-source relationships, and an error if the operation fails.
GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error)
// GetAnthroveUser retrieves a user from the OtterSpace graph by their ID. // GetAnthroveUser retrieves an Anthrove user from the graph by their ID
// It returns the user and an error if the operation fails.
GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error)
// GetAllAnthroveUserIDs retrieves all user IDs from the OtterSpace graph. // GetAllAnthroveUserIDs retrieves all Anthrove user IDs from the graph
// It returns a slice of user IDs and an error if the operation fails.
GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error)
} }

View File

@ -36,7 +36,7 @@ func (g *graphConnection) Connect(ctx context.Context, endpoint string, username
return nil return nil
} }
func (g *graphConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { 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) return internal.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username)
} }