diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index f43fb61..99310a9 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -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 } diff --git a/persistence/config/vault_configuration.go b/persistence/config/vault_configuration.go index 006aac1..cd9a16a 100644 --- a/persistence/config/vault_configuration.go +++ b/persistence/config/vault_configuration.go @@ -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 } diff --git a/persistence/store/vault_store.go b/persistence/store/vault_store.go index d64a8b1..17cbfca 100644 --- a/persistence/store/vault_store.go +++ b/persistence/store/vault_store.go @@ -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 +} +