2024-06-14 11:29:53 +00:00
package postgres
import (
"context"
2024-06-21 20:54:47 +00:00
"errors"
2024-07-05 12:06:39 +00:00
2024-07-02 08:11:42 +00:00
otterError "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/error"
2024-06-25 14:02:08 +00:00
2024-07-02 08:11:42 +00:00
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
2024-06-14 11:29:53 +00:00
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-07-03 20:54:35 +00:00
// TODO: Make Tests
func CreatePostInBatch ( ctx context . Context , db * gorm . DB , anthrovePost [ ] models . Post , batchSize int ) error {
if anthrovePost == nil {
return & otterError . EntityValidationFailed { Reason : "anthrovePost cannot be nil" }
}
2024-07-05 12:06:39 +00:00
if len ( anthrovePost ) == 0 {
return & otterError . EntityValidationFailed { Reason : "anthrovePost cannot be empty" }
}
2024-07-03 20:54:35 +00:00
if batchSize == 0 {
return & otterError . EntityValidationFailed { Reason : "batch size cannot be zero" }
}
result := db . WithContext ( ctx ) . CreateInBatches ( anthrovePost , batchSize )
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrDuplicatedKey ) {
return & otterError . EntityAlreadyExists { }
}
return result . Error
}
if result . RowsAffected == 0 {
return & otterError . NoDataWritten { }
}
log . WithFields ( log . Fields {
"tag_size" : len ( anthrovePost ) ,
"batch_size" : batchSize ,
} ) . Trace ( "database: created tag node" )
return nil
}
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-07-01 19:38:09 +00:00
func GetPostBySourceURL ( 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
}