dendrite.daemon/core/handlers/note/save_note_handler.go
Kristian 2275d4bae4
feat(vault/rebuild): added rebuild command / handler (#31)
* feat(vault/rebuild_index): add rebuild command

* remove comments

* rm unec using

* remove unec command

* build
2026-04-23 21:18:09 +02:00

88 lines
2 KiB
Go

package note
import (
"context"
"encoding/json"
filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type saveNoteCommand struct {
Path string `json:"path"`
}
type SaveNoteHandler struct {
uow *repositories.UnitOfWork
noteRepo repositories.INoteRepository
tagService services.ITagService
noteService services.INoteService
linkService services.ILinkService
}
func NewSaveNoteHandler(
uow *repositories.UnitOfWork,
nr repositories.INoteRepository,
ts services.ITagService,
ns services.INoteService,
ls services.ILinkService,
) *SaveNoteHandler {
return &SaveNoteHandler{uow, nr, ts, ns, ls}
}
func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
var cmd saveNoteCommand
if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err
}
file, err := filehandling.ReadFile(cmd.Path)
if err != nil {
return nil, err
}
tx, err := h.uow.Begin()
if err != nil {
return nil, err
}
defer h.uow.Rollback()
note, err := h.noteRepo.GetBySlug(ctx, file.Slug)
if err != nil {
return nil, err
}
if note == nil {
note, err = h.noteService.CreateNote(ctx, tx, file.Path, file.Title, file.Slug)
if err != nil {
return nil, err
}
} else {
if err = h.noteService.DeleteNoteMetaData(ctx, tx, note.ID()); err != nil {
return nil, err
}
if err = h.noteService.UpdateNote(ctx, tx, note.ID(), file.Path, file.Title, file.Slug); err != nil {
return nil, err
}
}
tagModels, err := h.tagService.CreateTags(ctx, tx, file.FrontMatter.Tags)
if err != nil {
return nil, err
}
if err = h.tagService.CreateNoteTags(ctx, tx, note.ID(), tagModels); err != nil {
return nil, err
}
if err = h.linkService.CreateLinks(ctx, tx, note.ID(), file.ExtractedLinks); err != nil {
return nil, err
}
if err := h.uow.Commit(); err != nil {
return nil, err
}
return nil, nil
}