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 {