2024-06-14 11:19:55 +00:00
package postgres
import (
"context"
2024-06-25 19:55:43 +00:00
"errors"
2024-06-26 07:20:52 +00:00
2024-06-26 08:02:54 +00:00
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
2024-06-14 11:19:55 +00:00
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
2024-06-26 14:12:10 +00:00
func CreateReferenceBetweenPostAndSource ( ctx context . Context , db * gorm . DB , anthrovePostID models . AnthrovePostID , sourceDomain models . AnthroveSourceDomain , postURL models . AnthrovePostURL ) error {
2024-06-25 19:55:43 +00:00
if anthrovePostID == "" {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDIsEmpty }
2024-06-25 19:55:43 +00:00
}
if len ( anthrovePostID ) != 25 {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDToShort }
2024-06-25 19:55:43 +00:00
}
if sourceDomain == "" {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : "sourceDomain cannot be empty" }
2024-06-25 19:55:43 +00:00
}
2024-06-14 11:19:55 +00:00
2024-06-26 14:12:10 +00:00
result := db . WithContext ( ctx ) . Exec ( ` INSERT INTO "PostReference" (post_id, source_id, url) SELECT $1, source.id, $2 FROM "Source" AS source WHERE domain = $3; ` , anthrovePostID , postURL , sourceDomain )
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 & otterError . NoDataFound { }
2024-06-25 19:55:43 +00:00
}
2024-06-26 14:12:10 +00:00
if errors . Is ( result . Error , gorm . ErrCheckConstraintViolated ) {
2024-06-26 08:02:54 +00:00
return & otterError . EntityAlreadyExists { }
2024-06-25 19:55:43 +00:00
}
2024-06-26 14:12:10 +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:19:55 +00:00
}
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-25 19:55:43 +00:00
if anthrovePostID == "" {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDIsEmpty }
2024-06-25 19:55:43 +00:00
}
if len ( anthrovePostID ) != 25 {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDToShort }
2024-06-25 19:55:43 +00:00
}
if anthroveUserID == "" {
2024-06-26 08:02:54 +00:00
return & otterError . EntityValidationFailed { Reason : "anthroveUserID cannot be empty" }
2024-06-25 19:55:43 +00:00
}
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 ) ,
}
2024-06-25 19:55:43 +00:00
result := db . WithContext ( ctx ) . Create ( & userFavorite )
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 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:22:52 +00:00
}
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-25 19:55:43 +00:00
if anthrovePostID == "" {
2024-06-26 08:02:54 +00:00
return false , & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDIsEmpty }
2024-06-25 19:55:43 +00:00
}
if len ( anthrovePostID ) != 25 {
2024-06-26 08:02:54 +00:00
return false , & otterError . EntityValidationFailed { Reason : otterError . AnthroveUserIDToShort }
2024-06-25 19:55:43 +00:00
}
if anthroveUserID == "" {
2024-06-26 08:02:54 +00:00
return false , & otterError . EntityValidationFailed { Reason : "anthroveUserID cannot be empty" }
2024-06-25 19:55:43 +00:00
}
2024-06-25 20:22:05 +00:00
if len ( anthroveUserID ) != 25 {
2024-06-26 08:02:54 +00:00
return false , & otterError . EntityValidationFailed { Reason : "anthroveUserID needs to be 25 characters long" }
2024-06-25 20:22:05 +00:00
}
2024-06-25 19:55:43 +00:00
result := db . WithContext ( ctx ) . Model ( & models . UserFavorites { } ) . Where ( "user_id = ? AND post_id = ?" , string ( anthroveUserID ) , string ( anthrovePostID ) ) . Count ( & count )
if result . Error != nil {
if errors . Is ( result . Error , gorm . ErrRecordNotFound ) {
2024-06-26 08:02:54 +00:00
return false , & otterError . NoDataFound { }
2024-06-25 19:55:43 +00:00
}
return false , result . Error
2024-06-14 11:25:54 +00:00
}
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
}