From b86c6671567e8d421ff9587702341ee0b933662b Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 21 Jun 2024 22:54:47 +0200 Subject: [PATCH] fix(postgres): CheckIfAnthrovePostNodeExistsBySourceURL now working as intended Signed-off-by: soxx --- internal/postgres/post.go | 23 ++++++++++++++++++----- pkg/database/postgres.go | 3 +-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/postgres/post.go b/internal/postgres/post.go index 1706b44..cf7a58c 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -2,8 +2,10 @@ package postgres import ( "context" + "errors" "fmt" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" "git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels" log "github.com/sirupsen/logrus" "gorm.io/gorm" @@ -38,15 +40,26 @@ func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID)) } -func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (bool, error) { - var size int64 - err := db.WithContext(ctx).Model(&pgModels.PostReference{}).Where("url = ?", sourceURL).Count(&size).Error +func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*graphModels.AnthrovePost, bool, error) { + var post pgModels.Post + err := db.WithContext(ctx).Model(&pgModels.Post{}).InnerJoins("References", db.Where("url = ?", sourceURL)).First(&post).Error if err != nil { - return false, err + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, false, nil + } + return nil, false, err } - return size > 0, nil + pgPost := &graphModels.AnthrovePost{ + PostID: models.AnthrovePostID(post.ID), + Rating: post.Rating, + } + + //TODO Now test it! :D + // Naaa + // D: + return pgPost, true, nil } func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (bool, error) { diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index dbba6f5..aec5911 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -89,8 +89,7 @@ func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Conte // CheckPostNodeExistsBySourceURL NOT WORKING! TODO! func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) { - var post graphModels.AnthrovePost - exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl) + post, exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl) return &post, exists, err }