From bf2ff4066bff684345d7c594d1ff45172548480d Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 17:24:15 +0200 Subject: [PATCH] fix(repos): pass id to values --- core/handlers/create_note_handler.go | 7 +++++-- core/models/note.go | 21 +++++++++++++++++++++ core/models/tag.go | 17 ++++++++--------- persistence/repositories/note_repository.go | 12 +++++++----- persistence/repositories/tag_repository.go | 2 +- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 139731a..e9b7d4f 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -48,12 +48,13 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an if err != nil { return nil, err } + defer h.uow.Rollback() tagRepo := repositories.NewTagRepository(tx) noteRepo := repositories.NewNoteRepository(tx) - tagModels, err := models.NewTags(tags) + tagModels, err := models.CreateTags(tags) if err != nil { return nil, err } @@ -62,7 +63,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - if err = noteRepo.Upsert(ctx, cmd.Title, cmd.Path, slug); err != nil { + note := models.CreateNote(cmd.Path, cmd.Title, slug) + + if err = noteRepo.Upsert(ctx, note); err != nil { return nil, err } diff --git a/core/models/note.go b/core/models/note.go index 9897a51..8894198 100644 --- a/core/models/note.go +++ b/core/models/note.go @@ -1,5 +1,11 @@ package models +import ( + "time" + + "github.com/google/uuid" +) + type Note struct { id string path string @@ -20,6 +26,21 @@ func NewNote(id, path, title, slug, createdAt, updatedAt string) *Note { } } +func CreateNote(path, title, slug string) *Note { + id, err := uuid.NewV7() + if err != nil { + panic(err) + } + return &Note{ + id: id.String(), + path: path, + title: title, + slug: slug, + createdAt: time.Now().Format("2006-01-02 15:04:05"), + updatedAt: time.Now().Format("2006-01-02 15:04:05"), + } +} + func (n *Note) ID() string { return n.id } diff --git a/core/models/tag.go b/core/models/tag.go index 4e01507..8f6461b 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -7,22 +7,21 @@ type Tag struct { name string } -func NewTag(id string, name string) *Tag { +func NewTag(name string) *Tag { + id, err := uuid.NewV7() + if err != nil { + panic(err) + } return &Tag{ - id: id, + id: id.String(), name: name, } } -func NewTags(tags []string) ([]*Tag, error) { - +func CreateTags(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) + tagModels[i] = NewTag(tag) } return tagModels, nil diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index e9d9444..e7d3425 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -3,10 +3,12 @@ package repositories import ( "context" "database/sql" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/models" ) type NoteRepository interface { - Upsert(ctx context.Context, title string, path string, slug string) error + Upsert(ctx context.Context, note *models.Note) error } type noteRepository struct { @@ -17,14 +19,14 @@ func NewNoteRepository(tx *sql.Tx) NoteRepository { return ¬eRepository{Transaction: tx} } -func (r *noteRepository) Upsert(ctx context.Context, title string, path string, slug string) error { +func (r *noteRepository) Upsert(ctx context.Context, note *models.Note) error { query := ` - INSERT INTO notes (title, path, slug, created_at, updated_at) - VALUES (?, ?, ?, datetime('now'), datetime('now')) + INSERT INTO notes (id, title, path, slug, created_at, updated_at) + VALUES (?, ?, ?, ?, datetime('now'), datetime('now')) ON CONFLICT (slug) DO UPDATE SET title = EXCLUDED.title, path = EXCLUDED.path; ` - _, err := r.Transaction.ExecContext(ctx, query, title, path, slug) + _, err := r.Transaction.ExecContext(ctx, query, note.ID(), note.Title(), note.Path(), note.Slug()) return err } diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go index 7d1c095..a694166 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -33,7 +33,7 @@ func (r *tagRepository) Upsert(ctx context.Context, tags []*models.Tag) error { args = append(args, tag.ID(), tag.Name()) } - query := "INSERT OR IGNORE INTO tags(name) VALUES " + strings.Join(placeholders, ",") + query := "INSERT OR IGNORE INTO tags(id, name) VALUES " + strings.Join(placeholders, ",") _, err := r.Transaction.ExecContext(ctx, query, args...) return err