feat(create): added upsert_note_tags method call on create
This commit is contained in:
parent
bf2ff4066b
commit
8195a8d5ba
3 changed files with 18 additions and 8 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
10
core/utils/doc.go
Normal file
10
core/utils/doc.go
Normal 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
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue