diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 6a045f6..7990a1f 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -5,8 +5,10 @@ import ( "encoding/json" "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/persistence/repositories" + "github.com/google/uuid" ) type createNoteCommand struct { @@ -26,6 +28,7 @@ func NewCreateNoteHandler(uow *repositories.UnitOfWork) *CreateNoteHandler { func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { var cmd createNoteCommand + if err := json.Unmarshal(raw, &cmd); err != nil { return nil, err } @@ -51,6 +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 { + tagModels[i] = models.NewTag(uuidv7.New(), tag) + } + if err = tagRepo.Upsert(ctx, tags); err != nil { return nil, err } @@ -59,6 +67,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } + h.uow.FileStore.Stage(cmd.Path, data) if err = h.uow.Commit(); err != nil { diff --git a/core/models/note.go b/core/models/note.go index c95b71b..9897a51 100644 --- a/core/models/note.go +++ b/core/models/note.go @@ -1,7 +1,7 @@ package models type Note struct { - id int + id string path string title string slug string @@ -9,7 +9,7 @@ type Note struct { updatedAt string } -func NewNote(id int, path, title, slug, createdAt, updatedAt string) *Note { +func NewNote(id, path, title, slug, createdAt, updatedAt string) *Note { return &Note{ id: id, path: path, @@ -20,7 +20,7 @@ func NewNote(id int, path, title, slug, createdAt, updatedAt string) *Note { } } -func (n *Note) ID() int { +func (n *Note) ID() string { return n.id } diff --git a/core/models/note_tag.go b/core/models/note_tag.go index ec36634..3b6dd36 100644 --- a/core/models/note_tag.go +++ b/core/models/note_tag.go @@ -1,21 +1,21 @@ package models type NoteTag struct { - noteID int - tagID int + noteID string + tagID string } -func NewNoteTag(noteID, tagID int) *NoteTag { +func NewNoteTag(noteID, tagID string) *NoteTag { return &NoteTag{ noteID: noteID, tagID: tagID, } } -func (nt *NoteTag) NoteID() int { +func (nt *NoteTag) NoteID() string { return nt.noteID } -func (nt *NoteTag) TagID() int { +func (nt *NoteTag) TagID() string { return nt.tagID } diff --git a/core/models/tag.go b/core/models/tag.go index e36d306..814441e 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -1,18 +1,20 @@ package models +import "github.com/google/uuid" + type Tag struct { - id int + id string name string } -func NewTag(id int, name string) *Tag { +func NewTag(id uuid.UUID, name string) *Tag { return &Tag{ - id: id, + id: id.String(), name: name, } } -func (t *Tag) ID() int { +func (t *Tag) ID() string { return t.id } diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go index a49257e..f922ee3 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -4,31 +4,33 @@ import ( "context" "database/sql" "strings" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/models" ) -type TagRepository interface { - Upsert(ctx context.Context, names []string) error +type ITagRepository interface { + Upsert(ctx context.Context, tags []models.Tag) error } type tagRepository struct { Transaction *sql.Tx } -func NewTagRepository(tx *sql.Tx) TagRepository { +func NewTagRepository(tx *sql.Tx) ITagRepository { return &tagRepository{Transaction: tx} } -func (r *tagRepository) Upsert(ctx context.Context, names []string) error { - if len(names) == 0 { +func (r *tagRepository) Upsert(ctx context.Context, tags []models.Tag) error { + if len(tags) == 0 { return nil } - placeholders := make([]string, 0, len(names)) - args := make([]any, 0, len(names)) + placeholders := make([]string, 0, len(tags)) + args := make([]any, 0, len(tags)) - for _, name := range names { - placeholders = append(placeholders, "(?)") - args = append(args, name) + for _, tag := range tags { + placeholders = append(placeholders, "(?, ?)") + args = append(args, tag.ID(), tag.Name()) } query := "INSERT OR IGNORE INTO tags(name) VALUES " + strings.Join(placeholders, ",")