From ddd8ac837e130e9ba0a9978aa1b22d5216b2ffc4 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 12:02:31 +0200 Subject: [PATCH 1/7] feat(models): updated model ids --- core/handlers/create_note_handler.go | 9 +++++++++ core/models/note.go | 6 +++--- core/models/note_tag.go | 10 +++++----- core/models/tag.go | 10 ++++++---- persistence/repositories/tag_repository.go | 22 ++++++++++++---------- 5 files changed, 35 insertions(+), 22 deletions(-) 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, ",") From 2471f5546a1aff290102a087ab3157d40f01d188 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 12:05:12 +0200 Subject: [PATCH 2/7] ref(migrations): updated existing migrations to text id --- core/models/links.go | 10 +++++----- persistence/migrations/001_dendrite_db_init.sql | 6 +++--- persistence/migrations/002_tags.sql | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/models/links.go b/core/models/links.go index 538a2d8..c870840 100644 --- a/core/models/links.go +++ b/core/models/links.go @@ -1,12 +1,12 @@ package models type Link struct { - fromNoteID int - toNoteID int + fromNoteID string + toNoteID string raw string } -func NewLink(fromNoteID, toNoteID int, raw string) *Link { +func NewLink(fromNoteID, toNoteID, raw string) *Link { return &Link{ fromNoteID: fromNoteID, toNoteID: toNoteID, @@ -14,11 +14,11 @@ func NewLink(fromNoteID, toNoteID int, raw string) *Link { } } -func (l *Link) FromNoteID() int { +func (l *Link) FromNoteID() string { return l.fromNoteID } -func (l *Link) ToNoteID() int { +func (l *Link) ToNoteID() string { return l.toNoteID } diff --git a/persistence/migrations/001_dendrite_db_init.sql b/persistence/migrations/001_dendrite_db_init.sql index 1ebfc49..90584bc 100644 --- a/persistence/migrations/001_dendrite_db_init.sql +++ b/persistence/migrations/001_dendrite_db_init.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS notes ( - id INTEGER PRIMARY KEY, + id TEXT PRIMARY KEY, path TEXT UNIQUE, title TEXT, slug TEXT UNIQUE NOT NULL, @@ -8,8 +8,8 @@ CREATE TABLE IF NOT EXISTS notes ( ); CREATE TABLE IF NOT EXISTS links ( - from_note_id INTEGER NOT NULL, - to_note_id INTEGER NOT NULL, + from_note_id TEXT NOT NULL, + to_note_id TEXT NOT NULL, raw TEXT, FOREIGN KEY(from_note_id) REFERENCES notes(id) ON DELETE CASCADE, FOREIGN KEY(to_note_id) REFERENCES notes(id) ON DELETE CASCADE diff --git a/persistence/migrations/002_tags.sql b/persistence/migrations/002_tags.sql index 739968a..f8e59dc 100644 --- a/persistence/migrations/002_tags.sql +++ b/persistence/migrations/002_tags.sql @@ -1,11 +1,11 @@ CREATE TABLE IF NOT EXISTS tags ( - id INTEGER PRIMARY KEY, + id TEXT PRIMARY KEY, name TEXT UNIQUE ); CREATE TABLE IF NOT EXISTS note_tags ( - note_id INTEGER, - tag_id INTEGER, + note_id TEXT, + tag_id TEXT, PRIMARY KEY (note_id, tag_id), FOREIGN KEY(note_id) REFERENCES notes(id) ON DELETE CASCADE, FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE From d716086db180e1704cf9c861ad4af3f4be0e4869 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 12:38:07 +0200 Subject: [PATCH 3/7] feat(tag_repo): updated tag repository --- core/handlers/create_note_handler.go | 12 +++++++++--- core/models/tag.go | 5 ++--- go.mod | 2 +- persistence/repositories/tag_repository.go | 1 + 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 7990a1f..da50500 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -7,6 +7,7 @@ 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" ) @@ -56,10 +57,16 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an tagModels := make([]models.Tag, len(tags)) for i, tag := range tags { - tagModels[i] = models.NewTag(uuidv7.New(), tag) + + id, err := uuid.NewV7() + if err != nil { + return nil, err + } + + tagModels[i] = *models.NewTag(id.String(), tag) } - if err = tagRepo.Upsert(ctx, tags); err != nil { + if err = tagRepo.Upsert(ctx, tagModels); err != nil { return nil, err } @@ -67,7 +74,6 @@ 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/tag.go b/core/models/tag.go index 814441e..00c0648 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -1,15 +1,14 @@ package models -import "github.com/google/uuid" type Tag struct { id string name string } -func NewTag(id uuid.UUID, name string) *Tag { +func NewTag(id string, name string) *Tag { return &Tag{ - id: id.String(), + id: id, name: name, } } diff --git a/go.mod b/go.mod index 1254b4e..c43cce8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/KristianJBorgwarth/dendrite.daemon go 1.26 require ( + github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.11.1 modernc.org/sqlite v1.47.0 ) @@ -10,7 +11,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go index f922ee3..89a8357 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -39,6 +39,7 @@ func (r *tagRepository) Upsert(ctx context.Context, tags []models.Tag) error { return err } + func (r *tagRepository) UpsertNoteTags(noteID int64, tagIDs []int64) error { if len(tagIDs) == 0 { return nil From b132b7e45004e2380b6ad970856e9a1580abb55b Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 12:45:05 +0200 Subject: [PATCH 4/7] sm(tag): moved tag create to tag file --- core/handlers/create_note_handler.go | 13 +++---------- core/models/tag.go | 15 +++++++++++++++ persistence/repositories/tag_repository.go | 4 ++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index da50500..b7fffea 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -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,15 +54,9 @@ 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() - if err != nil { - return nil, err - } - - tagModels[i] = *models.NewTag(id.String(), tag) + tagModels, err := models.NewTags(tags) + if err != nil { + return nil, err } if err = tagRepo.Upsert(ctx, tagModels); err != nil { diff --git a/core/models/tag.go b/core/models/tag.go index 00c0648..4e01507 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -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 } diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go index 89a8357..7d1c095 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -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 } From 4da1ae032278043fc4e5180f3569666ba3d7d065 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 13:22:20 +0200 Subject: [PATCH 5/7] feat(note_tags): bulk create func --- core/handlers/create_note_handler.go | 1 - core/models/note_tag.go | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index b7fffea..139731a 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -8,7 +8,6 @@ import ( "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 { diff --git a/core/models/note_tag.go b/core/models/note_tag.go index 3b6dd36..c7e6090 100644 --- a/core/models/note_tag.go +++ b/core/models/note_tag.go @@ -12,6 +12,14 @@ func NewNoteTag(noteID, tagID string) *NoteTag { } } +func NewNoteTags(noteID string, tagIDs []string) []*NoteTag { + noteTags := make([]*NoteTag, len(tagIDs)) + for i, tagID := range tagIDs { + noteTags[i] = NewNoteTag(noteID, tagID) + } + return noteTags +} + func (nt *NoteTag) NoteID() string { return nt.noteID } 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 6/7] 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 From 8195a8d5ba5c9c008eab5c5e0764462b40b36846 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 20:32:12 +0200 Subject: [PATCH 7/7] feat(create): added upsert_note_tags method call on create --- core/handlers/create_note_handler.go | 5 +++++ core/utils/doc.go | 10 ++++++++++ persistence/repositories/tag_repository.go | 11 +++-------- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 core/utils/doc.go diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index e9b7d4f..a61c7fe 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -7,6 +7,7 @@ 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/utils" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) @@ -69,6 +70,10 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } + if err = tagRepo.UpsertNoteTags(note.ID(), utils.Select(tagModels, func(t *models.Tag) string { return t.ID() })); err != nil { + return nil, err + } + h.uow.FileStore.Stage(cmd.Path, data) if err = h.uow.Commit(); err != nil { diff --git a/core/utils/doc.go b/core/utils/doc.go new file mode 100644 index 0000000..f8b0649 --- /dev/null +++ b/core/utils/doc.go @@ -0,0 +1,10 @@ +// Package utils provides BASIC FUCKING utilities +package utils + +func Select[T any, U any](input []T, mapper func(T) U) []U { + output := make([]U, len(input)) + for i, item := range input { + output[i] = mapper(item) + } + return output +} diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go index a694166..334e846 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -10,6 +10,7 @@ import ( type ITagRepository interface { Upsert(ctx context.Context, tags []*models.Tag) error + UpsertNoteTags(noteID string, tagIDs []string) error } type tagRepository struct { @@ -39,8 +40,7 @@ func (r *tagRepository) Upsert(ctx context.Context, tags []*models.Tag) error { return err } - -func (r *tagRepository) UpsertNoteTags(noteID int64, tagIDs []int64) error { +func (r *tagRepository) UpsertNoteTags(noteID string, tagIDs []string) error { if len(tagIDs) == 0 { return nil } @@ -53,12 +53,7 @@ func (r *tagRepository) UpsertNoteTags(noteID int64, tagIDs []int64) error { args = append(args, noteID, tagID) } - query := "WITH input(note_id, tag_id) AS (VALUES " + - strings.Join(placeholders, ",") + - ") INSERT INTO note_tags(note_id, tag_id) " + - "SELECT note_id, tag_id FROM input " + - "ON CONFLICT(note_id, tag_id) DO NOTHING" + - "SELECT note_id, tag_id FROM input;" + query := "INSERT OR IGNORE INTO note_tags(note_id, tag_id) VALUES " + strings.Join(placeholders, ",") _, err := r.Transaction.ExecContext(context.Background(), query, args...) if err != nil {