refactor(model): changed types of model

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-05-31 16:03:35 +02:00
parent e3a5adb2cc
commit 76f3c8d97e
10 changed files with 31 additions and 28 deletions

View File

@ -10,7 +10,7 @@ import (
) )
// CreateAnthrovePostToSourceLink establishes a link between a models.AnthrovePost and a models.AnthroveSource // 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 := ` query := `
MATCH (sourceNode:Source {domain: $source_url}) MATCH (sourceNode:Source {domain: $source_url})
MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id}) MATCH (postNode:AnthrovePost {post_id: $anthrove_post_id})

View File

@ -86,9 +86,9 @@ func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]m
} }
sources = append(sources, models.AnthroveSource{ sources = append(sources, models.AnthroveSource{
DisplayName: source.Props["display_name"].(string), DisplayName: models.AnthroveSourceDisplayName(source.Props["display_name"].(string)),
Domain: source.Props["domain"].(string), Domain: models.AnthroveSourceDomain(source.Props["domain"].(string)),
Icon: source.Props["icon"].(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 // 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 var source models.AnthroveSource
query := ` query := `
@ -126,9 +126,9 @@ func GetSourceNodesByURL(ctx context.Context, driver neo4j.DriverWithContext, so
return nil, err return nil, err
} }
source.DisplayName = record.Props["display_name"].(string) source.DisplayName = models.AnthroveSourceDisplayName(record.Props["display_name"].(string))
source.Domain = record.Props["domain"].(string) source.Domain = models.AnthroveSourceDomain(record.Props["domain"].(string))
source.Icon = record.Props["icon"].(string) source.Icon = models.AnthroveSourceIcon(record.Props["icon"].(string))
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"source_url": sourceUrl, "source_url": sourceUrl,

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 models.AnthroveSourceDomain, userID string, username string) error {
query := ` query := `
MATCH (sourceNode:Source {domain: $source_domain}) 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) 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, UserID: sourceUserID,
Username: sourceUsername, Username: sourceUsername,
Source: models.AnthroveSource{ Source: models.AnthroveSource{
DisplayName: displayName, DisplayName: models.AnthroveSourceDisplayName(displayName),
Domain: domain, Domain: models.AnthroveSourceDomain(domain),
Icon: icon, Icon: models.AnthroveSourceIcon(icon),
}, },
} }
userSource[displayName] = anthroveSourceUser userSource[displayName] = anthroveSourceUser
@ -149,7 +149,7 @@ func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anth
return userSource, nil 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) userSource := make(map[string]models.AnthroveUserRelationship)
@ -194,9 +194,9 @@ func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithCont
UserID: sourceUserID, UserID: sourceUserID,
Username: sourceUsername, Username: sourceUsername,
Source: models.AnthroveSource{ Source: models.AnthroveSource{
DisplayName: displayName, DisplayName: models.AnthroveSourceDisplayName(displayName),
Domain: domain, Domain: models.AnthroveSourceDomain(domain),
Icon: icon, Icon: models.AnthroveSourceIcon(icon),
}, },
} }
userSource[displayName] = anthroveSourceUser userSource[displayName] = anthroveSourceUser
@ -256,9 +256,9 @@ func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthro
}) })
userSources = models.AnthroveSource{ userSources = models.AnthroveSource{
DisplayName: utils.GetOrDefault(source.Props, "display_name", "").(string), DisplayName: models.AnthroveSourceDisplayName(utils.GetOrDefault(source.Props, "display_name", "").(string)),
Domain: utils.GetOrDefault(source.Props, "domain", "").(string), Domain: models.AnthroveSourceDomain(utils.GetOrDefault(source.Props, "domain", "").(string)),
Icon: utils.GetOrDefault(source.Props, "icon", "").(string), Icon: models.AnthroveSourceIcon(utils.GetOrDefault(source.Props, "icon", "").(string)),
} }
anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string)) anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string))

View File

@ -35,6 +35,6 @@ func (g *graphConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, so
} }
// LinkPostWithSource establishes a link between a models.AnthrovePost and a models.AnthroveSource // 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) return relations.CreateAnthrovePostToSourceLink(ctx, g.driver, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship)
} }

View File

@ -15,7 +15,7 @@ type Source interface {
DeleteSource(ctx context.Context, anthroveSource *models.AnthroveSource) error DeleteSource(ctx context.Context, anthroveSource *models.AnthroveSource) error
// GetSourceByURL returns the first models.AnthroveSource provided by the database // 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. // 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. // It returns a map of source domains to user-source relationships, and an error if the operation fails.

View File

@ -19,7 +19,7 @@ func (g *graphConnection) DeleteSource(ctx context.Context, anthroveSource *mode
} }
// GetSourceByURL returns the first models.AnthroveSource provided by the database // 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) 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) 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) return user.GetSpecifiedUserSourceLink(ctx, g.driver, anthroveUserID, sourceDisplayName)
} }

View File

@ -11,7 +11,7 @@ type User interface {
DeleteUser(ctx context.Context, anthroveUserID models.AnthroveUserID) error DeleteUser(ctx context.Context, anthroveUserID models.AnthroveUserID) error
// CreateUserWithRelationToSource will create or get a user to link it with a given source. // 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 creates a link between a user and a post in the OtterSpace graph.
LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error

View File

@ -19,7 +19,7 @@ func (g *graphConnection) DeleteUser(ctx context.Context, anthroveUserID models.
return fmt.Errorf("not implemented") 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) return user.CreateUserNodeWithSourceRelation(ctx, g.driver, anthroveUserID, sourceDomain, userID, username)
} }

View File

@ -3,6 +3,9 @@ package models
type AnthroveUserID string type AnthroveUserID string
type AnthrovePostID string type AnthrovePostID string
type AnthroveRating string type AnthroveRating string
type AnthroveSourceDomain string
type AnthroveSourceIcon string
type AnthroveSourceDisplayName string
const ( const (
SFW AnthroveRating = "s" SFW AnthroveRating = "s"

View File

@ -1,7 +1,7 @@
package models package models
type AnthroveSource struct { type AnthroveSource struct {
DisplayName string `json:"display_name" format:"string"` DisplayName AnthroveSourceDisplayName `json:"display_name" format:"string"`
Domain string `json:"domain" format:"string"` Domain AnthroveSourceDomain `json:"domain" format:"string"`
Icon string `json:"icon" format:"string"` Icon AnthroveSourceIcon `json:"icon" format:"string"`
} }