sm(tag): moved tag create to tag file

This commit is contained in:
Kristian Borgwarth 2026-04-03 12:45:05 +02:00
parent d716086db1
commit b132b7e450
3 changed files with 20 additions and 12 deletions

View file

@ -7,7 +7,6 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/core/template"
"github.com/KristianJBorgwarth/dendrite.daemon/core/utilities"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
"github.com/google/uuid"
)
@ -55,17 +54,11 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
tagRepo := repositories.NewTagRepository(tx)
noteRepo := repositories.NewNoteRepository(tx)
tagModels := make([]models.Tag, len(tags))
for i, tag := range tags {
id, err := uuid.NewV7()
tagModels, err := models.NewTags(tags)
if err != nil {
return nil, err
}
tagModels[i] = *models.NewTag(id.String(), tag)
}
if err = tagRepo.Upsert(ctx, tagModels); err != nil {
return nil, err
}

View file

@ -1,5 +1,6 @@
package models
import "github.com/google/uuid"
type Tag struct {
id string
@ -13,6 +14,20 @@ func NewTag(id string, name string) *Tag {
}
}
func NewTags(tags []string) ([]*Tag, error) {
tagModels := make([]*Tag, len(tags))
for i, tag := range tags {
id, err := uuid.NewV7()
if err != nil {
return nil, err
}
tagModels[i] = NewTag(id.String(), tag)
}
return tagModels, nil
}
func (t *Tag) ID() string {
return t.id
}

View file

@ -9,7 +9,7 @@ import (
)
type ITagRepository interface {
Upsert(ctx context.Context, tags []models.Tag) error
Upsert(ctx context.Context, tags []*models.Tag) error
}
type tagRepository struct {
@ -20,7 +20,7 @@ func NewTagRepository(tx *sql.Tx) ITagRepository {
return &tagRepository{Transaction: tx}
}
func (r *tagRepository) Upsert(ctx context.Context, tags []models.Tag) error {
func (r *tagRepository) Upsert(ctx context.Context, tags []*models.Tag) error {
if len(tags) == 0 {
return nil
}