feat(create): added upsert_note_tags method call on create

This commit is contained in:
Kristian Borgwarth 2026-04-03 20:32:12 +02:00
parent bf2ff4066b
commit 8195a8d5ba
3 changed files with 18 additions and 8 deletions

View file

@ -7,6 +7,7 @@ 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/utils"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "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 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) h.uow.FileStore.Stage(cmd.Path, data)
if err = h.uow.Commit(); err != nil { if err = h.uow.Commit(); err != nil {

10
core/utils/doc.go Normal file
View file

@ -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
}

View file

@ -10,6 +10,7 @@ import (
type ITagRepository interface { type ITagRepository interface {
Upsert(ctx context.Context, tags []*models.Tag) error Upsert(ctx context.Context, tags []*models.Tag) error
UpsertNoteTags(noteID string, tagIDs []string) error
} }
type tagRepository struct { type tagRepository struct {
@ -39,8 +40,7 @@ func (r *tagRepository) Upsert(ctx context.Context, tags []*models.Tag) error {
return err return err
} }
func (r *tagRepository) UpsertNoteTags(noteID string, tagIDs []string) error {
func (r *tagRepository) UpsertNoteTags(noteID int64, tagIDs []int64) error {
if len(tagIDs) == 0 { if len(tagIDs) == 0 {
return nil return nil
} }
@ -53,12 +53,7 @@ func (r *tagRepository) UpsertNoteTags(noteID int64, tagIDs []int64) error {
args = append(args, noteID, tagID) args = append(args, noteID, tagID)
} }
query := "WITH input(note_id, tag_id) AS (VALUES " + query := "INSERT OR IGNORE INTO note_tags(note_id, tag_id) VALUES " + strings.Join(placeholders, ",")
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;"
_, err := r.Transaction.ExecContext(context.Background(), query, args...) _, err := r.Transaction.ExecContext(context.Background(), query, args...)
if err != nil { if err != nil {