From 8d655e991fbca1792bef9ecd80b93ef9d04f598a Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 14 Jun 2024 13:19:55 +0200 Subject: [PATCH] feat(postgres): added LinkPostWithSource --- internal/postgres/relationships.go | 39 ++++++++++++++++++++++++++++++ pkg/database/postgres.go | 3 +-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 internal/postgres/relationships.go diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go new file mode 100644 index 0000000..b3170d4 --- /dev/null +++ b/internal/postgres/relationships.go @@ -0,0 +1,39 @@ +package postgres + +import ( + "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" + log "github.com/sirupsen/logrus" + "gorm.io/gorm" +) + +func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string) error { + var post pgModels.Post + var source pgModels.Source + + // Find the post + err := db.WithContext(ctx).Where("id = ?", anthrovePostID).First(&post).Error + if err != nil { + return err + } + + // Find the source + err = db.WithContext(ctx).Where("domain = ?", anthroveSourceDomain).First(&source).Error + if err != nil { + return err + } + + // Establish the relationship + err = db.WithContext(ctx).Model(&post).Association("Sources").Append(&source) + if err != nil { + return err + } + + log.WithFields(log.Fields{ + "anthrove_post_id": anthrovePostID, + "anthrove_source_domain": anthroveSourceDomain, + }).Trace("database: created anthrove post to source link") + + return nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 887a62a..bd57a64 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -73,8 +73,7 @@ func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, ant } func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error { - //TODO implement me - panic("implement me") + return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, anthroveSourceDomain) } func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error {