dendrite.nvim/core/handlers/vault/initialize_handler.go
Kristian c1feac0812
feat(rebuild_guard): only build index on init if empty index (#30)
* feat(persistence): added build-flag and repo funcs

* feat(init_handler): note count check to avoid unnecessary rebuilds

* ref(build_flag): remove build flag is redundant

* build
2026-04-23 20:38:37 +02:00

58 lines
1.4 KiB
Go

package vault
import (
"context"
"encoding/json"
"log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/store"
)
type initializeCommand struct {
VaultName string `json:"vaultName"`
VaultPath string `json:"vaultPath"`
TemplateDirectory string `json:"templateDirectory"`
}
type InitializeHandler struct {
idxRebuilder services.IIndexRebuilder
noteService services.INoteService
}
func NewInitializeHandler(idxR services.IIndexRebuilder, nsv services.INoteService) *InitializeHandler {
return &InitializeHandler{idxRebuilder: idxR, noteService: nsv}
}
func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
var cmd initializeCommand
if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err
}
store := store.NewVaultStore(cmd.VaultName, cmd.VaultPath, cmd.TemplateDirectory)
err := persistence.InitializeDBContext(store.Config.VaultName())
if err != nil {
return nil, err
}
noteCount, err := h.noteService.GetNoteCount(ctx)
if err != nil {
return nil, err
}
if noteCount > 0 {
slog.Debug("Vault already initialized, skipping index rebuild")
return nil, nil
}
err = h.idxRebuilder.RebuildIndex(ctx, cmd.VaultPath)
if err != nil {
return nil, err
}
return nil, nil
}