This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/internal/postgres/tag.go
2024-06-04 23:14:02 +02:00

39 lines
828 B
Go

package postgres
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.AnthrovePostID, tag *pgModels.Tag) error {
resultTag := db.WithContext(ctx).Where(tag).FirstOrCreate(tag)
if resultTag.Error != nil {
return resultTag.Error
}
pgPost := pgModels.Post{
BaseModel: pgModels.BaseModel{
ID: string(PostID),
},
}
err := db.WithContext(ctx).Model(pgPost).Association("Tags").Append(tag)
if err != nil {
return err
}
log.WithFields(log.Fields{
"anthrove_post_id": PostID,
"tag_name": tag.Name,
"tag_type": tag.Type,
}).Trace("database: created tag node")
return nil
}