From b0d98d842f2e37c21b7886514883a035c10626cc Mon Sep 17 00:00:00 2001 From: soxx Date: Sun, 23 Jun 2024 22:35:46 +0200 Subject: [PATCH] refactor(postgres): renamed function names and fixed tests and other issues Signed-off-by: soxx --- internal/postgres/post.go | 6 +-- internal/postgres/post_test.go | 18 ++++---- internal/postgres/relationships.go | 10 ++--- internal/postgres/relationships_test.go | 22 ++++----- internal/postgres/source.go | 8 ++-- internal/postgres/source_test.go | 8 ++-- internal/postgres/tag.go | 2 +- internal/postgres/tag_test.go | 6 +-- internal/postgres/user.go | 48 +++++++++++--------- internal/postgres/user_test.go | 60 +++++++++++++------------ pkg/database/database.go | 20 ++++----- pkg/database/postgres.go | 32 ++++++------- pkg/models/const.go | 5 ++- pkg/models/orm.go | 6 +++ 14 files changed, 135 insertions(+), 116 deletions(-) diff --git a/internal/postgres/post.go b/internal/postgres/post.go index 079be3d..2dde49a 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -23,7 +23,7 @@ func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) err 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 == "" { return nil, fmt.Errorf("anthrovePostID is required") } @@ -37,7 +37,7 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string 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 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 } -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 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 diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 098fe2b..cf2ebc4 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -99,7 +99,7 @@ func TestGetPostByAnthroveID(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthrovePostID string + anthrovePostID models.AnthrovePostID } tests := []struct { name string @@ -112,7 +112,7 @@ func TestGetPostByAnthroveID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthrovePostID: post.ID, + anthrovePostID: models.AnthrovePostID(post.ID), }, want: post, wantErr: false, @@ -186,7 +186,7 @@ func TestGetPostBySourceURL(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, source.Domain) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain)) if err != nil { t.Fatal("Could not create source reference", err) } @@ -236,13 +236,13 @@ func TestGetPostBySourceURL(t *testing.T) { } for _, tt := range tests { 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 { - t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetPostByURL() error = %v, wantErr %v", err, tt.wantErr) return } 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) } }) } @@ -283,7 +283,7 @@ func TestGetPostBySourceID(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, source.Domain) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain)) if err != nil { t.Fatal("Could not create source reference", err) } @@ -292,7 +292,7 @@ func TestGetPostBySourceID(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - sourceID string + sourceID models.AnthroveSourceID } tests := []struct { @@ -306,7 +306,7 @@ func TestGetPostBySourceID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - sourceID: source.ID, + sourceID: models.AnthroveSourceID(source.ID), }, want: post, wantErr: false, diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index cade964..5c268f8 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -7,7 +7,7 @@ import ( "gorm.io/gorm" ) -func CreateReferenceBetweenPostAndSource(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 err error @@ -19,9 +19,9 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr // Establish the relationship err = db.WithContext(ctx).Create(models.PostReference{ - PostID: anthrovePostID, + PostID: string(anthrovePostID), SourceID: source.ID, - URL: sourceDomain, + URL: string(sourceDomain), }).Error if err != nil { @@ -36,7 +36,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr return nil } -func CreateReferenceBetweenUserAndPost(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{ UserID: string(anthroveUserID), PostID: string(anthrovePostID), @@ -55,7 +55,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov return nil } -func CheckReferenceBetweenUserAndPost(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 err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error if err != nil { diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go index 4d49f65..cc763a0 100644 --- a/internal/postgres/relationships_test.go +++ b/internal/postgres/relationships_test.go @@ -37,7 +37,7 @@ func TestCheckUserToPostLink(t *testing.T) { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) if err != nil { t.Fatal(err) } @@ -46,8 +46,8 @@ func TestCheckUserToPostLink(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveUserID string - anthrovePostID string + anthroveUserID models.AnthroveUserID + anthrovePostID models.AnthrovePostID } tests := []struct { name string @@ -61,7 +61,7 @@ func TestCheckUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - anthrovePostID: post.ID, + anthrovePostID: models.AnthrovePostID(post.ID), }, want: true, wantErr: false, @@ -83,7 +83,7 @@ func TestCheckUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "123", - anthrovePostID: post.ID, + anthrovePostID: models.AnthrovePostID(post.ID), }, want: false, wantErr: false, @@ -150,8 +150,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthrovePostID string - sourceDomain string + anthrovePostID models.AnthrovePostID + sourceDomain models.AnthroveSourceDomain anthrovePostRelationship *models.PostReference } tests := []struct { @@ -164,7 +164,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthrovePostID: post.ID, + anthrovePostID: models.AnthrovePostID(post.ID), sourceDomain: "e621.net", }, wantErr: false, @@ -240,8 +240,8 @@ func TestEstablishUserToPostLink(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveUserID string - anthrovePostID string + anthroveUserID models.AnthroveUserID + anthrovePostID models.AnthrovePostID } tests := []struct { name string @@ -254,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - anthrovePostID: post.ID, + anthrovePostID: models.AnthrovePostID(post.ID), }, wantErr: false, }, diff --git a/internal/postgres/source.go b/internal/postgres/source.go index ec889fd..da13fc3 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -48,15 +48,15 @@ func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) { return sources, nil } -// GetSourceByURL returns the first source it finds based on the domain -func GetSourceByURL(ctx context.Context, db *gorm.DB, domain string) (*models.Source, error) { +// GetSourceByDomain returns the first source it finds based on the domain +func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) { var sources models.Source - if domain == "" { + if sourceDomain == "" { 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 { return nil, result.Error diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index a6d7863..80c0dc8 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -174,7 +174,7 @@ func TestGetSourceNodesByURL(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - domain string + domain models.AnthroveSourceDomain } tests := []struct { name string @@ -215,13 +215,13 @@ func TestGetSourceNodesByURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetSourceByURL(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 { - t.Errorf("GetSourceByURL() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) return } if !checkSourceNode(got, tt.want) { - t.Errorf("GetSourceByURL() got = %v, want %v", got, tt.want) + t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) } }) } diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 4213b28..a20b352 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -24,7 +24,7 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { return nil } -func CreateTagAndReferenceToPost(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 == "" { return fmt.Errorf("PostID is empty") diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index d83d760..bc94c77 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -41,7 +41,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - PostID string + PostID models.AnthrovePostID tag *models.Tag } tests := []struct { @@ -54,7 +54,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - PostID: post.ID, + PostID: models.AnthrovePostID(post.ID), tag: tag, }, wantErr: false, @@ -64,7 +64,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - PostID: post.ID, + PostID: models.AnthrovePostID(post.ID), tag: nil, }, wantErr: true, diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 91282d2..1b4e38f 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -8,7 +8,7 @@ import ( "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 == "" { return fmt.Errorf("anthroveUserID cannot be empty") @@ -31,51 +31,59 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID string) error { return nil } -func CreateUserWithRelationToSource(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") } + 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) if err != nil { return err } source := models.Source{ - Domain: sourceDomain, + BaseModel: models.BaseModel{ID: string(sourceID)}, } if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil { log.WithFields(log.Fields{ - "source_domain": sourceDomain, + "source_id": sourceID, }).Error("database: failed to find source") return err } userSource := models.UserSource{ - User: models.User{BaseModel: models.BaseModel{ID: anthroveUserID}}, + User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}}, SourceID: source.ID, - AccountUsername: username, - AccountID: userID, - UserID: anthroveUserID, + AccountUsername: accountUsername, + AccountID: accountId, + UserID: string(anthroveUserID), } if err := db.WithContext(ctx).FirstOrCreate(&userSource).Error; err != nil { log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, - "source_domain": sourceDomain, - "account_username": username, - "account_id": userID, + "source_id": sourceID, + "account_username": accountUsername, + "account_id": accountId, }).Error("database: failed to create user-source relationship") return err } log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, - "source_domain": sourceDomain, - "account_username": username, - "account_id": userID, + "source_id": sourceID, + "account_username": accountUsername, + "account_id": accountId, }).Trace("database: created user-source relationship") return nil @@ -143,7 +151,7 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models. return userSourceMap, nil } -func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID 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 == "" { return nil, fmt.Errorf("anthroveUserID cannot be empty") } @@ -193,9 +201,9 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo 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 userIDs []string + var userIDs []models.AnthroveUserID err := db.WithContext(ctx).Model(&models.User{}).Find(&users).Error if err != nil { @@ -204,7 +212,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) { } for _, user := range users { - userIDs = append(userIDs, user.ID) + userIDs = append(userIDs, models.AnthroveUserID(user.ID)) } log.WithFields(log.Fields{ @@ -214,7 +222,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]string, error) { return userIDs, nil } -func GetUserFavoriteWithPagination(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 favoritePosts []models.Post diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 3adee0f..6a701da 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -25,7 +25,7 @@ func TestCreateUser(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveUserID string + anthroveUserID models.AnthroveUserID } tests := []struct { name string @@ -72,6 +72,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { // Setup Test source := &models.Source{ + BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -85,8 +86,8 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveUserID string - sourceDomain string + anthroveUserID models.AnthroveUserID + sourceID models.AnthroveSourceID userID string username string } @@ -96,24 +97,24 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { wantErr bool }{ { - name: "Test 1: Valid anthroveUserID, sourceDomain, userID, username", + name: "Test 1: Valid anthroveUserID, sourceID, userID, username", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceDomain: source.Domain, + sourceID: models.AnthroveSourceID(source.ID), userID: "e1", username: "marius", }, wantErr: false, }, { - name: "Test 2: Invalid anthroveUserID, valid sourceDomain, userID, username", + name: "Test 2: Invalid anthroveUserID, valid sourceID, userID, username", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "2", - sourceDomain: source.Domain, + sourceID: models.AnthroveSourceID(source.ID), userID: "e1", username: "marius", }, @@ -125,19 +126,19 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "", - sourceDomain: source.Domain, + sourceID: models.AnthroveSourceID(source.ID), userID: "e1", username: "marius", }, wantErr: true, }, { - name: "Test 4: invalid sourceDomain", + name: "Test 4: invalid sourceID", args: args{ ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceDomain: "fa.net", + sourceID: "fa.net", userID: "e1", username: "marius", }, @@ -149,7 +150,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceDomain: source.Domain, + sourceID: models.AnthroveSourceID(source.ID), userID: "", username: "marius", }, @@ -161,7 +162,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceDomain: source.Domain, + sourceID: models.AnthroveSourceID(source.ID), userID: "aa", username: "", }, @@ -170,7 +171,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateUserWithRelationToSource(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("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -188,7 +189,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { // Setup Test - users := []string{"1", "2", "3"} + users := []models.AnthroveUserID{"1", "2", "3"} for _, user := range users { err = CreateUser(ctx, gormDB, user) @@ -205,7 +206,7 @@ func TestGetAllAnthroveUserIDs(t *testing.T) { tests := []struct { name string args args - want []string + want []models.AnthroveUserID wantErr bool }{ { @@ -264,7 +265,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { t.Fatal(err) } - err = CreateUserWithRelationToSource(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 { t.Fatal(err) } @@ -274,7 +275,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx context.Context db *gorm.DB anthroveUserID models.AnthroveUserID - sourceID string + sourceID models.AnthroveSourceID } tests := []struct { name string @@ -288,7 +289,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceID: source.ID, + sourceID: models.AnthroveSourceID(source.ID), }, want: expectedResult, wantErr: false, @@ -299,7 +300,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "22", - sourceID: source.ID, + sourceID: models.AnthroveSourceID(source.ID), }, want: nil, wantErr: true, @@ -321,7 +322,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "", - sourceID: source.ID, + sourceID: models.AnthroveSourceID(source.ID), }, want: nil, wantErr: true, @@ -362,6 +363,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { }) } } + func TestGetUserFavoriteNodeWithPagination(t *testing.T) { // Setup trow away containert ctx := context.Background() @@ -420,7 +422,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", expectedResultPost.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(expectedResultPost.ID)) if err != nil { t.Fatal(err) } @@ -430,7 +432,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { type args struct { ctx context.Context db *gorm.DB - anthroveUserID string + anthroveUserID models.AnthroveUserID skip int limit int } @@ -492,7 +494,7 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { } func TestGetUserFavoritesCount(t *testing.T) { - // Setup trow away containert + // Setup trow away container ctx := context.Background() container, gormDB, err := test.StartPostgresContainer(ctx) if err != nil { @@ -539,7 +541,7 @@ func TestGetUserFavoritesCount(t *testing.T) { if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) if err != nil { t.Fatal(err) } @@ -613,6 +615,7 @@ func TestGetUserSourceLinks(t *testing.T) { // Setup Test eSource := &models.Source{ + BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, DisplayName: "e621", Domain: "e621.net", } @@ -622,6 +625,7 @@ func TestGetUserSourceLinks(t *testing.T) { } faSource := &models.Source{ + BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "2")}, DisplayName: "fa", Domain: "fa.net", } @@ -648,11 +652,11 @@ func TestGetUserSourceLinks(t *testing.T) { }, } - err = CreateUserWithRelationToSource(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 { t.Fatal(err) } - err = CreateUserWithRelationToSource(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 { t.Fatal(err) } @@ -720,7 +724,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { if err != nil { t.Fatal(err) } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", post.ID) + err = CreateReferenceBetweenUserAndPost(ctx, gormDB, "1", models.AnthrovePostID(post.ID)) if err != nil { t.Fatal(err) } @@ -733,7 +737,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } for i, tag := range tags { - err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) + err = CreateTagAndReferenceToPost(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag) if err != nil { t.Fatal(err) } diff --git a/pkg/database/database.go b/pkg/database/database.go index f0c74f0..ee0a644 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -10,7 +10,7 @@ type OtterSpace interface { Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error // CreateUserWithRelationToSource adds a user with a relation to a source. - CreateUserWithRelationToSource(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 // CreateSource adds a new source to the database. CreateSource(ctx context.Context, anthroveSource *models.Source) error @@ -22,22 +22,22 @@ type OtterSpace interface { CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error // CreateReferenceBetweenPostAndSource links a post with a source. - CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *models.PostReference) error + CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error // CreateReferenceBetweenUserAndPost links a user with a post. - CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error + CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthrovePostID, anthrovePostID models.AnthrovePostID) error // CheckReferenceBetweenUserAndPost checks if a user-post link exists. - CheckReferenceBetweenUserAndPost(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(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) - // GetPostBySourceURL retrieves a post by its source URL. - GetPostBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) + // GetPostByURL retrieves a post by its source URL. + GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) // 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) // GetUserFavoritesCount retrieves the count of a user's favorites. GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) @@ -46,7 +46,7 @@ type OtterSpace interface { GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) // GetUserSourceBySourceID retrieves a specified source link of a user. - GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID 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(ctx context.Context) ([]models.AnthroveUserID, error) @@ -63,6 +63,6 @@ type OtterSpace interface { // GetAllSources retrieves all sources. GetAllSources(ctx context.Context) ([]models.Source, error) - // GetSourceByURL retrieves a source by its URL. - GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) + // GetSourceByDomain retrieves a source by its URL. + GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) } diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 3503deb..0aa504d 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -64,8 +64,8 @@ func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, usern return nil } -func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error { - return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceDomain, userID, userID) +func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error { + return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceID, accountId, accountUsername) } func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error { @@ -80,28 +80,28 @@ func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) } -func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error { - return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship) +func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error { + return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain) } -func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error { - return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUser.ID, anthrovePost.ID) +func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthrovePostID, anthrovePostID models.AnthrovePostID) error { + return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) } -func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { - return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID)) +func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { + return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) } func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) { return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePost.ID) } -func (p *postgresqlConnection) GetPostBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) { - return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl) +func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) { + return postgres.GetPostByURL(ctx, p.db, sourceUrl) } -func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) { - return postgres.GetPostBySourceID(ctx, p.db, sourcePostID) +func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) { + return postgres.GetPostBySourceID(ctx, p.db, sourceID) } func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { @@ -112,8 +112,8 @@ func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveU return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID) } -func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) { - return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName) +func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) { + return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID) } func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) { @@ -136,8 +136,8 @@ func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Sour return postgres.GetAllSource(ctx, p.db) } -func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) { - return postgres.GetSourceByURL(ctx, p.db, sourceUrl) +func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) { + return postgres.GetSourceByDomain(ctx, p.db, sourceDomain) } func (p *postgresqlConnection) migrateDatabase(connectionString string) error { diff --git a/pkg/models/const.go b/pkg/models/const.go index a9c3cf6..5b0e59a 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -2,6 +2,9 @@ package models type AnthroveUserID string type AnthrovePostID string +type AnthroveSourceID string +type AnthroveSourceDomain string + type Rating string type TagType string @@ -24,7 +27,6 @@ const ( ) func (r *Rating) Convert(e621Rating string) { - switch e621Rating { case "e": *r = NSFW @@ -35,5 +37,4 @@ func (r *Rating) Convert(e621Rating string) { default: *r = Unknown } - } diff --git a/pkg/models/orm.go b/pkg/models/orm.go index ff7bb64..7e74329 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -6,6 +6,12 @@ import ( "time" ) +type ID interface { + AnthroveUserID + AnthroveSourceID + AnthrovePostID +} + type BaseModel struct { ID string `gorm:"primaryKey"` CreatedAt time.Time