diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 1a07396..fd855eb 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -56,3 +56,21 @@ func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID mo return nil } + +func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { + var count int64 + err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error + if err != nil { + return false, err + } + + exists := count > 0 + + log.WithFields(log.Fields{ + "relationship_exists": exists, + "relationship_anthrove_user_id": anthroveUserID, + "relationship_anthrove_post_id": anthrovePostID, + }).Trace("database: checked user post relationship") + + return exists, nil +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index a4a9008..661a884 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -81,8 +81,7 @@ func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUse } func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { - //TODO implement me - panic("implement me") + return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID)) } func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) {