From 78777ce7be48fb302784e7f93b1107cd42b4b80a Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 14 Jun 2024 13:57:03 +0200 Subject: [PATCH] feat(postgres): partially added Check heckPostNodeExistsBy --- internal/postgres/post.go | 30 ++++++++++++++++++++++++++++++ pkg/database/postgres.go | 16 ++++++++++------ pkg/models/pgModels/orm.go | 13 ++++++++----- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/internal/postgres/post.go b/internal/postgres/post.go index ff3d0d3..02ac938 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -28,3 +28,33 @@ func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID mod return nil } + +func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (bool, error) { + return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID)) +} + +func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (bool, error) { + return executeCheckQuery(ctx, db, "url = ?", sourceURL) +} + +func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (bool, error) { + return executeCheckQuery(ctx, db, "source_id = ?", sourceID) +} + +func executeCheckQuery(ctx context.Context, db *gorm.DB, query string, args ...interface{}) (bool, error) { + var count int64 + err := db.WithContext(ctx).Model(&pgModels.Post{}).Where(query, args...).Count(&count).Error + if err != nil { + return false, err + } + + exists := count > 0 + + log.WithFields(log.Fields{ + "query": query, + "args": args, + "exists": exists, + }).Trace("database: executed check query") + + return exists, nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index bc4b494..a37c2e0 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -84,18 +84,22 @@ func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUs } func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) { - //TODO implement me - panic("implement me") + exists, err := postgres.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, p.db, anthrovePost.PostID) + return anthrovePost, exists, err } +// CheckPostNodeExistsBySourceURL NOT WORKING! TODO! func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) { - //TODO implement me - panic("implement me") + var post graphModels.AnthrovePost + exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl) + return &post, exists, err } +// CheckPostNodeExistsBySourceID NOT WORKING! TODO! func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) { - //TODO implement me - panic("implement me") + var post graphModels.AnthrovePost + exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceID(ctx, p.db, sourcePostID) + return &post, exists, err } func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { diff --git a/pkg/models/pgModels/orm.go b/pkg/models/pgModels/orm.go index 11aafa4..c637fa5 100644 --- a/pkg/models/pgModels/orm.go +++ b/pkg/models/pgModels/orm.go @@ -14,12 +14,15 @@ type BaseModel struct { } func (base *BaseModel) BeforeCreate(db *gorm.DB) error { - id, err := gonanoid.New() - if err != nil { - return err - } - base.ID = id + if base.ID == "" { + id, err := gonanoid.New() + if err != nil { + return err + } + + base.ID = id + } return nil }