2024-06-14 11:29:53 +00:00
package postgres
import (
"context"
2024-06-21 20:54:47 +00:00
"errors"
2024-06-26 07:10:04 +00:00
2024-06-26 08:02:54 +00:00
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
2024-06-25 14:02:08 +00:00
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 23:18:23 +00:00
func CreatePost ( ctx context . Context , db * gorm . DB , anthrovePost * models . Post ) error {
2024-06-25 14:02:08 +00:00
2024-06-25 19:55:43 +00:00
if anthrovePost == nil {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : "anthrovePost is nil" }
2024-06-25 14:02:08 +00:00
}
2024-06-25 19:55:43 +00:00
result := db . WithContext ( ctx ) . Create ( & anthrovePost )
2024-06-25 14:02:08 +00:00
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrDuplicatedKey ) {
2024-06-26 08:02:54 +00:00
return & otterError . EntityAlreadyExists { }
2024-06-25 14:02:08 +00:00
}
2024-06-25 19:55:43 +00:00
return result . Error
}
if result . RowsAffected == 0 {
2024-06-26 08:02:54 +00:00
return & otterError . NoDataWritten { }
2024-06-14 11:29:53 +00:00
}
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-23 20:35:46 +00:00
func GetPostByAnthroveID ( ctx context . Context , db * gorm . DB , anthrovePostID models . AnthrovePostID ) ( * models . Post , error ) {
2024-06-25 19:55:43 +00:00
2024-06-19 21:32:42 +00:00
if anthrovePostID == "" {
2024-06-26 08:02:54 +00:00
return nil , & otterError . EntityValidationFailed { Reason : "anthrovePostID is not set" }
2024-06-19 21:32:42 +00:00
}
2024-06-26 07:10:04 +00:00
if len ( anthrovePostID ) != 25 {
2024-06-26 08:02:54 +00:00
return nil , & otterError . EntityValidationFailed { Reason : "anthrovePostID needs to be 25 characters long" }
2024-06-26 07:10:04 +00:00
}
2024-06-25 19:55:43 +00:00
2024-06-22 21:25:28 +00:00
var post models . Post
2024-06-25 14:02:08 +00:00
result := db . WithContext ( ctx ) . First ( & post , "id = ?" , anthrovePostID )
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrRecordNotFound ) {
2024-06-26 08:02:54 +00:00
return nil , & otterError . NoDataFound { }
2024-06-25 14:02:08 +00:00
}
2024-06-25 19:55:43 +00:00
return nil , result . Error
2024-06-22 21:25:28 +00:00
}
return & post , nil
2024-06-14 11:57:03 +00:00
}
2024-06-23 20:35:46 +00:00
func GetPostByURL ( ctx context . Context , db * gorm . DB , sourceURL string ) ( * models . Post , error ) {
2024-06-25 19:55:43 +00:00
if sourceURL == "" {
2024-06-26 08:02:54 +00:00
return nil , & otterError . EntityValidationFailed { Reason : "sourceURL is not set" }
2024-06-25 19:55:43 +00:00
}
2024-06-22 20:27:38 +00:00
var post models . Post
2024-06-25 19:55:43 +00:00
result := 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 )
2024-06-21 12:56:11 +00:00
2024-06-25 19:55:43 +00:00
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrRecordNotFound ) {
2024-06-26 08:02:54 +00:00
return nil , & otterError . NoDataFound { }
2024-06-21 20:54:47 +00:00
}
2024-06-25 19:55:43 +00:00
return nil , result . Error
2024-06-21 21:25:55 +00:00
}
2024-06-22 21:25:28 +00:00
return & post , nil
2024-06-14 11:57:03 +00:00
}
2024-06-23 20:35:46 +00:00
func GetPostBySourceID ( ctx context . Context , db * gorm . DB , sourceID models . AnthroveSourceID ) ( * models . Post , error ) {
2024-06-25 19:55:43 +00:00
if sourceID == "" {
2024-06-26 08:02:54 +00:00
return nil , & otterError . EntityValidationFailed { Reason : "sourceID is not set" }
2024-06-25 19:55:43 +00:00
}
2024-06-22 21:25:28 +00:00
var post models . Post
2024-06-25 19:55:43 +00:00
result := 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.source_id = $1 LIMIT 1 ` , sourceID ) . First ( & post )
2024-06-19 21:32:42 +00:00
2024-06-25 19:55:43 +00:00
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrRecordNotFound ) {
2024-06-26 08:02:54 +00:00
return nil , & otterError . NoDataFound { }
2024-06-22 21:25:28 +00:00
}
2024-06-25 19:55:43 +00:00
return nil , result . Error
2024-06-14 11:57:03 +00:00
}
2024-06-22 21:25:28 +00:00
return & post , nil
2024-06-14 11:57:03 +00:00
}