Compare commits

..

2 Commits

Author SHA1 Message Date
b0d98d842f refactor(postgres): renamed function names and fixed tests and other issues
Signed-off-by: soxx <soxx@fenpa.ws>
2024-06-23 22:35:46 +02:00
8a0229eb23 test(postgres): renamed functions to be more clear
Signed-off-by: soxx <soxx@fenpa.ws>
2024-06-23 21:23:38 +02:00
14 changed files with 245 additions and 222 deletions

View File

@ -23,7 +23,7 @@ func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) err
return nil return nil
} }
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string) (*models.Post, error) { func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
if anthrovePostID == "" { if anthrovePostID == "" {
return nil, fmt.Errorf("anthrovePostID is required") return nil, fmt.Errorf("anthrovePostID is required")
} }
@ -37,7 +37,7 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string
return &post, nil return &post, nil
} }
func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) {
var post models.Post var post models.Post
err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1`, sourceURL).First(&post).Error err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1`, sourceURL).First(&post).Error
@ -51,7 +51,7 @@ func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*mo
return &post, nil return &post, nil
} }
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (*models.Post, error) { func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) {
var post models.Post var post models.Post
err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.source_id = $1 LIMIT 1`, sourceID).First(&post).Error err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.source_id = $1 LIMIT 1`, sourceID).First(&post).Error

View File

@ -99,7 +99,7 @@ func TestGetPostByAnthroveID(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID string anthrovePostID models.AnthrovePostID
} }
tests := []struct { tests := []struct {
name string name string
@ -112,7 +112,7 @@ func TestGetPostByAnthroveID(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: post.ID, anthrovePostID: models.AnthrovePostID(post.ID),
}, },
want: post, want: post,
wantErr: false, wantErr: false,
@ -181,12 +181,12 @@ func TestGetPostBySourceURL(t *testing.T) {
Icon: "https://e621.net/icon.ico", Icon: "https://e621.net/icon.ico",
} }
err = CreateSourceNode(ctx, gormDB, &source) err = CreateSource(ctx, gormDB, &source)
if err != nil { if err != nil {
t.Fatal("Could not create source", err) t.Fatal("Could not create source", err)
} }
err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain) err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain))
if err != nil { if err != nil {
t.Fatal("Could not create source reference", err) t.Fatal("Could not create source reference", err)
} }
@ -236,13 +236,13 @@ func TestGetPostBySourceURL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL) got, err := GetPostByURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetPostByURL() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !checkPost(got, tt.want) { if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want) t.Errorf("GetPostByURL() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -278,12 +278,12 @@ func TestGetPostBySourceID(t *testing.T) {
Icon: "https://e621.net/icon.ico", Icon: "https://e621.net/icon.ico",
} }
err = CreateSourceNode(ctx, gormDB, &source) err = CreateSource(ctx, gormDB, &source)
if err != nil { if err != nil {
t.Fatal("Could not create source", err) t.Fatal("Could not create source", err)
} }
err = EstablishAnthrovePostToSourceLink(ctx, gormDB, post.ID, source.Domain) err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain))
if err != nil { if err != nil {
t.Fatal("Could not create source reference", err) t.Fatal("Could not create source reference", err)
} }
@ -292,7 +292,7 @@ func TestGetPostBySourceID(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
sourceID string sourceID models.AnthroveSourceID
} }
tests := []struct { tests := []struct {
@ -306,7 +306,7 @@ func TestGetPostBySourceID(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
sourceID: source.ID, sourceID: models.AnthroveSourceID(source.ID),
}, },
want: post, want: post,
wantErr: false, wantErr: false,

View File

@ -7,7 +7,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID string, sourceDomain string) error { func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error {
var source models.Source var source models.Source
var err error var err error
@ -19,9 +19,9 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
// Establish the relationship // Establish the relationship
err = db.WithContext(ctx).Create(models.PostReference{ err = db.WithContext(ctx).Create(models.PostReference{
PostID: anthrovePostID, PostID: string(anthrovePostID),
SourceID: source.ID, SourceID: source.ID,
URL: sourceDomain, URL: string(sourceDomain),
}).Error }).Error
if err != nil { if err != nil {
@ -36,7 +36,7 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
return nil return nil
} }
func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) error { func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
userFavorite := models.UserFavorite{ userFavorite := models.UserFavorite{
UserID: string(anthroveUserID), UserID: string(anthroveUserID),
PostID: string(anthrovePostID), PostID: string(anthrovePostID),
@ -55,7 +55,7 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID st
return nil return nil
} }
func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) (bool, error) { func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
var count int64 var count int64
err := db.WithContext(ctx).Model(&models.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 { if err != nil {

View File

@ -37,7 +37,7 @@ func TestCheckUserToPostLink(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -46,8 +46,8 @@ func TestCheckUserToPostLink(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID string anthroveUserID models.AnthroveUserID
anthrovePostID string anthrovePostID models.AnthrovePostID
} }
tests := []struct { tests := []struct {
name string name string
@ -61,7 +61,7 @@ func TestCheckUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
anthrovePostID: post.ID, anthrovePostID: models.AnthrovePostID(post.ID),
}, },
want: true, want: true,
wantErr: false, wantErr: false,
@ -83,7 +83,7 @@ func TestCheckUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "123", anthroveUserID: "123",
anthrovePostID: post.ID, anthrovePostID: models.AnthrovePostID(post.ID),
}, },
want: false, want: false,
wantErr: false, wantErr: false,
@ -102,13 +102,13 @@ func TestCheckUserToPostLink(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := CheckUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID) got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("CheckUserToPostLink() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if got != tt.want { if got != tt.want {
t.Errorf("CheckUserToPostLink() got = %v, want %v", got, tt.want) t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -141,7 +141,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
Domain: "e621.net", Domain: "e621.net",
Icon: "icon.e621.net", Icon: "icon.e621.net",
} }
err = CreateSourceNode(ctx, gormDB, source) err = CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -150,8 +150,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthrovePostID string anthrovePostID models.AnthrovePostID
sourceDomain string sourceDomain models.AnthroveSourceDomain
anthrovePostRelationship *models.PostReference anthrovePostRelationship *models.PostReference
} }
tests := []struct { tests := []struct {
@ -164,7 +164,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthrovePostID: post.ID, anthrovePostID: models.AnthrovePostID(post.ID),
sourceDomain: "e621.net", sourceDomain: "e621.net",
}, },
wantErr: false, wantErr: false,
@ -202,8 +202,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := EstablishAnthrovePostToSourceLink(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { if err := CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr {
t.Errorf("EstablishAnthrovePostToSourceLink() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }
@ -240,8 +240,8 @@ func TestEstablishUserToPostLink(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID string anthroveUserID models.AnthroveUserID
anthrovePostID string anthrovePostID models.AnthrovePostID
} }
tests := []struct { tests := []struct {
name string name string
@ -254,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
anthrovePostID: post.ID, anthrovePostID: models.AnthrovePostID(post.ID),
}, },
wantErr: false, wantErr: false,
}, },
@ -291,8 +291,8 @@ func TestEstablishUserToPostLink(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := EstablishUserToPostLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { if err := CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr {
t.Errorf("EstablishUserToPostLink() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }

View File

@ -9,8 +9,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// CreateSourceNode creates a pgModels.Source // CreateSource creates a pgModels.Source
func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error { func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error {
if anthroveSource.Domain == "" { if anthroveSource.Domain == "" {
return fmt.Errorf("anthroveSource domain is required") return fmt.Errorf("anthroveSource domain is required")
@ -31,8 +31,8 @@ func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *models.S
return nil return nil
} }
// GetAllSourceNodes returns a list of all pgModels.Source // GetAllSource returns a list of all pgModels.Source
func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]models.Source, error) { func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) {
var sources []models.Source var sources []models.Source
result := db.WithContext(ctx).Find(&sources) result := db.WithContext(ctx).Find(&sources)
@ -48,15 +48,15 @@ func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]models.Source, error
return sources, nil return sources, nil
} }
// GetSourceNodesByURL returns the first source it finds based on the domain // GetSourceByDomain returns the first source it finds based on the domain
func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*models.Source, error) { func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
var sources models.Source var sources models.Source
if domain == "" { if sourceDomain == "" {
return nil, fmt.Errorf("domain is required") return nil, fmt.Errorf("domain is required")
} }
result := db.WithContext(ctx).Where("domain = ?", domain).First(&sources) result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources)
if result.Error != nil { if result.Error != nil {
return nil, result.Error return nil, result.Error

View File

@ -70,8 +70,8 @@ func TestCreateSourceNode(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CreateSourceNode(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr { if err := CreateSource(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr {
t.Errorf("CreateSourceNode() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }
@ -107,7 +107,7 @@ func TestGetAllSourceNodes(t *testing.T) {
} }
for _, source := range sources { for _, source := range sources {
err = CreateSourceNode(ctx, gormDB, &source) err = CreateSource(ctx, gormDB, &source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -136,13 +136,13 @@ func TestGetAllSourceNodes(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetAllSourceNodes(tt.args.ctx, tt.args.db) got, err := GetAllSource(tt.args.ctx, tt.args.db)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetAllSourceNodes() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !checkSourcesNode(got, tt.want) { if !checkSourcesNode(got, tt.want) {
t.Errorf("GetAllSourceNodes() got = %v, want %v", got, tt.want) t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -165,7 +165,7 @@ func TestGetSourceNodesByURL(t *testing.T) {
Icon: "icon.e621.net", Icon: "icon.e621.net",
} }
err = CreateSourceNode(ctx, gormDB, source) err = CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -174,7 +174,7 @@ func TestGetSourceNodesByURL(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
domain string domain models.AnthroveSourceDomain
} }
tests := []struct { tests := []struct {
name string name string
@ -215,13 +215,13 @@ func TestGetSourceNodesByURL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetSourceNodesByURL(tt.args.ctx, tt.args.db, tt.args.domain) got, err := GetSourceByDomain(tt.args.ctx, tt.args.db, tt.args.domain)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetSourceNodesByURL() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !checkSourceNode(got, tt.want) { if !checkSourceNode(got, tt.want) {
t.Errorf("GetSourceNodesByURL() got = %v, want %v", got, tt.want) t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
} }
}) })
} }

View File

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func createTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
resultTag := db.WithContext(ctx).Where(tag).Create(tag) resultTag := db.WithContext(ctx).Where(tag).Create(tag)
@ -24,7 +24,7 @@ func createTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
return nil return nil
} }
func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID string, tag *models.Tag) error { func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *models.Tag) error {
if PostID == "" { if PostID == "" {
return fmt.Errorf("PostID is empty") return fmt.Errorf("PostID is empty")

View File

@ -41,7 +41,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
PostID string PostID models.AnthrovePostID
tag *models.Tag tag *models.Tag
} }
tests := []struct { tests := []struct {
@ -54,7 +54,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: post.ID, PostID: models.AnthrovePostID(post.ID),
tag: tag, tag: tag,
}, },
wantErr: false, wantErr: false,
@ -64,7 +64,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
PostID: post.ID, PostID: models.AnthrovePostID(post.ID),
tag: nil, tag: nil,
}, },
wantErr: true, wantErr: true,
@ -92,8 +92,8 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CreateTagNodeWitRelation(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr {
t.Errorf("CreateTagNodeWitRelation() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateTagAndReferenceToPost() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }
@ -126,7 +126,7 @@ func TestGetTags(t *testing.T) {
} }
for _, tag := range tags { for _, tag := range tags {
err = createTag(ctx, gormDB, &tag) err = CreateTag(ctx, gormDB, &tag)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID string) error { func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
if anthroveUserID == "" { if anthroveUserID == "" {
return fmt.Errorf("anthroveUserID cannot be empty") return fmt.Errorf("anthroveUserID cannot be empty")
@ -31,51 +31,59 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID string) error {
return nil return nil
} }
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID string, sourceDomain string, userID string, username string) error { func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
if anthroveUserID == "" || username == "" || userID == "" { if anthroveUserID == "" {
return fmt.Errorf("anthroveUserID cannot be empty") return fmt.Errorf("anthroveUserID cannot be empty")
} }
if accountId == "" {
return fmt.Errorf("account_id cannot be empty")
}
if accountUsername == "" {
return fmt.Errorf("account_username cannot be empty")
}
err := CreateUser(ctx, db, anthroveUserID) err := CreateUser(ctx, db, anthroveUserID)
if err != nil { if err != nil {
return err return err
} }
source := models.Source{ source := models.Source{
Domain: sourceDomain, BaseModel: models.BaseModel{ID: string(sourceID)},
} }
if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil { if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"source_domain": sourceDomain, "source_id": sourceID,
}).Error("database: failed to find source") }).Error("database: failed to find source")
return err return err
} }
userSource := models.UserSource{ userSource := models.UserSource{
User: models.User{BaseModel: models.BaseModel{ID: anthroveUserID}}, User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}},
SourceID: source.ID, SourceID: source.ID,
AccountUsername: username, AccountUsername: accountUsername,
AccountID: userID, AccountID: accountId,
UserID: anthroveUserID, UserID: string(anthroveUserID),
} }
if err := db.WithContext(ctx).FirstOrCreate(&userSource).Error; err != nil { if err := db.WithContext(ctx).FirstOrCreate(&userSource).Error; err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_domain": sourceDomain, "source_id": sourceID,
"account_username": username, "account_username": accountUsername,
"account_id": userID, "account_id": accountId,
}).Error("database: failed to create user-source relationship") }).Error("database: failed to create user-source relationship")
return err return err
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_domain": sourceDomain, "source_id": sourceID,
"account_username": username, "account_username": accountUsername,
"account_id": userID, "account_id": accountId,
}).Trace("database: created user-source relationship") }).Trace("database: created user-source relationship")
return nil return nil
@ -143,19 +151,23 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
return userSourceMap, nil return userSourceMap, nil
} }
func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
if anthroveUserID == "" || sourceDisplayName == "" { if anthroveUserID == "" {
return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty") return nil, fmt.Errorf("anthroveUserID cannot be empty")
}
if sourceID == "" {
return nil, fmt.Errorf("sourceID cannot be empty")
} }
var userSources []models.UserSource var userSources []models.UserSource
userSourceMap := make(map[string]models.UserSource) userSourceMap := make(map[string]models.UserSource)
err := db.WithContext(ctx).Model(&models.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("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_display_name": sourceDisplayName, "source_id": sourceID,
}).Error("database: failed to get specified user source link") }).Error("database: failed to get specified user source link")
return nil, err return nil, err
} }
@ -183,15 +195,15 @@ func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_display_name": sourceDisplayName, "source_id": sourceID,
}).Trace("database: got specified user source link") }).Trace("database: got specified user source link")
return userSourceMap, nil return userSourceMap, nil
} }
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) { func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) {
var users []models.User var users []models.User
var userIDs []string var userIDs []models.AnthroveUserID
err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error
if err != nil { if err != nil {
@ -200,7 +212,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) {
} }
for _, user := range users { for _, user := range users {
userIDs = append(userIDs, user.ID) userIDs = append(userIDs, models.AnthroveUserID(user.ID))
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -210,7 +222,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) {
return userIDs, nil return userIDs, nil
} }
func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID string, skip int, limit int) (*models.FavoriteList, error) { func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
var userFavorites []models.UserFavorite var userFavorites []models.UserFavorite
var favoritePosts []models.Post var favoritePosts []models.Post
@ -249,7 +261,7 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthrov
return &models.FavoriteList{Posts: favoritePosts}, nil return &models.FavoriteList{Posts: favoritePosts}, nil
} }
func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
var userFavorites []models.UserFavorite var userFavorites []models.UserFavorite
err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error
if err != nil { if err != nil {

View File

@ -25,7 +25,7 @@ func TestCreateUser(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID string anthroveUserID models.AnthroveUserID
} }
tests := []struct { tests := []struct {
name string name string
@ -72,11 +72,12 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
// Setup Test // Setup Test
source := &models.Source{ source := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
Icon: "icon.e621.net", Icon: "icon.e621.net",
} }
err = CreateSourceNode(ctx, gormDB, source) err = CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -85,8 +86,8 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID string anthroveUserID models.AnthroveUserID
sourceDomain string sourceID models.AnthroveSourceID
userID string userID string
username string username string
} }
@ -96,24 +97,24 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Test 1: Valid anthroveUserID, sourceDomain, userID, username", name: "Test 1: Valid anthroveUserID, sourceID, userID, username",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: source.Domain, sourceID: models.AnthroveSourceID(source.ID),
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Invalid anthroveUserID, valid sourceDomain, userID, username", name: "Test 2: Invalid anthroveUserID, valid sourceID, userID, username",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "2", anthroveUserID: "2",
sourceDomain: source.Domain, sourceID: models.AnthroveSourceID(source.ID),
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -125,19 +126,19 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceDomain: source.Domain, sourceID: models.AnthroveSourceID(source.ID),
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 4: invalid sourceDomain", name: "Test 4: invalid sourceID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: "fa.net", sourceID: "fa.net",
userID: "e1", userID: "e1",
username: "marius", username: "marius",
}, },
@ -149,7 +150,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: source.Domain, sourceID: models.AnthroveSourceID(source.ID),
userID: "", userID: "",
username: "marius", username: "marius",
}, },
@ -161,7 +162,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDomain: source.Domain, sourceID: models.AnthroveSourceID(source.ID),
userID: "aa", userID: "aa",
username: "", username: "",
}, },
@ -170,8 +171,8 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CreateUserNodeWithSourceRelation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDomain, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr {
t.Errorf("CreateUserNodeWithSourceRelation() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
} }
@ -188,7 +189,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
// Setup Test // Setup Test
users := []string{"1", "2", "3"} users := []models.AnthroveUserID{"1", "2", "3"}
for _, user := range users { for _, user := range users {
err = CreateUser(ctx, gormDB, user) err = CreateUser(ctx, gormDB, user)
@ -205,7 +206,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
want []string want []models.AnthroveUserID
wantErr bool wantErr bool
}{ }{
{ {
@ -232,7 +233,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
} }
} }
func TestGetSpecifiedUserSourceLink(t *testing.T) { func TestGetUserSourceBySourceID(t *testing.T) {
// Setup trow away container // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
@ -254,16 +255,17 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
} }
source := &models.Source{ source := &models.Source{
BaseModel: models.BaseModel{ID: expectedResult["e621"].Source.ID},
DisplayName: expectedResult["e621"].Source.DisplayName, DisplayName: expectedResult["e621"].Source.DisplayName,
Domain: expectedResult["e621"].Source.Domain, Domain: expectedResult["e621"].Source.Domain,
} }
err = CreateSourceNode(ctx, gormDB, source) err = CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", source.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -273,7 +275,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID models.AnthroveUserID anthroveUserID models.AnthroveUserID
sourceDisplayName string sourceID models.AnthroveSourceID
} }
tests := []struct { tests := []struct {
name string name string
@ -282,45 +284,45 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Test 1: Valid AnthroveUserID and SourceDisplayName", name: "Test 1: Valid AnthroveUserID and sourceID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDisplayName: "e621", sourceID: models.AnthroveSourceID(source.ID),
}, },
want: expectedResult, want: expectedResult,
wantErr: false, wantErr: false,
}, },
{ {
name: "Test 2: Invalid AnthroveUserID and valid SourceDisplayName", name: "Test 2: Invalid AnthroveUserID and valid sourceID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "22", anthroveUserID: "22",
sourceDisplayName: "e621", sourceID: models.AnthroveSourceID(source.ID),
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 3: Valid AnthroveUserID and invalid SourceDisplayName", name: "Test 3: Valid AnthroveUserID and invalid sourceID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDisplayName: "fa", sourceID: "fa",
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{ {
name: "Test 4: No AnthroveUserID and Valid SourceDisplayName", name: "Test 4: No AnthroveUserID and Valid sourceID",
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceDisplayName: "e621", sourceID: models.AnthroveSourceID(source.ID),
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
@ -331,7 +333,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "1", anthroveUserID: "1",
sourceDisplayName: "", sourceID: "",
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
@ -342,7 +344,7 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
anthroveUserID: "", anthroveUserID: "",
sourceDisplayName: "", sourceID: "",
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
@ -350,13 +352,13 @@ func TestGetSpecifiedUserSourceLink(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetSpecifiedUserSourceLink(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceDisplayName) got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetSpecifiedUserSourceLink() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetSpecifiedUserSourceLink() got = %v, want %v", got, tt.want) t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -420,7 +422,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", expectedResultPost.ID) err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -430,7 +432,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
anthroveUserID string anthroveUserID models.AnthroveUserID
skip int skip int
limit int limit int
} }
@ -479,20 +481,20 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetUserFavoriteNodeWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetUserFavoriteNodeWithPagination() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserFavoriteNodeWithPagination() got = %v, want %v", got, tt.want) t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want)
} }
}) })
} }
} }
func TestGetUserFavoritesCount(t *testing.T) { func TestGetUserFavoritesCount(t *testing.T) {
// Setup trow away containert // Setup trow away container
ctx := context.Background() ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx) container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil { if err != nil {
@ -539,7 +541,7 @@ func TestGetUserFavoritesCount(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -613,19 +615,21 @@ func TestGetUserSourceLinks(t *testing.T) {
// Setup Test // Setup Test
eSource := &models.Source{ eSource := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")},
DisplayName: "e621", DisplayName: "e621",
Domain: "e621.net", Domain: "e621.net",
} }
err = CreateSourceNode(ctx, gormDB, eSource) err = CreateSource(ctx, gormDB, eSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
faSource := &models.Source{ faSource := &models.Source{
BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "2")},
DisplayName: "fa", DisplayName: "fa",
Domain: "fa.net", Domain: "fa.net",
} }
err = CreateSourceNode(ctx, gormDB, faSource) err = CreateSource(ctx, gormDB, faSource)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -648,11 +652,11 @@ func TestGetUserSourceLinks(t *testing.T) {
}, },
} }
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", eSource.Domain, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(eSource.ID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = CreateUserNodeWithSourceRelation(ctx, gormDB, "1", faSource.Domain, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(faSource.ID), expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -720,7 +724,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = EstablishUserToPostLink(ctx, gormDB, "1", post.ID) err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -733,7 +737,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
for i, tag := range tags { for i, tag := range tags {
err = CreateTagNodeWitRelation(ctx, gormDB, posts[i].ID, &tag) err = CreateTagAndReferenceToPost(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -788,13 +792,13 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := GetUserTagNodeWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetUserTagNodeWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserTagNodeWitRelationToFavedPosts() got = %v, want %v", got, tt.want) t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want)
} }
}) })
} }

View File

@ -9,60 +9,60 @@ type OtterSpace interface {
// Connect establishes a connection to the database. // Connect establishes a connection to the database.
Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error
// AddUserWithRelationToSource adds a user with a relation to a source. // CreateUserWithRelationToSource adds a user with a relation to a source.
AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error
// AddSource adds a new source to the database. // CreateSource adds a new source to the database.
AddSource(ctx context.Context, anthroveSource *models.Source) error CreateSource(ctx context.Context, anthroveSource *models.Source) error
// AddPost adds a new post to the database. // CreatePost adds a new post to the database.
AddPost(ctx context.Context, anthrovePost *models.Post) error CreatePost(ctx context.Context, anthrovePost *models.Post) error
// AddTagWithRelationToPost adds a tag with a relation to a post. // CreateTagAndReferenceToPost adds a tag with a relation to a post.
AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error
// LinkPostWithSource links a post with a source. // CreateReferenceBetweenPostAndSource links a post with a source.
LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.PostReference) error CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error
// LinkUserWithPost links a user with a post. // CreateReferenceBetweenUserAndPost links a user with a post.
LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthrovePostID, anthrovePostID models.AnthrovePostID) error
// CheckUserPostLink checks if a user-post link exists. // CheckReferenceBetweenUserAndPost checks if a user-post link exists.
CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error)
// GetPostByAnthroveID retrieves a post by its Anthrove ID. // GetPostByAnthroveID retrieves a post by its Anthrove ID.
GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error)
// GetPostBySourceURL retrieves a post by its source URL. // GetPostByURL retrieves a post by its source URL.
GetPostBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error)
// GetPostBySourceID retrieves a post by its source ID. // GetPostBySourceID retrieves a post by its source ID.
GetPostBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error)
// GetUserFavoriteCount retrieves the count of a user's favorites. // GetUserFavoritesCount retrieves the count of a user's favorites.
GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error)
// GetUserSourceLinks retrieves the source links of a user. // GetUserSourceLinks retrieves the source links of a user.
GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error)
// GetSpecifiedUserSourceLink retrieves a specified source link of a user. // GetUserSourceBySourceID retrieves a specified source link of a user.
GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error)
// GetAllAnthroveUserIDs retrieves all Anthrove user IDs. // GetAllAnthroveUserIDs retrieves all Anthrove user IDs.
GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error)
// GetUserFavoritePostsWithPagination retrieves a user's favorite posts with pagination. // GetUserFavoriteWithPagination retrieves a user's favorite posts with pagination.
GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error)
// GetUserTagsTroughFavedPosts retrieves a user's tags through their favorited posts. // GetUserTagWitRelationToFavedPosts retrieves a user's tags through their favorited posts.
GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error)
// GetAllTags retrieves all tags. // GetAllTags retrieves all tags.
GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) GetAllTags(ctx context.Context) ([]models.Tag, error)
// GetAllSources retrieves all sources. // GetAllSources retrieves all sources.
GetAllSources(ctx context.Context) ([]models.Source, error) GetAllSources(ctx context.Context) ([]models.Source, error)
// GetSourceByURL retrieves a source by its URL. // GetSourceByDomain retrieves a source by its URL.
GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error)
} }

View File

@ -64,47 +64,47 @@ func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, usern
return nil return nil
} }
func (p *postgresqlConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
return postgres.CreateUserNodeWithSourceRelation(ctx, p.db, anthroveUserID, sourceDomain, userID, userID) return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceID, accountId, accountUsername)
} }
func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *models.Source) error { func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error {
return postgres.CreateSourceNode(ctx, p.db, anthroveSource) return postgres.CreateSource(ctx, p.db, anthroveSource)
} }
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error { func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error {
return postgres.CreatePost(ctx, p.db, anthrovePost) return postgres.CreatePost(ctx, p.db, anthrovePost)
} }
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, anthroveTag) return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag)
} }
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error { func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error {
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship) return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain)
} }
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error { func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthrovePostID, anthrovePostID models.AnthrovePostID) error {
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.ID, anthrovePost.ID) return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
} }
func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID)) return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
} }
func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) { func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) {
return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePost.ID) return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePost.ID)
} }
func (p *postgresqlConnection) GetPostBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) { func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) {
return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl) return postgres.GetPostByURL(ctx, p.db, sourceUrl)
} }
func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) { func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) {
return postgres.GetPostBySourceID(ctx, p.db, sourcePostID) return postgres.GetPostBySourceID(ctx, p.db, sourceID)
} }
func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID) return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
} }
@ -112,32 +112,32 @@ func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveU
return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID) return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID)
} }
func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName) return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID)
} }
func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) { func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
return postgres.GetAllAnthroveUserIDs(ctx, p.db) return postgres.GetAllAnthroveUserIDs(ctx, p.db)
} }
func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { func (p *postgresqlConnection) GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit) return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit)
} }
func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { func (p *postgresqlConnection) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return postgres.GetUserTagNodeWitRelationToFavedPosts(ctx, p.db, anthroveUserID) return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
} }
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) { func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.Tag, error) {
return postgres.GetTags(ctx, p.db) return postgres.GetTags(ctx, p.db)
} }
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) { func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) {
return postgres.GetAllSourceNodes(ctx, p.db) return postgres.GetAllSource(ctx, p.db)
} }
func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) { func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
return postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl) return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
} }
func (p *postgresqlConnection) migrateDatabase(connectionString string) error { func (p *postgresqlConnection) migrateDatabase(connectionString string) error {

View File

@ -2,6 +2,9 @@ package models
type AnthroveUserID string type AnthroveUserID string
type AnthrovePostID string type AnthrovePostID string
type AnthroveSourceID string
type AnthroveSourceDomain string
type Rating string type Rating string
type TagType string type TagType string
@ -24,7 +27,6 @@ const (
) )
func (r *Rating) Convert(e621Rating string) { func (r *Rating) Convert(e621Rating string) {
switch e621Rating { switch e621Rating {
case "e": case "e":
*r = NSFW *r = NSFW
@ -35,5 +37,4 @@ func (r *Rating) Convert(e621Rating string) {
default: default:
*r = Unknown *r = Unknown
} }
} }

View File

@ -6,6 +6,12 @@ import (
"time" "time"
) )
type ID interface {
AnthroveUserID
AnthroveSourceID
AnthrovePostID
}
type BaseModel struct { type BaseModel struct {
ID string `gorm:"primaryKey"` ID string `gorm:"primaryKey"`
CreatedAt time.Time CreatedAt time.Time