This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/internal/postgres/relationships.go

40 lines
1023 B
Go

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
}