2024-06-04 21:14:02 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-25 19:55:43 +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-04 21:14:02 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
|
2024-06-20 12:54:06 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
if tag == nil {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := db.WithContext(ctx).Where(tag).Create(tag)
|
|
|
|
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
|
|
|
|
}
|
2024-06-20 12:54:06 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
if result.RowsAffected == 0 {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.NoDataWritten{}
|
2024-06-20 12:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_name": tag.Name,
|
|
|
|
"tag_type": tag.Type,
|
|
|
|
}).Trace("database: created tag node")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
|
2024-06-04 21:14:02 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
if anthrovePostID == "" {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(anthrovePostID) != 25 {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
|
2024-06-20 12:54:06 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
if tag == nil {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
|
2024-06-22 23:18:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
pgPost := models.Post{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
2024-06-25 19:55:43 +00:00
|
|
|
ID: anthrovePostID,
|
2024-06-04 21:14:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-19 21:32:42 +00:00
|
|
|
err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag)
|
2024-06-04 21:14:02 +00:00
|
|
|
if err != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.NoDataFound{}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
2024-06-26 08:02:54 +00:00
|
|
|
return errors.Join(err, &otterError.NoRelationCreated{})
|
2024-06-04 21:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
2024-06-25 19:55:43 +00:00
|
|
|
"anthrove_post_id": anthrovePostID,
|
2024-06-04 21:14:02 +00:00
|
|
|
"tag_name": tag.Name,
|
|
|
|
"tag_type": tag.Type,
|
|
|
|
}).Trace("database: created tag node")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 11:05:07 +00:00
|
|
|
|
2024-06-22 21:25:06 +00:00
|
|
|
func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
var tags []models.Tag
|
2024-06-25 19:55:43 +00:00
|
|
|
|
2024-06-14 11:05:07 +00:00
|
|
|
result := db.WithContext(ctx).Find(&tags)
|
2024-06-25 19:55:43 +00:00
|
|
|
|
2024-06-14 11:05:07 +00:00
|
|
|
if result.Error != nil {
|
2024-06-26 07:10:04 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-06-26 08:02:54 +00:00
|
|
|
return nil, &otterError.NoDataFound{}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
2024-06-14 11:05:07 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_amount": len(tags),
|
|
|
|
}).Trace("database: got tags")
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|