SoXX
4fa936d774
All checks were successful
Gitea Build Check / Build (push) Successful in 10m58s
Signed-off-by: SoXX <soxx@fenpa.ws>
139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
|
|
"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 &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if sourceDomain == "" {
|
|
return &errors2.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 &errors2.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 &errors2.EntityAlreadyExists{}
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return &errors2.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 &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if anthroveUserID == "" {
|
|
return &errors2.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 &errors2.EntityAlreadyExists{}
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return &errors2.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, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
|
|
}
|
|
|
|
if len(anthrovePostID) != 25 {
|
|
return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
|
|
}
|
|
|
|
if anthroveUserID == "" {
|
|
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return false, &errors2.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, &errors2.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
|
|
}
|