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"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
2024-06-22 20:27:38 +00:00
func CreateAnthrovePostNode ( ctx context . Context , db * gorm . DB , anthrovePost * models . Post ) error {
2024-06-22 20:06:36 +00:00
post := models . Post {
BaseModel : models . BaseModel {
2024-06-22 20:27:38 +00:00
ID : string ( anthrovePost . ID ) ,
2024-06-14 11:29:53 +00:00
} ,
2024-06-22 20:27:38 +00:00
Rating : anthrovePost . Rating ,
2024-06-14 11:29:53 +00:00
}
err := db . WithContext ( ctx ) . Create ( & post ) . Error
if err != nil {
return err
}
log . WithFields ( log . Fields {
2024-06-22 20:27:38 +00:00
"anthrove_post_id" : anthrovePost . ID ,
"anthrove_post_rating" : anthrovePost . Rating ,
2024-06-14 11:29:53 +00:00
} ) . Trace ( "database: created anthrove post" )
return nil
}
2024-06-14 11:57:03 +00:00
2024-06-22 20:27:38 +00:00
func GetPostByAnthroveID ( ctx context . Context , db * gorm . DB , anthrovePostID string ) ( * models . Post , 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-22 20:27:38 +00:00
func GetPostBySourceURL ( ctx context . Context , db * gorm . DB , sourceURL string ) ( * models . Post , bool , error ) {
var post models . Post
err := db . WithContext ( ctx ) . Raw ( ` SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1 ` , sourceURL ) . First ( & post ) . 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
}
2024-06-22 20:27:38 +00:00
func GetPostBySourceID ( ctx context . Context , db * gorm . DB , sourceID string ) ( * models . Post , error ) {
2024-06-14 11:57:03 +00:00
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
}