75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID string, sourceDomain string) error {
|
|
var source models.Source
|
|
var err error
|
|
|
|
// Find the source
|
|
err = db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Establish the relationship
|
|
err = db.WithContext(ctx).Create(models.PostReference{
|
|
PostID: anthrovePostID,
|
|
SourceID: source.ID,
|
|
URL: sourceDomain,
|
|
}).Error
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_post_id": anthrovePostID,
|
|
"anthrove_source_domain": sourceDomain,
|
|
}).Trace("database: created anthrove post to source link")
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) error {
|
|
userFavorite := models.UserFavorite{
|
|
UserID: string(anthroveUserID),
|
|
PostID: string(anthrovePostID),
|
|
}
|
|
|
|
err := db.WithContext(ctx).Create(&userFavorite).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id": anthroveUserID,
|
|
"anthrove_post_id": anthrovePostID,
|
|
}).Trace("database: created user to post link")
|
|
|
|
return nil
|
|
}
|
|
|
|
func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) (bool, error) {
|
|
var count int64
|
|
err := db.WithContext(ctx).Model(&models.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
|
|
}
|