139 lines
3.9 KiB
Go
139 lines
3.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error {
|
|
var source models.Source
|
|
|
|
if anthrovePostID == "" {
|
|
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if sourceDomain == "" {
|
|
return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"}
|
|
}
|
|
|
|
// Find the source
|
|
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return &otterError.NoDataFound{}
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
// Establish the relationship
|
|
result = db.WithContext(ctx).Create(models.PostReference{
|
|
PostID: string(anthrovePostID),
|
|
SourceID: string(source.ID),
|
|
URL: string(sourceDomain),
|
|
})
|
|
|
|
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{
|
|
"anthrove_post_id": anthrovePostID,
|
|
"anthrove_source_domain": sourceDomain,
|
|
}).Trace("database: created anthrove post to source link")
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
|
|
|
|
if anthrovePostID == "" {
|
|
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if anthroveUserID == "" {
|
|
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
userFavorite := models.UserFavorites{
|
|
UserID: string(anthroveUserID),
|
|
PostID: string(anthrovePostID),
|
|
}
|
|
|
|
result := db.WithContext(ctx).Create(&userFavorite)
|
|
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{
|
|
"anthrove_user_id": anthroveUserID,
|
|
"anthrove_post_id": anthrovePostID,
|
|
}).Trace("database: created user to post link")
|
|
|
|
return nil
|
|
}
|
|
|
|
func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
|
|
var count int64
|
|
|
|
if anthrovePostID == "" {
|
|
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if anthroveUserID == "" {
|
|
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
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) {
|
|
return false, &otterError.NoDataFound{}
|
|
}
|
|
return false, result.Error
|
|
}
|
|
|
|
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
|
|
}
|