2024-06-14 11:19:55 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error {
|
2024-06-22 20:06:36 +00:00
|
|
|
var source models.Source
|
2024-06-19 21:32:42 +00:00
|
|
|
var err error
|
2024-06-14 11:19:55 +00:00
|
|
|
|
|
|
|
// Find the source
|
2024-06-22 20:27:38 +00:00
|
|
|
err = db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source).Error
|
2024-06-14 11:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Establish the relationship
|
2024-06-22 20:06:36 +00:00
|
|
|
err = db.WithContext(ctx).Create(models.PostReference{
|
2024-06-23 20:35:46 +00:00
|
|
|
PostID: string(anthrovePostID),
|
2024-06-19 21:32:42 +00:00
|
|
|
SourceID: source.ID,
|
2024-06-23 20:35:46 +00:00
|
|
|
URL: string(sourceDomain),
|
2024-06-19 21:32:42 +00:00
|
|
|
}).Error
|
|
|
|
|
2024-06-14 11:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_post_id": anthrovePostID,
|
2024-06-22 20:27:38 +00:00
|
|
|
"anthrove_source_domain": sourceDomain,
|
2024-06-14 11:19:55 +00:00
|
|
|
}).Trace("database: created anthrove post to source link")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 11:22:52 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
|
2024-06-24 15:07:41 +00:00
|
|
|
userFavorite := models.UserFavorites{
|
2024-06-14 11:22:52 +00:00
|
|
|
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
|
|
|
|
}
|
2024-06-14 11:25:54 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
|
2024-06-14 11:25:54 +00:00
|
|
|
var count int64
|
2024-06-24 15:07:41 +00:00
|
|
|
err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error
|
2024-06-14 11:25:54 +00:00
|
|
|
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
|
|
|
|
}
|