From 58dcf94d492739221d1e08eee80172756a51d21f Mon Sep 17 00:00:00 2001 From: soxx Date: Sat, 22 Jun 2024 22:06:36 +0200 Subject: [PATCH] refactor(postgres): removed old graph related code Signed-off-by: soxx --- internal/graph/logger.go | 46 -- internal/graph/post.go | 116 ----- internal/graph/relationships.go | 100 ---- internal/graph/source.go | 113 ----- internal/graph/tag.go | 84 ---- internal/graph/user.go | 454 ------------------ internal/postgres/post.go | 13 +- internal/postgres/relationships.go | 9 +- internal/postgres/relationships_test.go | 3 +- internal/postgres/source.go | 14 +- internal/postgres/source_test.go | 20 +- internal/postgres/tag.go | 13 +- internal/postgres/tag_test.go | 11 +- internal/postgres/user.go | 57 ++- internal/postgres/user_test.go | 83 ++-- internal/utils/modelConvert.go | 60 --- pkg/database/database.go | 58 +-- pkg/database/graph.go | 123 ----- .../migrations/001_inital_database.sql | 8 +- pkg/models/README.md | 84 ---- pkg/models/{pgModels => }/orm.go | 2 +- pkg/models/pgModels/userSource.go | 13 - pkg/models/{pgModels => }/post.go | 8 +- pkg/models/{pgModels => }/postReference.go | 2 +- pkg/models/{pgModels => }/source.go | 2 +- pkg/models/{pgModels => }/tag.go | 19 +- pkg/models/{pgModels => }/user.go | 2 +- pkg/models/{pgModels => }/userFavorite.go | 6 +- pkg/models/userSource.go | 14 + 29 files changed, 164 insertions(+), 1373 deletions(-) delete mode 100644 internal/graph/logger.go delete mode 100644 internal/graph/post.go delete mode 100644 internal/graph/relationships.go delete mode 100644 internal/graph/source.go delete mode 100644 internal/graph/tag.go delete mode 100644 internal/graph/user.go delete mode 100644 internal/utils/modelConvert.go delete mode 100644 pkg/database/graph.go delete mode 100644 pkg/models/README.md rename pkg/models/{pgModels => }/orm.go (95%) delete mode 100644 pkg/models/pgModels/userSource.go rename pkg/models/{pgModels => }/post.go (65%) rename pkg/models/{pgModels => }/postReference.go (95%) rename pkg/models/{pgModels => }/source.go (95%) rename pkg/models/{pgModels => }/tag.go (53%) rename pkg/models/{pgModels => }/user.go (92%) rename pkg/models/{pgModels => }/userFavorite.go (69%) create mode 100644 pkg/models/userSource.go diff --git a/internal/graph/logger.go b/internal/graph/logger.go deleted file mode 100644 index 914db3a..0000000 --- a/internal/graph/logger.go +++ /dev/null @@ -1,46 +0,0 @@ -package graph - -import ( - "fmt" - - neo4jLog "github.com/neo4j/neo4j-go-driver/v5/neo4j/log" - log "github.com/sirupsen/logrus" -) - -type graphLogger struct { - graphDebug bool -} - -func NewGraphLogger(graphDebug bool) neo4jLog.Logger { - return &graphLogger{graphDebug: graphDebug} -} - -func (n graphLogger) Error(name string, id string, err error) { - log.WithFields(log.Fields{ - "name": name, - "id": id, - }).Errorf("database: %s", err) -} - -func (n graphLogger) Warnf(name string, id string, msg string, args ...any) { - log.WithFields(log.Fields{ - "name": name, - "id": id, - }).Warnf("database: %v", fmt.Sprintf(msg, args...)) -} - -func (n graphLogger) Infof(name string, id string, msg string, args ...any) { - log.WithFields(log.Fields{ - "name": name, - "id": id, - }).Infof("database: %v", fmt.Sprintf(msg, args...)) -} - -func (n graphLogger) Debugf(name string, id string, msg string, args ...any) { - if n.graphDebug { - log.WithFields(log.Fields{ - "name": name, - "id": id, - }).Debugf("database: %v", fmt.Sprintf(msg, args...)) - } -} diff --git a/internal/graph/post.go b/internal/graph/post.go deleted file mode 100644 index d263928..0000000 --- a/internal/graph/post.go +++ /dev/null @@ -1,116 +0,0 @@ -package graph - -import ( - "context" - "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" - log "github.com/sirupsen/logrus" -) - -func CreateAnthrovePostNode(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *graphModels.AnthrovePost) error { - query := ` - CREATE (newPostNode:AnthrovePost {post_id: $anthrove_post_id, rating: $anthrove_rating}) - ` - - params := map[string]any{ - "anthrove_post_id": anthrovePost.PostID, - "anthrove_rating": anthrovePost.Rating, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return err - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePost.PostID, - "anthrove_post_rating": anthrovePost.Rating, - }).Trace("database: created anthrove post") - - return nil -} - -func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, driver neo4j.DriverWithContext, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) { - query := ` - OPTIONAL MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) - RETURN postNode.post_id AS AnthrovePostID - ` - - params := map[string]any{ - "anthrove_post_id": anthrovePost.PostID, - } - - anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) - if err != nil { - return nil, false, err - } - - return anthrovePost, exists, nil -} - -func CheckIfAnthrovePostNodeExistsBySourceURl(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl string) (*graphModels.AnthrovePost, bool, error) { - query := ` - OPTIONAL MATCH (postNode:AnthrovePost)<-[:REFERENCE {url: $source_url}]-() - RETURN postNode.post_id AS AnthrovePostID - ` - - params := map[string]any{ - "source_url": sourceUrl, - } - anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) - if err != nil { - return nil, false, err - } - - return anthrovePost, exists, nil -} - -func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, driver neo4j.DriverWithContext, sourcePostID string) (*graphModels.AnthrovePost, bool, error) { - query := ` - OPTIONAL MATCH (postNode:AnthrovePost)<-[:REFERENCE {source_post_id: $source_post_id}]-() - RETURN postNode.post_id AS AnthrovePostID - ` - - params := map[string]any{ - "source_post_id": sourcePostID, - } - - anthrovePost, exists, err := executeCheckQuery(ctx, driver, query, params) - if err != nil { - return nil, false, err - } - - return anthrovePost, exists, nil -} - -func executeCheckQuery(ctx context.Context, driver neo4j.DriverWithContext, query string, params map[string]any) (*graphModels.AnthrovePost, bool, error) { - - var anthrovePost graphModels.AnthrovePost - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return &anthrovePost, false, err - } - - record := result.Records - - anthrovePostID, isNil, err := neo4j.GetRecordValue[string](record[0], "AnthrovePostID") - exists := !isNil - if err != nil { - return &anthrovePost, exists, err - } - - anthrovePost.PostID = models.AnthrovePostID(anthrovePostID) - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePost.PostID, - "anthrove_post_exists": exists, - }).Trace("database: checked if post exists") - - if !exists { - return nil, exists, nil - } - - return &anthrovePost, exists, nil -} diff --git a/internal/graph/relationships.go b/internal/graph/relationships.go deleted file mode 100644 index d9ea521..0000000 --- a/internal/graph/relationships.go +++ /dev/null @@ -1,100 +0,0 @@ -package graph - -import ( - "context" - "fmt" - "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" - log "github.com/sirupsen/logrus" -) - -func EstablishAnthrovePostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error { - query := ` - MATCH (sourceNode:Source {domain: $source_url}) - MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) - MERGE (sourceNode)-[:REFERENCE {url: $source_post_url, source_post_id: $source_post_id}]->(postNode) - ` - - params := map[string]any{ - "source_url": anthroveSourceDomain, - "anthrove_post_id": anthrovePostID, - "source_post_url": anthrovePostRelationship.Url, - "source_post_id": anthrovePostRelationship.PostID, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return err - } - - log.WithFields(log.Fields{ - "source_url": anthroveSourceDomain, - "anthrove_post_id": anthrovePostID, - "source_post_url": anthrovePostRelationship.Url, - "source_post_id": anthrovePostRelationship.PostID, - }).Trace("database: creating anthrove post to source link") - - return nil -} - -func EstablishUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error { - - query := ` - MATCH (user:User {user_id: $anthrove_user_id}) - MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id}) - MERGE (user)-[:FAV]->(anthrovePost) - ` - - params := map[string]any{ - "anthrove_post_id": anthrovePost.PostID, - "anthrove_user_id": anthroveUser.UserID, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return err - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePost.PostID, - "anthrove_user_id": anthroveUser.UserID, - }).Trace("database: created user to post link") - - return nil -} - -func CheckUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { - query := ` - OPTIONAL MATCH (:User {user_id: $anthrove_user_id})-[f:FAV]->(:AnthrovePost)<-[:REFERENCE{source_post_id: $source_post_id}]-(:Source{domain: $source_domain}) - RETURN COUNT(f) > 0 AS hasRelationship - ` - - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - "source_post_id": sourcePostID, - "source_domain": sourceUrl, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return false, err - } - - if len(result.Records) == 0 { - return false, fmt.Errorf("no records found") - } - - exists, _, err := neo4j.GetRecordValue[bool](result.Records[0], "hasRelationship") - if err != nil { - return false, err - } - - log.WithFields(log.Fields{ - "relationship_exists": exists, - "relationship_anthrove_user_id": anthroveUserID, - "relationship_e621_post_id": "", - }).Trace("database: checked user post relationship") - - return exists, nil -} diff --git a/internal/graph/source.go b/internal/graph/source.go deleted file mode 100644 index 5ed8fa6..0000000 --- a/internal/graph/source.go +++ /dev/null @@ -1,113 +0,0 @@ -package graph - -import ( - "context" - "fmt" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - - "github.com/neo4j/neo4j-go-driver/v5/neo4j" - log "github.com/sirupsen/logrus" -) - -func CreateSourceNode(ctx context.Context, driver neo4j.DriverWithContext, anthroveSource *graphModels.AnthroveSource) error { - query := ` - MERGE (sourceNode:Source {domain: $source_url}) - ON CREATE SET sourceNode.domain = $source_url, sourceNode.display_name = $source_display_name, sourceNode.icon = $source_icon - ` - params := map[string]any{ - "source_url": anthroveSource.Domain, - "source_display_name": anthroveSource.DisplayName, - "source_icon": anthroveSource.Icon, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return fmt.Errorf("database: %w", err) - } - - log.WithFields(log.Fields{ - "node_source_url": anthroveSource.Domain, - "node_source_displayName": anthroveSource.DisplayName, - "node_source_icon": anthroveSource.Icon, - }).Trace("database: created source node") - - return nil -} - -func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]graphModels.AnthroveSource, error) { - var sources []graphModels.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, graphModels.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("database: created tag node") - - return sources, nil -} - -func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl string) (*graphModels.AnthroveSource, error) { - - var source graphModels.AnthroveSource - - query := ` - MATCH (s:Source {domain: $source_url}) - RETURN s as source - ` - params := map[string]any{ - "source_url": sourceUrl, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return nil, err - } - - if len(result.Records) == 0 { - return nil, fmt.Errorf("source not found") - } - - record, _, err := neo4j.GetRecordValue[neo4j.Node](result.Records[0], "source") - if err != nil { - return nil, err - } - - source.DisplayName = record.Props["display_name"].(string) - source.Domain = record.Props["domain"].(string) - source.Icon = record.Props["icon"].(string) - - log.WithFields(log.Fields{ - "source_url": sourceUrl, - }).Trace("database: got source node") - - return &source, nil -} diff --git a/internal/graph/tag.go b/internal/graph/tag.go deleted file mode 100644 index cc9142c..0000000 --- a/internal/graph/tag.go +++ /dev/null @@ -1,84 +0,0 @@ -package graph - -import ( - "context" - "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" - log "github.com/sirupsen/logrus" -) - -func CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error { - query := ` - MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id}) - MERGE (tagNode:Tag {name: $tag_name, type: $tag_type}) - MERGE (anthrovePost)-[:HAS]->(tagNode) - ` - params := map[string]interface{}{ - "tag_name": anthroveTag.Name, - "tag_type": anthroveTag.Type, - "anthrove_post_id": anthrovePostID, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return err - } - - log.WithFields(log.Fields{ - "anthrove_post_id": anthrovePostID, - "tag_name": anthroveTag.Name, - "tag_type": anthroveTag.Type, - }).Trace("database: created tag node") - - return nil -} - -func GetTags(ctx context.Context, driver neo4j.DriverWithContext) ([]graphModels.TagsWithFrequency, error) { - var userTags []graphModels.TagsWithFrequency - - query := ` - MATCH (:AnthrovePost)-[:HAS]->(t:Tag) - RETURN t as tag, COUNT(t) AS frequency - ORDER BY frequency DESC - ` - 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] - - tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag") - if err != nil { - return nil, err - } - - frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency") - if err != nil { - return nil, err - } - - userTags = append(userTags, graphModels.TagsWithFrequency{ - Frequency: frequency, - Tags: graphModels.AnthroveTag{ - Name: tag.Props["name"].(string), - Type: tag.Props["type"].(string), - }, - }) - - } - - log.WithFields(log.Fields{ - "tag_amount": len(userTags), - }).Trace("database: created tag node") - - return userTags, nil -} diff --git a/internal/graph/user.go b/internal/graph/user.go deleted file mode 100644 index a136de3..0000000 --- a/internal/graph/user.go +++ /dev/null @@ -1,454 +0,0 @@ -package graph - -import ( - "context" - "fmt" - "git.dragse.it/anthrove/otter-space-sdk/internal/utils" - "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" - log "github.com/sirupsen/logrus" -) - -func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { - query := ` - MATCH (userNode:User {user_id: $anthrove_user_id}) - MATCH (sourceNode:Source {domain: $source_domain}) - MERGE (userNode)-[r:HAS_ACCOUNT_AT {username: $source_user_name, user_id: $source_user_id}]->(sourceNode) - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - "source_user_id": userID, - "source_user_name": username, - "source_domain": sourceDomain, - } - - _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return err - } - - var anthroveUserRelationship []graphModels.AnthroveUserRelationship - - anthroveUserRelationship = append(anthroveUserRelationship, graphModels.AnthroveUserRelationship{ - UserID: userID, - Username: username, - ScrapeTimeInterval: "", - Source: graphModels.AnthroveSource{ - DisplayName: "", - Domain: sourceDomain, - Icon: "", - }, - }) - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "source_user_id": userID, - "source_user_name": username, - "source_domain": sourceDomain, - }).Trace("database: crated user with relationship") - - return nil -} - -func GetUserFavoritesCount(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (int64, error) { - var userFavoriteCount int64 - - query := ` - MATCH (userNode:User {user_id: $anthrove_user_id}) - MATCH (userNode)-[:FAV]->(favPost:AnthrovePost) - MATCH (sourceNode)-[:REFERENCE]->(favPost) - RETURN count( DISTINCT favPost) AS FavoritePostsCount - ` - - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return 0, err - } - - if len(result.Records) == 0 { - // no matches -> user does not exist, return count 0 - return userFavoriteCount, err - } - - record := result.Records[0] - - userFavoriteCount, _, err = neo4j.GetRecordValue[int64](record, "FavoritePostsCount") - if err != nil { - return userFavoriteCount, err - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_user_fav_count": userFavoriteCount, - }).Trace("database: got user favorite count") - - return userFavoriteCount, nil -} - -func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) { - - userSource := make(map[string]graphModels.AnthroveUserRelationship) - - query := ` - MATCH (user:User{user_id: $anthrove_user_id})-[r:HAS_ACCOUNT_AT]->(s:Source) - RETURN toString(r.user_id) AS sourceUserID, toString(r.username) AS sourceUsername, s as source; - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return nil, err - } - - if len(result.Records) == 0 { - return nil, fmt.Errorf("user has no relations") - } - - for i := range result.Records { - record := result.Records[i] - source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "source") - if err != nil { - return nil, err - } - sourceUserID, _, err := neo4j.GetRecordValue[string](record, "sourceUserID") - if err != nil { - return nil, err - } - sourceUsername, _, err := neo4j.GetRecordValue[string](record, "sourceUsername") - if err != nil { - return nil, err - } - - displayName := source.Props["display_name"].(string) - domain := source.Props["domain"].(string) - icon := source.Props["icon"].(string) - - anthroveSourceUser := graphModels.AnthroveUserRelationship{ - UserID: sourceUserID, - Username: sourceUsername, - Source: graphModels.AnthroveSource{ - DisplayName: displayName, - Domain: domain, - Icon: icon, - }, - } - userSource[displayName] = anthroveSourceUser - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_data": userSource, - }).Trace("database: got user favorite count") - - return userSource, nil -} - -func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) { - - userSource := make(map[string]graphModels.AnthroveUserRelationship) - - query := ` - MATCH (user:User{user_id: $anthrove_user_id})-[r:HAS_ACCOUNT_AT]->(s:Source{display_name: $source_display_name}) - RETURN toString(r.user_id) AS sourceUserID, toString(r.username) AS sourceUsername, s as source; - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - "source_display_name": sourceDisplayName, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return nil, err - } - - if len(result.Records) == 0 { - return nil, fmt.Errorf("user has no relations with the source %s", sourceDisplayName) - } - - for i := range result.Records { - record := result.Records[i] - source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "source") - if err != nil { - return nil, err - } - sourceUserID, _, err := neo4j.GetRecordValue[string](record, "sourceUserID") - if err != nil { - return nil, err - } - sourceUsername, _, err := neo4j.GetRecordValue[string](record, "sourceUsername") - if err != nil { - return nil, err - } - - displayName := source.Props["display_name"].(string) - domain := source.Props["domain"].(string) - icon := source.Props["icon"].(string) - - anthroveSourceUser := graphModels.AnthroveUserRelationship{ - UserID: sourceUserID, - Username: sourceUsername, - Source: graphModels.AnthroveSource{ - DisplayName: displayName, - Domain: domain, - Icon: icon, - }, - } - userSource[displayName] = anthroveSourceUser - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - "anthrove_data": userSource, - }).Trace("database: got user favorite count") - - return userSource, nil -} - -func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) { - var err error - var anthroveUser graphModels.AnthroveUser - var userSources graphModels.AnthroveSource - userRelationships := make([]graphModels.AnthroveUserRelationship, 0) - - query := ` - MATCH (user:User{user_id: $anthrove_user_id})-[relation:HAS_ACCOUNT_AT]->(source:Source) - RETURN user as User, relation as Relation, source as Source; - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - } - - result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) - if err != nil { - return nil, err - } - - if len(result.Records) == 0 { - return nil, fmt.Errorf("user has no relations") - } - - for i := range result.Records { - record := result.Records[i] - - user, _, err := neo4j.GetRecordValue[neo4j.Node](record, "User") - if err != nil { - return nil, err - } - relation, _, err := neo4j.GetRecordValue[neo4j.Relationship](record, "Relation") - if err != nil { - return nil, err - } - source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "Source") - if err != nil { - return nil, err - } - - userRelationships = append(userRelationships, graphModels.AnthroveUserRelationship{ - UserID: fmt.Sprintf("%v", utils.GetOrDefault(relation.Props, "user_id", "")), - Username: utils.GetOrDefault(relation.Props, "username", "").(string), - ScrapeTimeInterval: utils.GetOrDefault(relation.Props, "scrape_time_interval", "").(string), - }) - - userSources = graphModels.AnthroveSource{ - DisplayName: utils.GetOrDefault(source.Props, "display_name", "").(string), - Domain: utils.GetOrDefault(source.Props, "domain", "").(string), - Icon: utils.GetOrDefault(source.Props, "icon", "").(string), - } - - anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string)) - anthroveUser.Relationship = userRelationships - - for j := range userRelationships { - anthroveUser.Relationship[j].Source = userSources - } - - } - - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - }).Trace("database: got anthrove user") - - return &anthroveUser, nil - -} - -func GetAllAnthroveUserIDs(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveUserID, error) { - var err error - var anthroveUsers []models.AnthroveUserID - - query := ` - MATCH (anthroveUser:User) - RETURN anthroveUser - ` - result, err := neo4j.ExecuteQuery(ctx, driver, query, nil, neo4j.EagerResultTransformer) - if err != nil { - return nil, err - } - - if len(result.Records) == 0 { - log.Warnf("No users found, this should not be happening!") - return []models.AnthroveUserID{}, nil - } - - for i := range result.Records { - record := result.Records[i] - - user, _, err := neo4j.GetRecordValue[neo4j.Node](record, "anthroveUser") - if err != nil { - return nil, err - } - - anthroveUsers = append(anthroveUsers, models.AnthroveUserID(fmt.Sprintf(user.Props["user_id"].(string)))) - } - - log.WithFields(log.Fields{ - "anthrove_user_id_count": len(anthroveUsers), - }).Trace("database: got al anthrove user IDs") - - return anthroveUsers, nil - -} - -func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) { - var err error - var favoritePosts []graphModels.FavoritePost - - query := ` - CALL { - MATCH (user:User{user_id: $anthrove_user_id})-[r:FAV]->(p:AnthrovePost) - RETURN p.post_id AS post_id - ORDER BY id(p) ASC - SKIP $skip - LIMIT $limit - } - WITH collect(post_id) AS faves - MATCH (a:AnthrovePost)<-[r:REFERENCE]-(s:Source) - WHERE a.post_id in faves - RETURN a AS anthrovePost, r AS postRelation, s AS Source - ORDER BY id(a) ASC - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - "limit": limit, - "skip": skip, - } - - 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] - - anthrovePost, _, err := neo4j.GetRecordValue[neo4j.Node](record, "anthrovePost") - if err != nil { - return nil, err - } - - postRelation, _, err := neo4j.GetRecordValue[neo4j.Relationship](record, "postRelation") - if err != nil { - return nil, err - } - - source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "Source") - if err != nil { - return nil, err - } - - if len(favoritePosts) != 0 && favoritePosts[len(favoritePosts)-1].AnthrovePost.PostID == models.AnthrovePostID(anthrovePost.Props["post_id"].(string)) { - favoritePosts[len(favoritePosts)-1].Relations = append(favoritePosts[len(favoritePosts)-1].Relations, graphModels.FavoriteRelations{ - SourcesID: source.Props["display_name"].(string), - Relations: graphModels.AnthrovePostRelationship{ - PostID: postRelation.Props["source_post_id"].(string), - Url: postRelation.Props["url"].(string), - }, - }) - } else { - favoritePosts = append(favoritePosts, graphModels.FavoritePost{ - AnthrovePost: graphModels.AnthrovePost{ - PostID: models.AnthrovePostID(anthrovePost.Props["post_id"].(string)), - Rating: models.Rating(anthrovePost.Props["rating"].(string)), - }, - Relations: []graphModels.FavoriteRelations{{ - SourcesID: source.Props["display_name"].(string), - Relations: graphModels.AnthrovePostRelationship{ - PostID: postRelation.Props["source_post_id"].(string), - Url: postRelation.Props["url"].(string), - }, - }}, - }) - - } - - } - - log.WithFields(log.Fields{ - "anthrove_user_fav_count": len(favoritePosts), - }).Trace("database: got al anthrove user favorites") - - return &graphModels.FavoriteList{Posts: favoritePosts}, nil - -} - -func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) { - var userTags []graphModels.TagsWithFrequency - - query := ` - MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag) - RETURN t as tag, COUNT(t) AS frequency - ORDER BY frequency DESC - ` - params := map[string]any{ - "anthrove_user_id": anthroveUserID, - } - - 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] - - tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag") - if err != nil { - return nil, err - } - - frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency") - if err != nil { - return nil, err - } - - userTags = append(userTags, graphModels.TagsWithFrequency{ - Frequency: frequency, - Tags: graphModels.AnthroveTag{ - Name: tag.Props["name"].(string), - Type: tag.Props["type"].(string), - }, - }) - - } - - log.WithFields(log.Fields{ - "tag_amount": len(userTags), - }).Trace("database: created tag node") - - return userTags, nil -} diff --git a/internal/postgres/post.go b/internal/postgres/post.go index 3ed43a5..d00c08a 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -6,14 +6,13 @@ import ( "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveRating models.Rating) error { - post := pgModels.Post{ - BaseModel: pgModels.BaseModel{ + post := models.Post{ + BaseModel: models.BaseModel{ ID: string(anthrovePostID), }, Rating: anthroveRating, @@ -41,8 +40,8 @@ func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, } func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*graphModels.AnthrovePost, bool, error) { - postRef := pgModels.PostReference{} - err := db.WithContext(ctx).Model(&pgModels.PostReference{}).Where("url = ?", sourceURL).First(&postRef).Error + postRef := models.PostReference{} + err := db.WithContext(ctx).Model(&models.PostReference{}).Where("url = ?", sourceURL).First(&postRef).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { @@ -51,7 +50,7 @@ func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, return nil, false, err } - var post pgModels.Post + var post models.Post err = db.WithContext(ctx).First(&post, "id = ?", postRef.PostID).Error if err != nil { @@ -76,7 +75,7 @@ func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, db *gorm.DB, s func executeCheckQuery(ctx context.Context, db *gorm.DB, query string, args ...interface{}) (bool, error) { var count int64 - err := db.WithContext(ctx).Model(&pgModels.Post{}).Where(query, args...).Count(&count).Error + err := db.WithContext(ctx).Model(&models.Post{}).Where(query, args...).Count(&count).Error if err != nil { return false, err } diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index a109fdb..969e66d 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -4,13 +4,12 @@ import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error { - var source pgModels.Source + var source models.Source var err error // Find the source @@ -20,7 +19,7 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov } // Establish the relationship - err = db.WithContext(ctx).Create(pgModels.PostReference{ + err = db.WithContext(ctx).Create(models.PostReference{ PostID: string(anthrovePostID), SourceID: source.ID, URL: anthrovePostRelationship.Url, @@ -39,7 +38,7 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov } func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { - userFavorite := pgModels.UserFavorite{ + userFavorite := models.UserFavorite{ UserID: string(anthroveUserID), PostID: string(anthrovePostID), } @@ -59,7 +58,7 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID mo func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { var count int64 - err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error + err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error if err != nil { return false, err } diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go index 29b29ba..b96a5f3 100644 --- a/internal/postgres/relationships_test.go +++ b/internal/postgres/relationships_test.go @@ -3,7 +3,6 @@ package postgres import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" "testing" @@ -26,7 +25,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { t.Fatal(err) } - source := &pgModels.Source{ + source := &models.Source{ DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", diff --git a/internal/postgres/source.go b/internal/postgres/source.go index 0e60a5e..5e4ecd5 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -3,20 +3,20 @@ package postgres import ( "context" "fmt" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) // CreateSourceNode creates a pgModels.Source -func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *pgModels.Source) error { +func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { if anthroveSource.Domain == "" { return fmt.Errorf("anthroveSource domain is required") } - result := db.WithContext(ctx).Where(pgModels.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource) + result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource) if result.Error != nil { return result.Error @@ -32,8 +32,8 @@ func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *pgModels } // GetAllSourceNodes returns a list of all pgModels.Source -func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]pgModels.Source, error) { - var sources []pgModels.Source +func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]models.Source, error) { + var sources []models.Source result := db.WithContext(ctx).Find(&sources) @@ -49,8 +49,8 @@ func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]pgModels.Source, err } // GetSourceNodesByURL returns the first source it finds based on the domain -func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*pgModels.Source, error) { - var sources pgModels.Source +func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*models.Source, error) { + var sources models.Source if domain == "" { return nil, fmt.Errorf("domain is required") diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index d0514c2..99fd7b8 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -2,7 +2,7 @@ package postgres import ( "context" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" "testing" @@ -19,13 +19,13 @@ func TestCreateSourceNode(t *testing.T) { // Setup Test - validAnthroveSource := &pgModels.Source{ + validAnthroveSource := &models.Source{ DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", } - invalidAnthroveSource := &pgModels.Source{ + invalidAnthroveSource := &models.Source{ Domain: "", } @@ -33,7 +33,7 @@ func TestCreateSourceNode(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveSource *pgModels.Source + anthroveSource *models.Source } tests := []struct { name string @@ -88,7 +88,7 @@ func TestGetAllSourceNodes(t *testing.T) { // Setup Test - sources := []pgModels.Source{ + sources := []models.Source{ { DisplayName: "e621", Domain: "e621.net", @@ -121,7 +121,7 @@ func TestGetAllSourceNodes(t *testing.T) { tests := []struct { name string args args - want []pgModels.Source + want []models.Source wantErr bool }{ { @@ -159,7 +159,7 @@ func TestGetSourceNodesByURL(t *testing.T) { // Setup Test - source := &pgModels.Source{ + source := &models.Source{ DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -179,7 +179,7 @@ func TestGetSourceNodesByURL(t *testing.T) { tests := []struct { name string args args - want *pgModels.Source + want *models.Source wantErr bool }{ { @@ -227,7 +227,7 @@ func TestGetSourceNodesByURL(t *testing.T) { } } -func checkSourcesNode(got []pgModels.Source, want []pgModels.Source) bool { +func checkSourcesNode(got []models.Source, want []models.Source) bool { for i, source := range want { if source.DisplayName != got[i].DisplayName { return false @@ -244,7 +244,7 @@ func checkSourcesNode(got []pgModels.Source, want []pgModels.Source) bool { } -func checkSourceNode(got *pgModels.Source, want *pgModels.Source) bool { +func checkSourceNode(got *models.Source, want *models.Source) bool { if want == nil && got == nil { return true diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 4249069..124742e 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -4,12 +4,11 @@ import ( "context" "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) -func CreateTag(ctx context.Context, db *gorm.DB, tag *pgModels.Tag) error { +func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { resultTag := db.WithContext(ctx).Where(tag).Create(tag) @@ -25,7 +24,7 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *pgModels.Tag) error { return nil } -func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *pgModels.Tag) error { +func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *models.Tag) error { if PostID == "" { return fmt.Errorf("PostID is empty") @@ -37,8 +36,8 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An return resultTag.Error } - pgPost := pgModels.Post{ - BaseModel: pgModels.BaseModel{ + pgPost := models.Post{ + BaseModel: models.BaseModel{ ID: string(PostID), }, } @@ -58,8 +57,8 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An return nil } -func GetTags(ctx context.Context, db *gorm.DB) ([]pgModels.Tag, error) { - var tags []pgModels.Tag +func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) { + var tags []models.Tag result := db.WithContext(ctx).Find(&tags) if result.Error != nil { return nil, result.Error diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 347754e..c0b323a 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -3,7 +3,6 @@ package postgres import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" "testing" @@ -25,7 +24,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { t.Fatal(err) } - tag := &pgModels.Tag{ + tag := &models.Tag{ Name: "JayTheFerret", Type: "artist", } @@ -35,7 +34,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { ctx context.Context db *gorm.DB PostID models.AnthrovePostID - tag *pgModels.Tag + tag *models.Tag } tests := []struct { name string @@ -103,7 +102,7 @@ func TestGetTags(t *testing.T) { // Setup Test - tags := []pgModels.Tag{ + tags := []models.Tag{ { Name: "JayTheFerret", Type: "artist", @@ -133,7 +132,7 @@ func TestGetTags(t *testing.T) { tests := []struct { name string args args - want []pgModels.Tag + want []models.Tag wantErr bool }{ { @@ -160,7 +159,7 @@ func TestGetTags(t *testing.T) { } } -func checkTag(got []pgModels.Tag, want []pgModels.Tag) bool { +func checkTag(got []models.Tag, want []models.Tag) bool { for i, tag := range want { if tag.Type != got[i].Type { return false diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 10b343c..2c9e56e 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -5,7 +5,6 @@ import ( "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -16,8 +15,8 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove return fmt.Errorf("anthroveUserID cannot be empty") } - user := pgModels.User{ - BaseModel: pgModels.BaseModel{ + user := models.User{ + BaseModel: models.BaseModel{ ID: string(anthroveUserID), }, } @@ -39,8 +38,8 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove return fmt.Errorf("anthroveUserID cannot be empty") } - user := pgModels.User{ - BaseModel: pgModels.BaseModel{ + user := models.User{ + BaseModel: models.BaseModel{ ID: string(anthroveUserID), }, } @@ -52,7 +51,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove return err } - source := pgModels.Source{ + source := models.Source{ Domain: sourceDomain, } @@ -63,7 +62,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove return err } - userSource := pgModels.UserSource{ + userSource := models.UserSource{ UserID: user.ID, SourceID: source.ID, AccountUsername: username, @@ -96,7 +95,7 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode } var count int64 - err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error + err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -113,10 +112,10 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode } func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) { - var userSources []pgModels.UserSource + var userSources []models.UserSource userSourceMap := make(map[string]graphModels.AnthroveUserRelationship) - err := db.WithContext(ctx).Model(&pgModels.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error + err := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -125,8 +124,8 @@ func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.A } for _, userSource := range userSources { - var source pgModels.Source - err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error + var source models.Source + err = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error if err != nil { log.WithFields(log.Fields{ "source_id": userSource.SourceID, @@ -157,10 +156,10 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty") } - var userSources []pgModels.UserSource + var userSources []models.UserSource userSourceMap := make(map[string]graphModels.AnthroveUserRelationship) - err := db.WithContext(ctx).Model(&pgModels.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error + err := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -170,8 +169,8 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID } for _, userSource := range userSources { - var source pgModels.Source - err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error + var source models.Source + err = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error if err != nil { log.WithFields(log.Fields{ "source_id": userSource.SourceID, @@ -204,8 +203,8 @@ func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Ant return nil, fmt.Errorf("anthroveUserID cannot be empty") } - var user pgModels.User - var userSources []pgModels.UserSource + var user models.User + var userSources []models.UserSource anthroveUser := &graphModels.AnthroveUser{ UserID: anthroveUserID, } @@ -218,7 +217,7 @@ func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Ant return nil, err } - err = db.WithContext(ctx).Model(&pgModels.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error + err = db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -227,8 +226,8 @@ func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Ant } for _, userSource := range userSources { - var source pgModels.Source - err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error + var source models.Source + err = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error if err != nil { log.WithFields(log.Fields{ "source_id": userSource.SourceID, @@ -255,10 +254,10 @@ func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Ant } func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) { - var users []pgModels.User + var users []models.User var userIDs []models.AnthroveUserID - err := db.WithContext(ctx).Model(&pgModels.User{}).Find(&users).Error + err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error if err != nil { log.Error("database: failed to get all anthrove user IDs") return nil, err @@ -276,10 +275,10 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU } func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) { - var userFavorites []pgModels.UserFavorite + var userFavorites []models.UserFavorite var favoritePosts []graphModels.FavoritePost - err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error + err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error if err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, @@ -290,8 +289,8 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthrov } for _, userFavorite := range userFavorites { - var post pgModels.Post - err = db.WithContext(ctx).Model(&pgModels.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error + var post models.Post + err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error if err != nil { log.WithFields(log.Fields{ "post_id": userFavorite.PostID, @@ -316,7 +315,7 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthrov } func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) { - var userFavorites []pgModels.UserFavorite + var userFavorites []models.UserFavorite err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error if err != nil { log.WithFields(log.Fields{ @@ -330,7 +329,7 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, ant typeName string }]int) for _, userFavorite := range userFavorites { - var post pgModels.Post + var post models.Post err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error if err != nil { log.WithFields(log.Fields{ diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 531e13e..6beec7b 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -5,7 +5,6 @@ import ( "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" "reflect" @@ -73,7 +72,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { // Setup Test - source := &pgModels.Source{ + source := &models.Source{ DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -320,7 +319,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) { defer container.Terminate(ctx) // Setup Test - source := &pgModels.Source{ + source := &models.Source{ DisplayName: "e621", Domain: "e621.net", } @@ -455,37 +454,37 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1")), Rating: "safe", }, - }, + }, { AnthrovePost: graphModels.AnthrovePost{ PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2")), Rating: "safe", }, - }, + }, { AnthrovePost: graphModels.AnthrovePost{ PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3")), Rating: "explicit", }, - }, + }, { AnthrovePost: graphModels.AnthrovePost{ PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4")), Rating: "explicit", }, - }, + }, { AnthrovePost: graphModels.AnthrovePost{ PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5")), Rating: "questionable", }, - }, + }, { AnthrovePost: graphModels.AnthrovePost{ PostID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6")), Rating: "safe", }, - }, + }, } expectedResult := &graphModels.FavoriteList{ Posts: expectedResultPostsGraph, @@ -502,31 +501,31 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { t.Fatal(err) } - expectedResultPostsPg := []pgModels.Post{ + expectedResultPostsPg := []models.Post{ { - BaseModel: pgModels.BaseModel{ID: "Post1"}, + BaseModel: models.BaseModel{ID: "Post1"}, Rating: "safe", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post2"}, + BaseModel: models.BaseModel{ID: "Post2"}, Rating: "safe", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post3"}, + BaseModel: models.BaseModel{ID: "Post3"}, Rating: "explicit", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post4"}, + BaseModel: models.BaseModel{ID: "Post4"}, Rating: "explicit", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post5"}, + BaseModel: models.BaseModel{ID: "Post5"}, Rating: "questionable", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post6"}, + BaseModel: models.BaseModel{ID: "Post6"}, Rating: "safe", - }, + }, } for _, expectedResultPost := range expectedResultPostsPg { @@ -621,31 +620,31 @@ func TestGetUserFavoritesCount(t *testing.T) { t.Fatal(err) } - expectedResultPostsPg := []pgModels.Post{ + expectedResultPostsPg := []models.Post{ { - BaseModel: pgModels.BaseModel{ID: "Post1"}, + BaseModel: models.BaseModel{ID: "Post1"}, Rating: "safe", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post2"}, + BaseModel: models.BaseModel{ID: "Post2"}, Rating: "safe", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post3"}, + BaseModel: models.BaseModel{ID: "Post3"}, Rating: "explicit", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post4"}, + BaseModel: models.BaseModel{ID: "Post4"}, Rating: "explicit", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post5"}, + BaseModel: models.BaseModel{ID: "Post5"}, Rating: "questionable", - }, + }, { - BaseModel: pgModels.BaseModel{ID: "Post6"}, + BaseModel: models.BaseModel{ID: "Post6"}, Rating: "safe", - }, + }, } for _, expectedResultPost := range expectedResultPostsPg { @@ -727,7 +726,7 @@ func TestGetUserSourceLink(t *testing.T) { // Setup Test - esource := &pgModels.Source{ + esource := &models.Source{ DisplayName: "e621", Domain: "e621.net", } @@ -736,7 +735,7 @@ func TestGetUserSourceLink(t *testing.T) { t.Fatal(err) } - fasource := &pgModels.Source{ + fasource := &models.Source{ DisplayName: "fa", Domain: "fa.net", } @@ -823,10 +822,10 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { t.Fatal(err) } - posts := []pgModels.Post{ - {BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"}, - {BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"}, - {BaseModel: pgModels.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"}, + posts := []models.Post{ + {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"}, + {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"}, + {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"}, } for _, post := range posts { @@ -840,7 +839,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } } - tags := []pgModels.Tag{ + tags := []models.Tag{ {Name: "JayTheFerret", Type: "artist"}, {Name: "Ferret", Type: "species"}, {Name: "Jay", Type: "character"}, diff --git a/internal/utils/modelConvert.go b/internal/utils/modelConvert.go deleted file mode 100644 index 29a4995..0000000 --- a/internal/utils/modelConvert.go +++ /dev/null @@ -1,60 +0,0 @@ -package utils - -import ( - "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" -) - -// GraphConvertSource converts a graphModels.AnthroveSource to a pgModels.Source -func GraphConvertSource(graphSource *graphModels.AnthroveSource) *pgModels.Source { - pgSource := &pgModels.Source{ - DisplayName: graphSource.DisplayName, - Domain: graphSource.Domain, - Icon: graphSource.Icon, - } - return pgSource -} - -// PostgresConvertToAnthroveSource converts a pgModels.Source to a graphModels.AnthroveSource -func PostgresConvertToAnthroveSource(pgSource *pgModels.Source) *graphModels.AnthroveSource { - graphSource := &graphModels.AnthroveSource{ - DisplayName: pgSource.DisplayName, - Domain: pgSource.Domain, - Icon: pgSource.Icon, - } - - return graphSource -} - -// GraphConvertTag converts a graphModels.AnthroveTag to a pgModels.Tag -func GraphConvertTag(graphTag *graphModels.AnthroveTag) *pgModels.Tag { - pgTag := &pgModels.Tag{ - Name: graphTag.Name, - Type: models.TagType(graphTag.Type), - } - return pgTag -} - -// PostgresConvertToAnthroveTag converts a pgModels.Tag to a graphModels.AnthroveTag -func PostgresConvertToAnthroveTag(pgTag *pgModels.Tag) *graphModels.AnthroveTag { - graphTag := &graphModels.AnthroveTag{ - Name: pgTag.Name, - Type: string(pgTag.Type), - } - - return graphTag -} - -func ConvertToTagsWithFrequency(tags []pgModels.Tag) []graphModels.TagsWithFrequency { - var tagsWithFrequency []graphModels.TagsWithFrequency - for _, tag := range tags { - graphTag := PostgresConvertToAnthroveTag(&tag) - tagsWithFrequency = append(tagsWithFrequency, graphModels.TagsWithFrequency{ - Frequency: 0, - Tags: *graphTag, - }) - } - - return tagsWithFrequency -} diff --git a/pkg/database/database.go b/pkg/database/database.go index 307bc83..a6c908b 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -1,34 +1,8 @@ -// Package database 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/database" -// ) -// -// func main() { -// client := database.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 database import ( "context" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" - "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" ) // OtterSpace provides an interface for interacting with the OtterSpace API. @@ -45,23 +19,23 @@ type OtterSpace interface { // AddSource adds a new source to the OtterSpace database. // It returns an error if the operation fails. - AddSource(ctx context.Context, anthroveSource *graphModels.AnthroveSource) error + AddSource(ctx context.Context, anthroveSource *models.Source) error // AddPost adds a new post to the OtterSpace database. // It returns an error if the operation fails. - AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error + AddPost(ctx context.Context, anthrovePost *models.Post) error // AddTagWithRelationToPost adds a new tag to the OtterSpace database and associates it with a post. // It returns an error if the operation fails. - AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error + AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error // LinkPostWithSource establishes a link between a post and a source in the OtterSpace database. // It returns an error if the operation fails. - LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error + LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.PostReference) error // LinkUserWithPost establishes a link between a user and a post in the OtterSpace database. // It returns an error if the operation fails. - LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error + LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error // CheckUserPostLink checks if a link between a user and a post exists in the OtterSpace database. // It returns true if the link exists, false otherwise, and an error if the operation fails. @@ -69,15 +43,15 @@ type OtterSpace interface { // CheckPostNodeExistsByAnthroveID checks if a post node exists in the OtterSpace database 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 *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) + CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, bool, error) // CheckPostNodeExistsBySourceURL checks if a post node exists in the OtterSpace database 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) (*graphModels.AnthrovePost, bool, error) + CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, bool, error) // CheckPostNodeExistsBySourceID checks if a post node exists in the OtterSpace database 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) (*graphModels.AnthrovePost, bool, error) + CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.Post, bool, error) // GetUserFavoriteCount retrieves the count of a user's favorite posts from the OtterSpace database. // It returns the count and an error if the operation fails. @@ -85,32 +59,32 @@ type OtterSpace interface { // GetUserSourceLinks retrieves the links between a user and sources in the OtterSpace database. // 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]graphModels.AnthroveUserRelationship, error) + GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) // GetSpecifiedUserSourceLink GetUserSourceLinks retrieves the links between a user and a specific source in the OtterSpace database. // It returns a map of source domains to user-source relationships, and an error if the operation fails. - GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) + GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) // GetAnthroveUser retrieves a user from the OtterSpace database by their ID. // It returns the user and an error if the operation fails. - GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) + GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.User, error) // GetAllAnthroveUserIDs retrieves all user IDs from the OtterSpace database. // It returns a slice of user IDs and an error if the operation fails. GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) // GetUserFavoritePostsWithPagination gets all user favorites with relation and sources for the given user - GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) + GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) // GetUserTagsTroughFavedPosts returns a list of Tags that the user hs favorites through a post - GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) + GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) // GetAllTags returns a list of Tags that the user hs favorites through a post - GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) + GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) // GetAllSources returns a list of Sources in the database - GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) + GetAllSources(ctx context.Context) ([]models.Source, error) // GetSourceByURL returns the Source Node based on the URL - GetSourceByURL(ctx context.Context, sourceUrl string) (*graphModels.AnthroveSource, error) + GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) } diff --git a/pkg/database/graph.go b/pkg/database/graph.go deleted file mode 100644 index 7fc5bf2..0000000 --- a/pkg/database/graph.go +++ /dev/null @@ -1,123 +0,0 @@ -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) - } -} diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index 70ae07a..749a337 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -89,10 +89,10 @@ CREATE TABLE "UserFavorites" CREATE TABLE "UserSource" ( - user_id TEXT REFERENCES "User" (id), - source_id TEXT REFERENCES "Source" (id), - account_username TEXT, - account_id TEXT, + user_id TEXT REFERENCES "User" (id), + source_id TEXT REFERENCES "Source" (id), + scrape_time_interval TEXT account_username TEXT, + account_id TEXT, PRIMARY KEY (user_id, source_id), UNIQUE (account_username, account_id) ); diff --git a/pkg/models/README.md b/pkg/models/README.md deleted file mode 100644 index af61315..0000000 --- a/pkg/models/README.md +++ /dev/null @@ -1,84 +0,0 @@ -# Postgres - -https://www.dbdiagram.io/d - -```` -Table User { - id string [primary key] - created_at timestamp -} - -Table Post { - id varchar(25) [primary key] - rating Rating - created_at timestamp -} - -Enum Rating { - safe - questionable - explicit -} - -Table Source { - id varchar(25) [primary key] - display_name text - domain text [not null, unique] -} - -Table Tag { - name text [primary key] - type TagType -} - -Enum TagType { - general - species - character - artist - lore - meta - invalid -} - -Table TagAlias { - name text [primary key] - tag_id text -} - -Table TagGroup { - name text [primary key] - tag_id text -} - -Table UserFavorites { - user_id text [primary key] - post_id text [primary key] - created_at timestamp -} - -Table UserSource { - user_id text [primary key] - source_id text [primary key] - account_username text - account_id text -} - -Table PostReference { - post_id text [primary key] - source_id text [primary key] - url text [not null, unique] - source_post_id text -} - -Ref: Tag.name > TagAlias.tag_id -Ref: Tag.name > TagGroup.tag_id -Ref: Tag.name <> Post.id -Ref: UserFavorites.user_id > User.id -Ref: UserFavorites.post_id > Post.id -Ref: UserSource.user_id > User.id -Ref: UserSource.source_id > Source.id -Ref: PostReference.post_id > Post.id -Ref: PostReference.source_id > Source.id - -```` \ No newline at end of file diff --git a/pkg/models/pgModels/orm.go b/pkg/models/orm.go similarity index 95% rename from pkg/models/pgModels/orm.go rename to pkg/models/orm.go index 4fd79bb..ff7bb64 100644 --- a/pkg/models/pgModels/orm.go +++ b/pkg/models/orm.go @@ -1,4 +1,4 @@ -package pgModels +package models import ( gonanoid "github.com/matoous/go-nanoid/v2" diff --git a/pkg/models/pgModels/userSource.go b/pkg/models/pgModels/userSource.go deleted file mode 100644 index 04371fe..0000000 --- a/pkg/models/pgModels/userSource.go +++ /dev/null @@ -1,13 +0,0 @@ -package pgModels - -type UserSource struct { - UserID string `gorm:"primaryKey"` - Source Source `gorm:"foreignKey:ID;references:SourceID"` - SourceID string `gorm:"primaryKey"` - AccountUsername string - AccountID string -} - -func (UserSource) TableName() string { - return "UserSource" -} diff --git a/pkg/models/pgModels/post.go b/pkg/models/post.go similarity index 65% rename from pkg/models/pgModels/post.go rename to pkg/models/post.go index d5ea588..bac8a50 100644 --- a/pkg/models/pgModels/post.go +++ b/pkg/models/post.go @@ -1,13 +1,9 @@ -package pgModels - -import ( - "git.dragse.it/anthrove/otter-space-sdk/pkg/models" -) +package models // Post model type Post struct { BaseModel - Rating models.Rating `gorm:"type:enum('safe','questionable','explicit')"` + Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `gorm:"many2many:post_tags;"` Favorites []UserFavorite `gorm:"foreignKey:PostID"` References []PostReference `gorm:"foreignKey:PostID"` diff --git a/pkg/models/pgModels/postReference.go b/pkg/models/postReference.go similarity index 95% rename from pkg/models/pgModels/postReference.go rename to pkg/models/postReference.go index 8ab7797..999e145 100644 --- a/pkg/models/pgModels/postReference.go +++ b/pkg/models/postReference.go @@ -1,4 +1,4 @@ -package pgModels +package models type PostReference struct { PostID string `gorm:"primaryKey"` diff --git a/pkg/models/pgModels/source.go b/pkg/models/source.go similarity index 95% rename from pkg/models/pgModels/source.go rename to pkg/models/source.go index 7159ea5..0860b81 100644 --- a/pkg/models/pgModels/source.go +++ b/pkg/models/source.go @@ -1,4 +1,4 @@ -package pgModels +package models // Source model type Source struct { diff --git a/pkg/models/pgModels/tag.go b/pkg/models/tag.go similarity index 53% rename from pkg/models/pgModels/tag.go rename to pkg/models/tag.go index 7538ac9..583cd78 100644 --- a/pkg/models/pgModels/tag.go +++ b/pkg/models/tag.go @@ -1,14 +1,12 @@ -package pgModels - -import "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +package models // Tag models type Tag struct { - Name string `gorm:"primaryKey"` - Type models.TagType `gorm:"column:tag_type"` - Aliases []TagAlias `gorm:"foreignKey:TagID"` - Groups []TagGroup `gorm:"foreignKey:TagID"` - Posts []Post `gorm:"many2many:post_tags;"` + Name string `gorm:"primaryKey"` + Type TagType `gorm:"column:tag_type"` + Aliases []TagAlias `gorm:"foreignKey:TagID"` + Groups []TagGroup `gorm:"foreignKey:TagID"` + Posts []Post `gorm:"many2many:post_tags;"` } func (Tag) TableName() string { @@ -34,3 +32,8 @@ type TagGroup struct { func (TagGroup) TableName() string { return "TagGroup" } + +type TagsWithFrequency struct { + Frequency int64 `json:"frequency"` + Tags Tag `json:"tags"` +} diff --git a/pkg/models/pgModels/user.go b/pkg/models/user.go similarity index 92% rename from pkg/models/pgModels/user.go rename to pkg/models/user.go index 1120ae7..96dd70e 100644 --- a/pkg/models/pgModels/user.go +++ b/pkg/models/user.go @@ -1,4 +1,4 @@ -package pgModels +package models // User model type User struct { diff --git a/pkg/models/pgModels/userFavorite.go b/pkg/models/userFavorite.go similarity index 69% rename from pkg/models/pgModels/userFavorite.go rename to pkg/models/userFavorite.go index e01093f..1393c61 100644 --- a/pkg/models/pgModels/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -1,4 +1,4 @@ -package pgModels +package models import "time" @@ -11,3 +11,7 @@ type UserFavorite struct { func (UserFavorite) TableName() string { return "UserFavorites" } + +type FavoriteList struct { + Posts []UserFavorite `json:"posts,omitempty"` +} diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go new file mode 100644 index 0000000..97793d6 --- /dev/null +++ b/pkg/models/userSource.go @@ -0,0 +1,14 @@ +package models + +type UserSource struct { + UserID string `gorm:"primaryKey"` + Source Source `gorm:"foreignKey:ID;references:SourceID"` + SourceID string `gorm:"primaryKey"` + ScrapeTimeInterval string + AccountUsername string + AccountID string +} + +func (UserSource) TableName() string { + return "UserSource" +}