From 5aac1f2916ad99c1ca48e97019a949ae4311d633 Mon Sep 17 00:00:00 2001 From: soxx Date: Tue, 25 Jun 2024 09:09:18 +0200 Subject: [PATCH] feat(postgres): start implementing new generic orm model Signed-off-by: soxx --- internal/postgres/post_test.go | 30 +++++++----- internal/postgres/relationships.go | 2 +- internal/postgres/relationships_test.go | 20 ++++---- internal/postgres/tag.go | 4 +- internal/postgres/tag_test.go | 4 +- internal/postgres/user.go | 12 ++--- internal/postgres/user_test.go | 64 +++++++++++++------------ pkg/models/post.go | 2 +- pkg/models/source.go | 2 +- pkg/models/user.go | 2 +- 10 files changed, 75 insertions(+), 67 deletions(-) diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 70d2b37..389e339 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -23,8 +23,8 @@ func TestCreateAnthrovePostNode(t *testing.T) { // Setup Tests validPost := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -84,8 +84,8 @@ func TestGetPostByAnthroveID(t *testing.T) { // Setup Tests post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -112,7 +112,7 @@ func TestGetPostByAnthroveID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthrovePostID: models.AnthrovePostID(post.ID), + anthrovePostID: post.ID, }, want: post, wantErr: false, @@ -163,8 +163,8 @@ func TestGetPostBySourceURL(t *testing.T) { // Setup Tests post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -175,7 +175,9 @@ func TestGetPostBySourceURL(t *testing.T) { } source := models.Source{ - BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), + }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.net/icon.ico", @@ -260,8 +262,8 @@ func TestGetPostBySourceID(t *testing.T) { // Setup Tests post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -272,7 +274,9 @@ func TestGetPostBySourceID(t *testing.T) { } source := models.Source{ - BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), + }, DisplayName: "e621", Domain: "e621.net", Icon: "https://e621.net/icon.ico", @@ -283,7 +287,7 @@ func TestGetPostBySourceID(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, models.AnthrovePostID(post.ID), models.AnthroveSourceDomain(source.Domain)) + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) if err != nil { t.Fatal("Could not create source reference", err) } @@ -306,7 +310,7 @@ func TestGetPostBySourceID(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, }, want: post, wantErr: false, diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index f719704..b52f37e 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -20,7 +20,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr // Establish the relationship err = db.WithContext(ctx).Create(models.PostReference{ PostID: string(anthrovePostID), - SourceID: source.ID, + SourceID: string(source.ID), URL: string(sourceDomain), }).Error diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go index cc763a0..e56b461 100644 --- a/internal/postgres/relationships_test.go +++ b/internal/postgres/relationships_test.go @@ -26,8 +26,8 @@ func TestCheckUserToPostLink(t *testing.T) { } post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -61,7 +61,7 @@ func TestCheckUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - anthrovePostID: models.AnthrovePostID(post.ID), + anthrovePostID: post.ID, }, want: true, wantErr: false, @@ -83,7 +83,7 @@ func TestCheckUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "123", - anthrovePostID: models.AnthrovePostID(post.ID), + anthrovePostID: post.ID, }, want: false, wantErr: false, @@ -125,8 +125,8 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { // Setup Test post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -164,7 +164,7 @@ func TestEstablishAnthrovePostToSourceLink(t *testing.T) { args: args{ ctx: ctx, db: gormDB, - anthrovePostID: models.AnthrovePostID(post.ID), + anthrovePostID: post.ID, sourceDomain: "e621.net", }, wantErr: false, @@ -225,8 +225,8 @@ func TestEstablishUserToPostLink(t *testing.T) { } post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } @@ -254,7 +254,7 @@ func TestEstablishUserToPostLink(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - anthrovePostID: models.AnthrovePostID(post.ID), + anthrovePostID: post.ID, }, wantErr: false, }, diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index a20b352..d0aa2e0 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -35,8 +35,8 @@ func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, PostID models } pgPost := models.Post{ - BaseModel: models.BaseModel{ - ID: string(PostID), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: PostID, }, } diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index bc94c77..a100774 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -21,8 +21,8 @@ func TestCreateTagNodeWitRelation(t *testing.T) { // Setup Test post := &models.Post{ - BaseModel: models.BaseModel{ - ID: fmt.Sprintf("%025s", "1"), + BaseModel: models.BaseModel[models.AnthrovePostID]{ + ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")), }, Rating: "safe", } diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 9cdc2da..359533f 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -15,8 +15,8 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove } user := models.User{ - BaseModel: models.BaseModel{ - ID: string(anthroveUserID), + BaseModel: models.BaseModel[models.AnthroveUserID]{ + ID: anthroveUserID, }, } @@ -51,7 +51,7 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs } source := models.Source{ - BaseModel: models.BaseModel{ID: string(sourceID)}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: sourceID}, } if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil { @@ -62,8 +62,8 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs } userSource := models.UserSource{ - User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}}, - SourceID: source.ID, + User: models.User{BaseModel: models.BaseModel[models.AnthroveUserID]{ID: anthroveUserID}}, + SourceID: string(source.ID), AccountUsername: accountUsername, AccountID: accountId, UserID: string(anthroveUserID), @@ -248,7 +248,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse favoritePosts = append(favoritePosts, models.Post{ - BaseModel: models.BaseModel{ID: post.ID}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID}, Rating: post.Rating, }) } diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 6a701da..1983612 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -72,7 +72,9 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { // Setup Test source := &models.Source{ - BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")), + }, DisplayName: "e621", Domain: "e621.net", Icon: "icon.e621.net", @@ -102,7 +104,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, userID: "e1", username: "marius", }, @@ -114,7 +116,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "2", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, userID: "e1", username: "marius", }, @@ -126,7 +128,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, userID: "e1", username: "marius", }, @@ -150,7 +152,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, userID: "", username: "marius", }, @@ -162,7 +164,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, userID: "aa", username: "", }, @@ -255,7 +257,9 @@ func TestGetUserSourceBySourceID(t *testing.T) { } source := &models.Source{ - BaseModel: models.BaseModel{ID: expectedResult["e621"].Source.ID}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ + ID: expectedResult["e621"].Source.ID, + }, DisplayName: expectedResult["e621"].Source.DisplayName, Domain: expectedResult["e621"].Source.Domain, } @@ -289,7 +293,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "1", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, }, want: expectedResult, wantErr: false, @@ -300,7 +304,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "22", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, }, want: nil, wantErr: true, @@ -322,7 +326,7 @@ func TestGetUserSourceBySourceID(t *testing.T) { ctx: ctx, db: gormDB, anthroveUserID: "", - sourceID: models.AnthroveSourceID(source.ID), + sourceID: source.ID, }, want: nil, wantErr: true, @@ -377,28 +381,28 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { expectedResultPosts := []models.Post{ { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))}, Rating: "safe", }, { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))}, Rating: "safe", }, { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))}, Rating: "explicit", }, { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post4")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post4"))}, Rating: "explicit", }, { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post5")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post5"))}, Rating: "questionable", }, { - BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post6")}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post6"))}, Rating: "safe", }, } @@ -511,27 +515,27 @@ func TestGetUserFavoritesCount(t *testing.T) { expectedResultPosts := []models.Post{ { - BaseModel: models.BaseModel{ID: "Post1"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post1"}, Rating: "safe", }, { - BaseModel: models.BaseModel{ID: "Post2"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post2"}, Rating: "safe", }, { - BaseModel: models.BaseModel{ID: "Post3"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post3"}, Rating: "explicit", }, { - BaseModel: models.BaseModel{ID: "Post4"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post4"}, Rating: "explicit", }, { - BaseModel: models.BaseModel{ID: "Post5"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post5"}, Rating: "questionable", }, { - BaseModel: models.BaseModel{ID: "Post6"}, + BaseModel: models.BaseModel[models.AnthrovePostID]{ID: "Post6"}, Rating: "safe", }, } @@ -615,7 +619,7 @@ func TestGetUserSourceLinks(t *testing.T) { // Setup Test eSource := &models.Source{ - BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "1")}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1"))}, DisplayName: "e621", Domain: "e621.net", } @@ -625,7 +629,7 @@ func TestGetUserSourceLinks(t *testing.T) { } faSource := &models.Source{ - BaseModel: models.BaseModel{ID: fmt.Sprintf("%025s", "2")}, + BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "2"))}, DisplayName: "fa", Domain: "fa.net", } @@ -652,11 +656,11 @@ func TestGetUserSourceLinks(t *testing.T) { }, } - err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(eSource.ID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, "1", eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { t.Fatal(err) } - err = CreateUserWithRelationToSource(ctx, gormDB, "1", models.AnthroveSourceID(faSource.ID), expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) + err = CreateUserWithRelationToSource(ctx, gormDB, "1", faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) if err != nil { t.Fatal(err) } @@ -714,9 +718,9 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } posts := []models.Post{ - {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post1")}, Rating: "safe"}, - {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post2")}, Rating: "safe"}, - {BaseModel: models.BaseModel{ID: fmt.Sprintf("%-25s", "Post3")}, Rating: "explicit"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post1"))}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post2"))}, Rating: "safe"}, + {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: models.AnthrovePostID(fmt.Sprintf("%-25s", "Post3"))}, Rating: "explicit"}, } for _, post := range posts { @@ -737,7 +741,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { } for i, tag := range tags { - err = CreateTagAndReferenceToPost(ctx, gormDB, models.AnthrovePostID(posts[i].ID), &tag) + err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) if err != nil { t.Fatal(err) } diff --git a/pkg/models/post.go b/pkg/models/post.go index 9aef5e7..bfe9946 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -2,7 +2,7 @@ package models // Post model type Post struct { - BaseModel + BaseModel[AnthrovePostID] Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `gorm:"many2many:post_tags;"` Favorites []UserFavorites `gorm:"foreignKey:PostID"` diff --git a/pkg/models/source.go b/pkg/models/source.go index 0860b81..200b48e 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -2,7 +2,7 @@ package models // Source model type Source struct { - BaseModel + BaseModel[AnthroveSourceID] DisplayName string Domain string `gorm:"not null;unique"` Icon string `gorm:"not null"` diff --git a/pkg/models/user.go b/pkg/models/user.go index 7f723e6..95ba75d 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -2,7 +2,7 @@ package models // User model type User struct { - BaseModel + BaseModel[AnthroveUserID] Favorites []UserFavorites `gorm:"foreignKey:UserID"` Sources []UserSource `gorm:"foreignKey:UserID"` }