feat(postgres): partially added Check heckPostNodeExistsBy

This commit is contained in:
SoXX 2024-06-14 13:57:03 +02:00
parent bd22ed3f79
commit 78777ce7be
3 changed files with 48 additions and 11 deletions

View File

@ -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
}

View File

@ -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) {

View File

@ -14,12 +14,15 @@ type BaseModel struct {
}
func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
if base.ID == "" {
id, err := gonanoid.New()
if err != nil {
return err
}
base.ID = id
}
return nil
}