From 76f3c8d97e0767f3813e7eafe734d24b5a221fb4 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 31 May 2024 16:03:35 +0200 Subject: [PATCH] refactor(model): changed types of model Signed-off-by: SoXX --- internal/relations/relationships.go | 2 +- internal/source/source.go | 14 +++++++------- internal/user/user.go | 22 +++++++++++----------- pkg/graph/post_impl.go | 2 +- pkg/graph/source.go | 2 +- pkg/graph/source_impl.go | 4 ++-- pkg/graph/user.go | 2 +- pkg/graph/user_impl.go | 2 +- pkg/models/const.go | 3 +++ pkg/models/source.go | 6 +++--- 10 files changed, 31 insertions(+), 28 deletions(-) diff --git a/internal/relations/relationships.go b/internal/relations/relationships.go index 81856f7..21aec4d 100644 --- a/internal/relations/relationships.go +++ b/internal/relations/relationships.go @@ -10,7 +10,7 @@ import ( ) // CreateAnthrovePostToSourceLink establishes a link between a models.AnthrovePost and a models.AnthroveSource -func CreateAnthrovePostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error { +func CreateAnthrovePostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveSourceDomain models.AnthroveSourceDomain, anthrovePostRelationship *models.AnthrovePostRelationship) error { query := ` MATCH (sourceNode:Source {domain: $source_url}) MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) diff --git a/internal/source/source.go b/internal/source/source.go index 1bfdc9c..4120131 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -86,9 +86,9 @@ func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]m } sources = append(sources, models.AnthroveSource{ - DisplayName: source.Props["display_name"].(string), - Domain: source.Props["domain"].(string), - Icon: source.Props["icon"].(string), + DisplayName: models.AnthroveSourceDisplayName(source.Props["display_name"].(string)), + Domain: models.AnthroveSourceDomain(source.Props["domain"].(string)), + Icon: models.AnthroveSourceIcon(source.Props["icon"].(string)), }) } @@ -101,7 +101,7 @@ func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]m } // GetSourceNodesByURL returns the first models.AnthroveSource provided by the database -func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl string) (*models.AnthroveSource, error) { +func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, sourceUrl models.AnthroveSourceDomain) (*models.AnthroveSource, error) { var source models.AnthroveSource query := ` @@ -126,9 +126,9 @@ func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, so return nil, err } - source.DisplayName = record.Props["display_name"].(string) - source.Domain = record.Props["domain"].(string) - source.Icon = record.Props["icon"].(string) + source.DisplayName = models.AnthroveSourceDisplayName(record.Props["display_name"].(string)) + source.Domain = models.AnthroveSourceDomain(record.Props["domain"].(string)) + source.Icon = models.AnthroveSourceIcon(record.Props["icon"].(string)) log.WithFields(log.Fields{ "source_url": sourceUrl, diff --git a/internal/user/user.go b/internal/user/user.go index 6439149..c436c61 100644 --- a/internal/user/user.go +++ b/internal/user/user.go @@ -10,7 +10,7 @@ import ( 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 models.AnthroveSourceDomain, userID string, username string) error { query := ` MATCH (sourceNode:Source {domain: $source_domain}) MERGE (:User {user_id: $anthrove_user_id})-[r:HAS_ACCOUNT_AT {username: $source_user_name, user_id: $source_user_id}]->(sourceNode) @@ -133,9 +133,9 @@ func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anth UserID: sourceUserID, Username: sourceUsername, Source: models.AnthroveSource{ - DisplayName: displayName, - Domain: domain, - Icon: icon, + DisplayName: models.AnthroveSourceDisplayName(displayName), + Domain: models.AnthroveSourceDomain(domain), + Icon: models.AnthroveSourceIcon(icon), }, } userSource[displayName] = anthroveSourceUser @@ -149,7 +149,7 @@ func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anth return userSource, nil } -func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) { +func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDisplayName models.AnthroveSourceDisplayName) (map[string]models.AnthroveUserRelationship, error) { userSource := make(map[string]models.AnthroveUserRelationship) @@ -194,9 +194,9 @@ func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithCont UserID: sourceUserID, Username: sourceUsername, Source: models.AnthroveSource{ - DisplayName: displayName, - Domain: domain, - Icon: icon, + DisplayName: models.AnthroveSourceDisplayName(displayName), + Domain: models.AnthroveSourceDomain(domain), + Icon: models.AnthroveSourceIcon(icon), }, } userSource[displayName] = anthroveSourceUser @@ -256,9 +256,9 @@ func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthro }) userSources = models.AnthroveSource{ - DisplayName: utils.GetOrDefault(source.Props, "display_name", "").(string), - Domain: utils.GetOrDefault(source.Props, "domain", "").(string), - Icon: utils.GetOrDefault(source.Props, "icon", "").(string), + DisplayName: models.AnthroveSourceDisplayName(utils.GetOrDefault(source.Props, "display_name", "").(string)), + Domain: models.AnthroveSourceDomain(utils.GetOrDefault(source.Props, "domain", "").(string)), + Icon: models.AnthroveSourceIcon(utils.GetOrDefault(source.Props, "icon", "").(string)), } anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string)) diff --git a/pkg/graph/post_impl.go b/pkg/graph/post_impl.go index 1cef4ee..f521d2e 100644 --- a/pkg/graph/post_impl.go +++ b/pkg/graph/post_impl.go @@ -35,6 +35,6 @@ func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, so } // LinkPostWithSource establishes a link between a models.AnthrovePost and a models.AnthroveSource -func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.AnthrovePostRelationship) error { +func (g *graphConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain models.AnthroveSourceDomain, anthrovePostRelationship *models.AnthrovePostRelationship) error { return relations.CreateAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship) } diff --git a/pkg/graph/source.go b/pkg/graph/source.go index 63f8a09..ddadc28 100644 --- a/pkg/graph/source.go +++ b/pkg/graph/source.go @@ -15,7 +15,7 @@ type Source interface { DeleteSource(ctx context.Context, anthroveSource *models.AnthroveSource) error // GetSourceByURL returns the first models.AnthroveSource provided by the database - GetSourceByURL(ctx context.Context, sourceUrl string) (*models.AnthroveSource, error) + GetSourceByURL(ctx context.Context, sourceUrl models.AnthroveSourceDomain) (*models.AnthroveSource, error) // GetSourceLinkForUser retrieves the links between a user and sources in the OtterSpace graph. // It returns a map of source domains to user-source relationships, and an error if the operation fails. diff --git a/pkg/graph/source_impl.go b/pkg/graph/source_impl.go index 7e03aa5..b2013e5 100644 --- a/pkg/graph/source_impl.go +++ b/pkg/graph/source_impl.go @@ -19,7 +19,7 @@ func (g *graphConnection) DeleteSource(ctx context.Context, anthroveSource *mode } // GetSourceByURL returns the first models.AnthroveSource provided by the database -func (g *graphConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.AnthroveSource, error) { +func (g *graphConnection) GetSourceByURL(ctx context.Context, sourceUrl models.AnthroveSourceDomain) (*models.AnthroveSource, error) { return source.GetSourceNodesByURL(ctx, g.driver, sourceUrl) } @@ -27,7 +27,7 @@ func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveS return source.GetAllSourceNodes(ctx, g.driver) } -func (g *graphConnection) GetSourceLinkForSpecifiedUser(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) { +func (g *graphConnection) GetSourceLinkForSpecifiedUser(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName models.AnthroveSourceDisplayName) (map[string]models.AnthroveUserRelationship, error) { return user.GetSpecifiedUserSourceLink(ctx, g.driver, anthroveUserID, sourceDisplayName) } diff --git a/pkg/graph/user.go b/pkg/graph/user.go index ac70d3d..7b4051b 100644 --- a/pkg/graph/user.go +++ b/pkg/graph/user.go @@ -11,7 +11,7 @@ type User interface { DeleteUser(ctx context.Context, anthroveUserID models.AnthroveUserID) error // CreateUserWithRelationToSource will create or get a user to link it with a given source. - CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error + CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain models.AnthroveSourceDomain, userID string, username string) error // LinkUserWithPost creates a link between a user and a post in the OtterSpace graph. LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error diff --git a/pkg/graph/user_impl.go b/pkg/graph/user_impl.go index 720896f..da1be48 100644 --- a/pkg/graph/user_impl.go +++ b/pkg/graph/user_impl.go @@ -19,7 +19,7 @@ func (g *graphConnection) DeleteUser(ctx context.Context, anthroveUserID models. return fmt.Errorf("not implemented") } -func (g *graphConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { +func (g *graphConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain models.AnthroveSourceDomain, userID string, username string) error { return user.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username) } diff --git a/pkg/models/const.go b/pkg/models/const.go index 9dd312e..e8dbe2e 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -3,6 +3,9 @@ package models type AnthroveUserID string type AnthrovePostID string type AnthroveRating string +type AnthroveSourceDomain string +type AnthroveSourceIcon string +type AnthroveSourceDisplayName string const ( SFW AnthroveRating = "s" diff --git a/pkg/models/source.go b/pkg/models/source.go index 27a4835..95e4140 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -1,7 +1,7 @@ package models type AnthroveSource struct { - DisplayName string `json:"display_name" format:"string"` - Domain string `json:"domain" format:"string"` - Icon string `json:"icon" format:"string"` + DisplayName AnthroveSourceDisplayName `json:"display_name" format:"string"` + Domain AnthroveSourceDomain `json:"domain" format:"string"` + Icon AnthroveSourceIcon `json:"icon" format:"string"` }