From 56bb414f9df6786a44299b3982d73af52855520d Mon Sep 17 00:00:00 2001 From: SoXX Date: Thu, 27 Jun 2024 22:05:21 +0200 Subject: [PATCH] feat(PostReference): added the ability to add missing filed in db Signed-off-by: SoXX --- internal/postgres/post_test.go | 4 ++-- internal/postgres/relationships.go | 4 ++-- internal/postgres/user_test.go | 2 +- pkg/database/database.go | 12 +++++++++++- pkg/database/postgres.go | 4 ++-- pkg/database/postgres_test.go | 6 +++--- pkg/models/postReference.go | 10 +++++++--- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index 735f6b5..f679eaa 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -199,7 +199,7 @@ func TestGetPostBySourceURL(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org") + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org", models.PostReferenceConfig{}) if err != nil { t.Fatal("Could not create source reference", err) } @@ -298,7 +298,7 @@ func TestGetPostBySourceID(t *testing.T) { t.Fatal("Could not create source", err) } - err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg") + err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg", models.PostReferenceConfig{}) if err != nil { t.Fatal("Could not create source reference", err) } diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 5e8e8e6..bda9569 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -10,7 +10,7 @@ import ( "gorm.io/gorm" ) -func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL) error { +func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { if anthrovePostID == "" { return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty} } @@ -23,7 +23,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"} } - result := db.WithContext(ctx).Exec(`INSERT INTO "PostReference" (post_id, source_id, url) SELECT $1, source.id, $2 FROM "Source" AS source WHERE domain = $3;`, anthrovePostID, postURL, sourceDomain) + result := db.WithContext(ctx).Exec(`INSERT INTO "PostReference" (post_id, source_id, url, full_file_url, preview_file_url, sample_file_url, source_post_id) SELECT $1, source.id, $2, $4, $5, $6, $7 FROM "Source" AS source WHERE domain = $3;`, anthrovePostID, postURL, sourceDomain, config.FullFileURL, config.PreviewFileURL, config.SampleFileURL, config.SourcePostID) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index ea9f2ad..b146d52 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -162,7 +162,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) { userID: "e1", username: "marius", }, - wantErr: false, + wantErr: true, }, { name: "Test 5: no userID", diff --git a/pkg/database/database.go b/pkg/database/database.go index 0060744..fc2e5ed 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -23,7 +23,7 @@ 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, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL) error + CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error // CreateReferenceBetweenUserAndPost links a user with a post. CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error @@ -66,4 +66,14 @@ type OtterSpace interface { // GetSourceByDomain retrieves a source by its URL. GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) + /* + CreateTagAlias() + DeleteTagAlias() + UpdateTagWithAlias() + + CreateTagGroup() + DeleteTagGroup() + UpdateTagWithGroup() + + UpdateUserWithScrapeTimeInterval()*/ } diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 676d308..fb53ec9 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -88,8 +88,8 @@ 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 models.AnthroveSourceDomain, postURL models.AnthrovePostURL) error { - return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL) +func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { + return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config) } func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index bef6428..c7d4996 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -521,7 +521,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T) db: gormDB, debug: true, } - if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl); (err != nil) != tt.wantErr { + if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr { t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -834,7 +834,7 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) { t.Fatal("Could not create source", err) } - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas") + err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas", models.PostReferenceConfig{}) if err != nil { t.Fatal("Could not create source reference", err) } @@ -935,7 +935,7 @@ func Test_postgresqlConnection_GetPostBySourceID(t *testing.T) { t.Fatal("Could not create source", err) } - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd") + err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd", models.PostReferenceConfig{}) if err != nil { t.Fatal("Could not create source reference", err) } diff --git a/pkg/models/postReference.go b/pkg/models/postReference.go index 999e145..df1267c 100644 --- a/pkg/models/postReference.go +++ b/pkg/models/postReference.go @@ -1,9 +1,13 @@ package models type PostReference struct { - PostID string `gorm:"primaryKey"` - SourceID string `gorm:"primaryKey"` - URL string `gorm:"not null;unique"` + PostID string `gorm:"primaryKey"` + SourceID string `gorm:"primaryKey"` + URL string `gorm:"not null;unique"` + PostReferenceConfig +} + +type PostReferenceConfig struct { SourcePostID string FullFileURL string PreviewFileURL string