feat: move some code from config to store

This commit is contained in:
Kristian Borgwarth 2026-04-06 12:12:10 +02:00
parent 614bf67972
commit ffc3435d1e
3 changed files with 30 additions and 15 deletions

View file

@ -9,12 +9,13 @@ import (
"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/store"
)
type createNoteCommand struct {
Title string `json:"title"`
TemplatePath string `json:"templatePath"`
Path string `json:"path"`
TemplateName string `json:"templateName"`
Directory string `json:"directory"`
}
type CreateNoteHandler struct {
@ -34,7 +35,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
slug := frontmatter.Slugify(cmd.Title)
data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title, slug)
templatePath := store.GetVaultStore().GetTemplatePath(cmd.TemplateName)
data, err := template.RenderTemplate(templatePath, cmd.Title, slug)
if err != nil {
return nil, err
}
@ -79,7 +82,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
tagModels = append(tagModels, dbTags...)
note := models.CreateNote(cmd.Path, cmd.Title, slug)
note := models.CreateNote(cmd.Directory, cmd.Title, slug)
if err = noteRepo.Upsert(ctx, note); err != nil {
return nil, err
@ -89,11 +92,11 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return nil, err
}
h.uow.FileStore.Stage(cmd.Path, data)
h.uow.FileStore.Stage(cmd.Directory, data)
if err = h.uow.Commit(); err != nil {
return nil, err
}
return cmd.Path, nil
return cmd.Directory, nil
}

View file

@ -1,7 +1,5 @@
package persistenceconfig
import "path"
type VaultConfiguration struct {
path string
templateDirectory string
@ -15,10 +13,6 @@ func (vc *VaultConfiguration) VaultPath() string {
return vc.path
}
func (vc *VaultConfiguration) fileTypeCheck(templateName string) string {
fileType := path.Ext(templateName)
if fileType != ".md" {
return templateName + ".md"
}
return templateName
func (vc *VaultConfiguration) TemplateDirectory() string {
return vc.templateDirectory
}

View file

@ -1,6 +1,10 @@
package store
import persistenceconfig "github.com/KristianJBorgwarth/dendrite.daemon/persistence/config"
import (
"path"
persistenceconfig "github.com/KristianJBorgwarth/dendrite.daemon/persistence/config"
)
type VaultStore struct {
Config *persistenceconfig.VaultConfiguration
@ -26,3 +30,17 @@ func GetVaultStore() *VaultStore {
func (vs *VaultStore) SetConfig(config persistenceconfig.VaultConfiguration) {
vs.Config = &config
}
func (vs *VaultStore) GetTemplatePath(templateName string) string {
templateName = vs.fileTypeCheck(templateName)
return path.Join(vs.Config.TemplateDirectory(), templateName)
}
func (vs *VaultStore) fileTypeCheck(templateName string) string {
fileType := path.Ext(templateName)
if fileType != ".md" {
return templateName + ".md"
}
return templateName
}