sm(tag): moved tag create to tag file
This commit is contained in:
parent
d716086db1
commit
b132b7e450
3 changed files with 20 additions and 12 deletions
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/template"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/template"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/utilities"
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
@ -55,15 +54,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
tagRepo := repositories.NewTagRepository(tx)
|
tagRepo := repositories.NewTagRepository(tx)
|
||||||
noteRepo := repositories.NewNoteRepository(tx)
|
noteRepo := repositories.NewNoteRepository(tx)
|
||||||
|
|
||||||
tagModels := make([]models.Tag, len(tags))
|
tagModels, err := models.NewTags(tags)
|
||||||
for i, tag := range tags {
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
id, err := uuid.NewV7()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tagModels[i] = *models.NewTag(id.String(), tag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = tagRepo.Upsert(ctx, tagModels); err != nil {
|
if err = tagRepo.Upsert(ctx, tagModels); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
id string
|
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 {
|
func (t *Tag) ID() string {
|
||||||
return t.id
|
return t.id
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ITagRepository interface {
|
type ITagRepository interface {
|
||||||
Upsert(ctx context.Context, tags []models.Tag) error
|
Upsert(ctx context.Context, tags []*models.Tag) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type tagRepository struct {
|
type tagRepository struct {
|
||||||
|
|
@ -20,7 +20,7 @@ func NewTagRepository(tx *sql.Tx) ITagRepository {
|
||||||
return &tagRepository{Transaction: tx}
|
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 {
|
if len(tags) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue