2024-06-14 11:29:53 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-21 20:54:47 +00:00
|
|
|
"errors"
|
2024-06-19 21:32:42 +00:00
|
|
|
"fmt"
|
2024-06-14 11:29:53 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-06-21 20:54:47 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
2024-06-14 11:29:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveRating models.Rating) error {
|
2024-06-22 20:06:36 +00:00
|
|
|
post := models.Post{
|
|
|
|
BaseModel: models.BaseModel{
|
2024-06-14 11:29:53 +00:00
|
|
|
ID: string(anthrovePostID),
|
|
|
|
},
|
|
|
|
Rating: anthroveRating,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).Create(&post).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_post_id": anthrovePostID,
|
|
|
|
"anthrove_post_rating": anthroveRating,
|
|
|
|
}).Trace("database: created anthrove post")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 11:57:03 +00:00
|
|
|
|
|
|
|
func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (bool, error) {
|
2024-06-19 21:32:42 +00:00
|
|
|
if anthrovePostID == "" {
|
|
|
|
return false, fmt.Errorf("anthrovePostID is required")
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:57:03 +00:00
|
|
|
return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID))
|
|
|
|
}
|
|
|
|
|
2024-06-21 20:58:52 +00:00
|
|
|
func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*graphModels.AnthrovePost, bool, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
postRef := models.PostReference{}
|
|
|
|
err := db.WithContext(ctx).Model(&models.PostReference{}).Where("url = ?", sourceURL).First(&postRef).Error
|
2024-06-21 12:56:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-06-21 20:54:47 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
var post models.Post
|
2024-06-21 21:25:55 +00:00
|
|
|
err = db.WithContext(ctx).First(&post, "id = ?", postRef.PostID).Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 20:57:05 +00:00
|
|
|
pgPost := graphModels.AnthrovePost{
|
2024-06-21 20:54:47 +00:00
|
|
|
PostID: models.AnthrovePostID(post.ID),
|
|
|
|
Rating: post.Rating,
|
2024-06-21 12:56:11 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 20:54:47 +00:00
|
|
|
//TODO Now test it! :D
|
|
|
|
// Naaa
|
|
|
|
// D:
|
2024-06-21 20:58:52 +00:00
|
|
|
return &pgPost, true, nil
|
2024-06-14 11:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-06-19 21:32:42 +00:00
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
err := db.WithContext(ctx).Model(&models.Post{}).Where(query, args...).Count(&count).Error
|
2024-06-14 11:57:03 +00:00
|
|
|
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
|
|
|
|
}
|