Compare commits

..

No commits in common. "4025f038269c09bd78e611657b228039948271c5" and "38b4cb815f2fac265c85cf6cd5699bc44b5282db" have entirely different histories.

28 changed files with 89 additions and 555 deletions

1
.gitignore vendored
View file

@ -1 +0,0 @@
dendrite

View file

@ -26,24 +26,21 @@ func main() {
linkRepo := repositories.NewLinkRepository(persistence.NewReadContext())
tagRepo := repositories.NewTagRepository(persistence.NewReadContext())
noteRepo := repositories.NewNoteRepository(persistence.NewReadContext())
cfeRepo := repositories.NewCfeRepository(persistence.NewReadContext())
tagService := services.NewTagService(tagRepo)
linkService := services.NewLinkService(linkRepo)
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo, cfeRepo)
cfeSvc := services.NewCfeService(cfeRepo)
idxr := services.NewIndexRebuilder(uow, noteRepo, linkRepo, tagRepo, indexRepo, cfeRepo)
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
idxr := services.NewIndexRebuilder(uow, noteRepo, linkRepo, tagRepo, indexRepo )
server.RegisterHandler("vault/init", vault.NewInitializeHandler(idxr, noteService))
server.RegisterHandler("vault/rebuild", vault.NewRebuildIndexHandler(idxr))
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo, cfeSvc))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService, cfeSvc))
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
server.RegisterHandler("note/delete", note.NewDeleteNoteHandler(uow, tagService, noteService))
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
server.RegisterHandler("note/backlinks", note.NewGetBackLinksHandler(linkRepo, noteRepo))
server.RegisterHandler("note/search_by_tag", note.NewGetNotesByTagHandler(noteRepo))
server.RegisterHandler("note/search_by_cf", note.NewGetNotesByCfeHandler(cfeRepo))
server.RegisterHandler("completion/tag", completion.NewCompleteTagHandler(tagRepo))
server.RegisterHandler("completion/slug", completion.NewCompleteSlugHandler(noteRepo))

View file

@ -17,11 +17,3 @@ func NewNoteDto(note *models.Note) *NoteDto {
Slug: note.Slug(),
}
}
func NewNoteDtos(notes []*models.Note) []*NoteDto {
dtos := make([]*NoteDto, len(notes))
for i, note := range notes {
dtos[i] = NewNoteDto(note)
}
return dtos
}

View file

@ -7,12 +7,12 @@ import (
)
type FrontMatter struct {
Title string `yaml:"title"`
Tags []string `yaml:"tags"`
Created string `yaml:"created"`
Updated string `yaml:"updated"`
Date string `yaml:"date"`
Custom map[string]any `yaml:",inline"`
Title string `yaml:"title"`
Tags []string `yaml:"tags"`
Created string `yaml:"created"`
Updated string `yaml:"updated"`
Date string `yaml:"date"`
Author string `yaml:"author"`
}
func ParseFrontMatter(file []byte) (*FrontMatter, error) {

View file

@ -20,18 +20,16 @@ type createNoteCommand struct {
type CreateNoteHandler struct {
uow *repositories.UnitOfWork
tagSvc services.ITagService
tagService services.ITagService
noteRepo repositories.INoteRepository
cfeSvc services.ICfService
}
func NewCreateNoteHandler(
uow *repositories.UnitOfWork,
tagSvc services.ITagService,
tagRepo services.ITagService,
noteRepo repositories.INoteRepository,
cfeSvc services.ICfService,
) *CreateNoteHandler {
return &CreateNoteHandler{uow, tagSvc, noteRepo, cfeSvc}
return &CreateNoteHandler{uow, tagRepo, noteRepo}
}
func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
@ -64,7 +62,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return notePath, nil
}
tagModels, err := h.tagSvc.CreateTags(ctx, dbCtx, template.FrontMatter.Tags)
tagModels, err := h.tagService.CreateTags(ctx, dbCtx, template.FrontMatter.Tags)
if err != nil {
return nil, err
}
@ -75,11 +73,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return nil, err
}
if err = h.tagSvc.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
return nil, err
}
if err = h.cfeSvc.AddCfe(ctx, dbCtx, note.ID(), template.FrontMatter.Custom); err != nil {
if err = h.tagService.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
return nil, err
}

View file

@ -1,47 +0,0 @@
package note
import (
"context"
"encoding/json"
"github.com/KristianJBorgwarth/dendrite.daemon/core/dtos"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type getNotesByCfQuery struct {
Key string `json:"key"`
Value string `json:"value"`
}
type GetNotesByCfHandler struct {
cfeRepo repositories.ICfRepository
}
func NewGetNotesByCfeHandler(
cfeRepo repositories.ICfRepository,
) *GetNotesByCfHandler {
return &GetNotesByCfHandler{cfeRepo: cfeRepo}
}
func (h *GetNotesByCfHandler) Handle(
ctx context.Context,
raw json.RawMessage,
) (any, error) {
var query getNotesByCfQuery
if err := json.Unmarshal(raw, &query); err != nil {
return nil, err
}
notes, err := h.cfeRepo.Search(ctx, query.Key, query.Value)
if err != nil {
return nil, err
}
if len(notes) == 0 {
return make([]*dtos.NoteDto,0), nil
}
noteDtos := dtos.NewNoteDtos(notes)
return noteDtos, nil
}

View file

@ -7,7 +7,6 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/dtos"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type getNotesByTagCommand struct {
Tag string `json:"tag"`
}
@ -31,7 +30,10 @@ func (h *GetNotesByTagHandler) Handle(ctx context.Context, raw json.RawMessage)
return nil, err
}
noteDtos := dtos.NewNoteDtos(notes)
noteDtos := make([]*dtos.NoteDto, len(notes))
for i, note := range notes {
noteDtos[i] = dtos.NewNoteDto(note)
}
return noteDtos, nil
}

View file

@ -20,7 +20,6 @@ type SaveNoteHandler struct {
tagService services.ITagService
noteService services.INoteService
linkService services.ILinkService
cfeSvc services.ICfService
}
func NewSaveNoteHandler(
@ -29,9 +28,8 @@ func NewSaveNoteHandler(
ts services.ITagService,
ns services.INoteService,
ls services.ILinkService,
cfs services.ICfService,
) *SaveNoteHandler {
return &SaveNoteHandler{uow, nr, ts, ns, ls, cfs}
return &SaveNoteHandler{uow, nr, ts, ns, ls}
}
func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
@ -86,10 +84,6 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
return nil, err
}
if err = h.cfeSvc.AddCfe(ctx, tx, note.ID(), file.FrontMatter.Custom); err != nil {
return nil, err
}
if err := h.uow.Commit(); err != nil {
return nil, err
}

View file

@ -3,7 +3,6 @@ package vault
import (
"context"
"encoding/json"
"errors"
"log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
@ -12,22 +11,11 @@ import (
)
type initializeCommand struct {
Config Config `json:"config"`
}
type Config struct {
VaultName string `json:"vault_name"`
VaultPath string `json:"vault_path"`
TemplatesDir string `json:"templates_dir"`
ExcludeIndexFiles []string `json:"exclude_index_files"`
OverrideDefaultIgnores bool `json:"override_default_ignores"`
DailyNotes DailyNotes `json:"daily_notes"`
}
type DailyNotes struct {
Dir string `json:"dir"`
FilenameFormat string `json:"filename_format"`
TemplateName string `json:"template_name"`
VaultName string `json:"vaultName"`
VaultPath string `json:"vaultPath"`
TemplateDirectory string `json:"templateDirectory"`
ExcludeIndexFiles []string `json:"excludeIndexFiles"`
OverrideDefaultIgnores bool `json:"overrideDefaultIgnores"`
}
type InitializeHandler struct {
@ -46,20 +34,12 @@ func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any
return nil, err
}
if cmd.Config.VaultPath == "" {
return nil, errors.New("vault_path is empty — check your dendrite.nvim config")
}
store := store.NewVaultStore(
cmd.Config.VaultName,
cmd.Config.VaultPath,
cmd.Config.TemplatesDir,
cmd.Config.ExcludeIndexFiles,
cmd.Config.OverrideDefaultIgnores,
cmd.Config.DailyNotes.Dir,
cmd.Config.DailyNotes.FilenameFormat,
cmd.Config.DailyNotes.TemplateName,
)
cmd.VaultName,
cmd.VaultPath,
cmd.TemplateDirectory,
cmd.ExcludeIndexFiles,
cmd.OverrideDefaultIgnores)
err := persistence.InitializeDBContext(store.Config.VaultName())
if err != nil {
@ -76,7 +56,7 @@ func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any
return nil, nil
}
err = h.idxRebuilder.RebuildIndex(ctx, cmd.Config.VaultPath)
err = h.idxRebuilder.RebuildIndex(ctx, cmd.VaultPath)
if err != nil {
return nil, err
}

View file

@ -1,27 +0,0 @@
package models
type CustomFronMatter struct {
nodeID string
key string
value string
}
func NewCustomFrontMatter(nodeID, key, value string) *CustomFronMatter {
return &CustomFronMatter{
nodeID: nodeID,
key: key,
value: value,
}
}
func (cfm *CustomFronMatter) NodeID() string {
return cfm.nodeID
}
func (cfm *CustomFronMatter) Key() string {
return cfm.key
}
func (cfm *CustomFronMatter) Value() string {
return cfm.value
}

View file

@ -1,11 +1,6 @@
package models
import (
"errors"
"log/slog"
filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
)
import filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
func MapToLinkModel(noteID string, extractedLinks []*filehandling.ExtractedLink) []*Link {
var links []*Link
@ -14,24 +9,3 @@ func MapToLinkModel(noteID string, extractedLinks []*filehandling.ExtractedLink)
}
return links
}
func MapToCustomFrontmatter(noteID string, extractedCfe map[string]any) ([]*CustomFronMatter, error) {
var cfe []*CustomFronMatter
for key, value := range extractedCfe {
if valueStr, ok := value.(string); ok {
cfe = append(cfe, NewCustomFrontMatter(noteID, key, valueStr))
continue
} else if valueArr, ok := value.([]any); ok {
for _, v := range valueArr {
if s, ok := v.(string); ok {
cfe = append(cfe, NewCustomFrontMatter(noteID, key, s))
}
}
continue
} else {
slog.Warn("Unsupported CFE value type, skipping", "key", key, "value", value)
return nil, errors.New("unsupported CFE value type")
}
}
return cfe, nil
}

View file

@ -1,37 +0,0 @@
package services
import (
"context"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type ICfService interface {
AddCfe(ctx context.Context, dbCtx persistence.IDbContext, noteID string, cfe map[string]any) error
}
type cfService struct {
cfRepo repositories.ICfRepository
}
func NewCfeService(cfRepo repositories.ICfRepository) ICfService {
return &cfService{cfRepo}
}
func (s *cfService) AddCfe(
ctx context.Context,
dbCtx persistence.IDbContext,
noteID string,
cfe map[string]any,
) error {
if len(cfe) == 0 {
return nil
}
cfModels, err := models.MapToCustomFrontmatter(noteID, cfe)
if err != nil {
return err
}
return s.cfRepo.InsertRange(ctx, dbCtx, cfModels)
}

View file

@ -15,14 +15,6 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/store"
)
type index struct {
notes []*models.Note
links []*models.Link
cfe []*models.CustomFronMatter
tags []*models.Tag
noteTags []*models.NoteTag
}
type IIndexRebuilder interface {
RebuildIndex(ctx context.Context, vaultRoot string) error
}
@ -33,7 +25,6 @@ type indexRebuilder struct {
linkRepo repositories.ILinkRepository
tagRepo repositories.ITagRepository
indexRepo repositories.IIndexRepository
cfeRepo repositories.ICfRepository
}
func NewIndexRebuilder(
@ -42,7 +33,6 @@ func NewIndexRebuilder(
linkRepo repositories.ILinkRepository,
tagRepo repositories.ITagRepository,
indexRepo repositories.IIndexRepository,
cfeRepo repositories.ICfRepository,
) *indexRebuilder {
return &indexRebuilder{
uow: uow,
@ -50,7 +40,6 @@ func NewIndexRebuilder(
linkRepo: linkRepo,
tagRepo: tagRepo,
indexRepo: indexRepo,
cfeRepo: cfeRepo,
}
}
@ -65,16 +54,13 @@ func (r *indexRebuilder) RebuildIndex(ctx context.Context, vaultRoot string) err
return err
}
index, err := r.buildDBModels(files)
if err != nil {
return err
}
notes, links, tags, noteTags := r.buildDBModels(files)
if err = r.indexRepo.WipeIndex(ctx, dbctx); err != nil {
return err
}
if err = r.buildIndex(ctx, dbctx, index); err != nil {
if err = r.buildIndex(ctx, dbctx, notes, links, tags, noteTags); err != nil {
return err
}
@ -88,25 +74,24 @@ func (r *indexRebuilder) RebuildIndex(ctx context.Context, vaultRoot string) err
func (r *indexRebuilder) buildIndex(
ctx context.Context,
dbctx persistence.IDbContext,
index *index,
notes []*models.Note,
links []*models.Link,
tags []*models.Tag,
noteTags []*models.NoteTag,
) error {
if err := r.noteRepo.InsertRange(ctx, dbctx, index.notes); err != nil {
if err := r.noteRepo.InsertRange(ctx, dbctx, notes); err != nil {
return err
}
if err := r.linkRepo.InsertRange(ctx, dbctx, index.links); err != nil {
if err := r.linkRepo.InsertRange(ctx, dbctx, links); err != nil {
return err
}
if err := r.tagRepo.InsertRange(ctx, dbctx, index.tags); err != nil {
if err := r.tagRepo.InsertRange(ctx, dbctx, tags); err != nil {
return err
}
if err := r.tagRepo.InsertNoteTags(ctx, dbctx, index.noteTags); err != nil {
return err
}
if err := r.cfeRepo.InsertRange(ctx, dbctx, index.cfe); err != nil {
if err := r.tagRepo.InsertNoteTags(ctx, dbctx, noteTags); err != nil {
return err
}
@ -161,10 +146,9 @@ func (r *indexRebuilder) IsValidDirectory(path string) bool {
return true
}
func (r *indexRebuilder) buildDBModels(files []*filehandling.File) (*index, error) {
func (r *indexRebuilder) buildDBModels(files []*filehandling.File) ([]*models.Note, []*models.Link, []*models.Tag, []*models.NoteTag) {
var notes []*models.Note
var links []*models.Link
var cfe []*models.CustomFronMatter
tagMap := make(map[string]*models.Tag)
var noteTags []*models.NoteTag
@ -176,12 +160,6 @@ func (r *indexRebuilder) buildDBModels(files []*filehandling.File) (*index, erro
}
noteTags = append(noteTags, models.CreateNoteTags(note.ID(), file.FrontMatter.Tags)...)
links = append(links, models.MapToLinkModel(note.ID(), file.ExtractedLinks)...)
mappedCfe, err := models.MapToCustomFrontmatter(note.ID(), file.FrontMatter.Custom)
if err != nil {
return nil, err
}
cfe = append(cfe, mappedCfe...)
}
tags := make([]*models.Tag, 0, len(tagMap))
@ -189,11 +167,5 @@ func (r *indexRebuilder) buildDBModels(files []*filehandling.File) (*index, erro
tags = append(tags, tag)
}
return &index{
notes: notes,
links: links,
tags: tags,
noteTags: noteTags,
cfe: cfe,
}, nil
return notes, links, tags, noteTags
}

View file

@ -21,16 +21,14 @@ type noteService struct {
tagRepo repositories.ITagRepository
linkRepo repositories.ILinkRepository
noteRepo repositories.INoteRepository
cfeRepo repositories.ICfRepository
}
func NewNoteService(
tagRepo repositories.ITagRepository,
linkRepo repositories.ILinkRepository,
noteRepo repositories.INoteRepository,
cfeRepo repositories.ICfRepository,
) INoteService {
return &noteService{tagRepo, linkRepo, noteRepo, cfeRepo}
return &noteService{tagRepo, linkRepo, noteRepo}
}
func (s *noteService) CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) {
@ -58,10 +56,6 @@ func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence.
return err
}
if err := s.cfeRepo.Delete(ctx, dbCtx, noteID); err != nil {
return err
}
return nil
}

BIN
dendrite Executable file

Binary file not shown.

View file

@ -49,10 +49,8 @@ ASSET="${BINARY}-${GOOS}-${GOARCH}"
URL="https://github.com/$REPO/releases/download/$TAG/$ASSET"
echo "Downloading $ASSET ($TAG)..."
TMP="$(mktemp)"
curl -fsSL "$URL" -o "$TMP"
chmod +x "$TMP"
mv "$TMP" "$INSTALL_DIR/$BINARY"
curl -fsSL "$URL" -o "$INSTALL_DIR/$BINARY"
chmod +x "$INSTALL_DIR/$BINARY"
echo ""
echo "dendrite $TAG installed to $INSTALL_DIR/$BINARY"

View file

@ -7,6 +7,3 @@ test-unit:
test-integration:
go test ./test/test_integration/...
build:
go build -o dendrite ./cmd

View file

@ -1,61 +1,50 @@
package persistenceconfig
type DailyNotes struct {
dir string
filenameFormat string
templateName string
}
func NewDailyNotes(dir, filenameFormat, templateName string) DailyNotes {
return DailyNotes{
dir: dir,
filenameFormat: filenameFormat,
templateName: templateName,
}
}
type VaultConfig struct {
vaultName string
vaultPath string
templatesDir string
excludeIndexFiles []string
type VaultConfiguration struct {
name string
path string
templateDirectory string
exludeIndexFiles []string
defaultExcludes []string
overrideDefaultIgnores bool
dailyNotes DailyNotes
}
func NewVaultConfiguration(
vaultName,
vaultPath,
templatesDir string,
templateDirectory string,
excludeIndexFiles []string,
overrideDefaultIgnores bool,
dailyNotes DailyNotes,
) *VaultConfig {
return &VaultConfig{
vaultName: vaultName,
vaultPath: vaultPath,
templatesDir: templatesDir,
dailyNotes: dailyNotes,
) *VaultConfiguration {
return &VaultConfiguration{
name: vaultName,
path: vaultPath,
templateDirectory: templateDirectory,
exludeIndexFiles: excludeIndexFiles,
defaultExcludes: []string{".git", ".templates"},
}
}
func (vc *VaultConfig) VaultName() string {
return vc.vaultName
func (vc *VaultConfiguration) VaultName() string {
return vc.name
}
func (vc *VaultConfig) VaultPath() string {
return vc.vaultPath
func (vc *VaultConfiguration) VaultPath() string {
return vc.path
}
func (vc *VaultConfig) TemplateDirectory() string {
return vc.templatesDir
func (vc *VaultConfiguration) TemplateDirectory() string {
return vc.templateDirectory
}
func (vc *VaultConfig) ExcludeIndexFiles() []string {
defaultIgnores := []string{"index.md", "index", ".templates"}
func (vc *VaultConfiguration) ExcludeIndexFiles() []string {
return vc.exludeIndexFiles
}
func (vc *VaultConfiguration) GetExcludeIndexFiles() []string {
defaultIgnores := []string{".git", ".templates"}
if vc.overrideDefaultIgnores {
return vc.excludeIndexFiles
return vc.exludeIndexFiles
}
return append(defaultIgnores, vc.excludeIndexFiles...)
return append(defaultIgnores, vc.exludeIndexFiles...)
}

View file

@ -1,9 +0,0 @@
CREATE TABLE custom_frontmatter(
note_id TEXT,
key TEXT,
value TEXT,
PRIMARY KEY (note_id, key, value),
FOREIGN KEY (note_id) REFERENCES note(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_cfe_key_value on custom_frontmatter(key, value);

View file

@ -1,91 +0,0 @@
package repositories
import (
"context"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
)
type ICfRepository interface {
InsertRange(ctx context.Context, dbContext persistence.IDbContext, cfe []*models.CustomFronMatter) error
Search(ctx context.Context, key string, value string) ([]*models.Note, error)
Delete(ctx context.Context, dbContext persistence.IDbContext, noteID string) error
}
type cfeRepository struct {
readDBContext persistence.ReadContext
}
func NewCfeRepository(rdb persistence.ReadContext) ICfRepository {
return &cfeRepository{readDBContext: rdb}
}
func (r *cfeRepository) InsertRange(
ctx context.Context,
dbCtx persistence.IDbContext,
cfe []*models.CustomFronMatter,
) error {
statement, err := dbCtx.Prepare(
`INSERT INTO custom_frontmatter (note_id, key, value)
VALUES (?, ?, ?) ON CONFLICT DO NOTHING;`)
if err != nil {
return err
}
for _, c := range cfe {
if _, err := statement.ExecContext(ctx, c.NodeID(), c.Key(), c.Value()); err != nil {
return err
}
}
return nil
}
func (r *cfeRepository) Delete(
ctx context.Context,
dbCtx persistence.IDbContext,
noteID string,
) error {
statement, err := dbCtx.Prepare(`DELETE FROM custom_frontmatter WHERE note_id = ?;`)
if err != nil {
return err
}
if _, err := statement.ExecContext(ctx, noteID); err != nil {
return err
}
return nil
}
func (r *cfeRepository) Search(
ctx context.Context,
key string,
value string,
) ([]*models.Note, error) {
rows, err := r.readDBContext.QueryContext(
ctx,
`SELECT n.id, n.path, n.title, n.slug, n.created_at, n.updated_at
FROM note n
INNER JOIN custom_frontmatter cfe ON n.id = cfe.note_id
WHERE cfe.key = ? AND cfe.value LIKE ?;`,
key,
"%"+value+"%",
)
if err != nil {
return nil, err
}
defer rows.Close()
var notes []*models.Note
for rows.Next() {
var id, title, path, slug, createdAt, updatedAT string
if err := rows.Scan(&id, &title, &path, &slug, &createdAt, &updatedAT); err != nil {
return nil, err
}
notes = append(notes, models.NewNote(id, title, path, slug, createdAt, updatedAT))
}
return notes, nil
}

View file

@ -22,8 +22,7 @@ func (r *indexRepository) WipeIndex(ctx context.Context, dbContext persistence.I
cmd := `DELETE FROM note;
DELETE FROM tag;
DELETE FROM note_tag;
DELETE FROM link;
DELETE FROM custom_frontmatter;`
DELETE FROM link;`
_, err := dbContext.ExecContext(ctx, cmd)
return err

View file

@ -7,7 +7,7 @@ import (
)
type VaultStore struct {
Config *persistenceconfig.VaultConfig
Config *persistenceconfig.VaultConfiguration
}
var vaultStore *VaultStore
@ -18,9 +18,6 @@ func NewVaultStore(
templateDir string,
exludeIndexFiles []string,
overrideDefaultIgnores bool,
dailyDir string,
dailyFilenameFormat string,
dailyTemplateName string,
) *VaultStore {
if vaultStore != nil {
return vaultStore
@ -31,7 +28,7 @@ func NewVaultStore(
templateDir,
exludeIndexFiles,
overrideDefaultIgnores,
persistenceconfig.NewDailyNotes(dailyDir, dailyFilenameFormat, dailyTemplateName))}
)}
return vaultStore
}
@ -42,10 +39,13 @@ func GetVaultStore() *VaultStore {
return vaultStore
}
func (vs *VaultStore) SetConfig(config persistenceconfig.VaultConfiguration) {
vs.Config = &config
}
func (vs *VaultStore) GetTemplatePath(templateName string) string {
templateName = vs.fileTypeCheck(templateName)
path := path.Join(vs.Config.VaultPath(), vs.Config.TemplateDirectory(), templateName)
return path
return path.Join(vs.Config.TemplateDirectory(), templateName)
}
func (vs *VaultStore) fileTypeCheck(templateName string) string {
@ -57,5 +57,5 @@ func (vs *VaultStore) fileTypeCheck(templateName string) string {
}
func (vs *VaultStore) GetExcludeIndexFiles() []string {
return vs.Config.ExcludeIndexFiles()
return vs.Config.GetExcludeIndexFiles()
}

View file

@ -19,7 +19,6 @@ func newCreateNoteHandler() *note.CreateNoteHandler {
repositories.NewUnitOfWork(),
services.NewTagService(repositories.NewTagRepository(persistence.NewReadContext())),
repositories.NewNoteRepository(persistence.NewReadContext()),
services.NewCfeService(repositories.NewCfeRepository(persistence.NewReadContext())),
)
}
@ -55,7 +54,7 @@ func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t
handler := newCreateNoteHandler()
vaultPath := Fixture.VaultStore.Config.VaultPath()
templateDir := filepath.Join(Fixture.VaultStore.Config.VaultPath(), Fixture.VaultStore.Config.TemplateDirectory())
templateDir := Fixture.VaultStore.Config.TemplateDirectory()
require.NoError(t, os.MkdirAll(templateDir, 0o755))
templatePath := filepath.Join(templateDir, "template.md")

View file

@ -1,74 +0,0 @@
package integration_test
import (
"encoding/json"
"testing"
"github.com/KristianJBorgwarth/dendrite.daemon/core/dtos"
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
"github.com/KristianJBorgwarth/dendrite.daemon/test/test_integration/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newGetNotesByCfeHandler() *note.GetNotesByCfHandler {
return note.NewGetNotesByCfeHandler(
repositories.NewCfeRepository(persistence.NewReadContext()),
)
}
func Test_Handle_ReturnsNotesWithMacthingCfe(t *testing.T) {
// Arrange
handler := newGetNotesByCfeHandler()
noteID := "test-note-id"
cfeKey := "author"
cfeValues := []string{"John Doe", "Jane Smith"}
utils.CreateNote(Fixture.TestContext, Fixture.DB, noteID, "Test Note", "test-note.md", "test");
utils.CreateCfe(Fixture.TestContext, Fixture.DB, noteID,
cfeKey, cfeValues)
params, _ := json.Marshal(map[string]any{
"key": "author",
"value": "J",
})
// Act
result, err := handler.Handle(Fixture.TestContext, params)
require.NoError(t, err)
require.NotNil(t, result)
noteDtos, ok := result.([]*dtos.NoteDto)
require.True(t, ok)
assert.Len(t, noteDtos, 2)
}
func Test_Handle_ReturnsEmptyListWhenNoMatchingCfe(t *testing.T) {
// Arrange
handler := newGetNotesByCfeHandler()
noteID := "test-note-id-2"
cfeKey := "category"
cfeValues := []string{"Tech", "Lifestyle"}
utils.CreateNote(Fixture.TestContext, Fixture.DB, noteID, "Another Test Note", "another-test-note.md", "test");
utils.CreateCfe(Fixture.TestContext, Fixture.DB, noteID,
cfeKey, cfeValues)
params, _ := json.Marshal(map[string]any{
"key": "category",
"value": "NonExistingCategory",
})
// Act
result, err := handler.Handle(Fixture.TestContext, params)
require.NoError(t, err)
require.NotNil(t, result)
noteDtos, ok := result.([]*dtos.NoteDto)
require.True(t, ok)
assert.Len(t, noteDtos, 0)
}

View file

@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"os"
"path"
"path/filepath"
"testing"
@ -45,7 +46,7 @@ func NewDBFixture() *DBFixture {
DB: dbContext.DB,
DBPath: dbPath,
TestContext: context.Background(),
VaultStore: store.NewVaultStore("testVault", vaultPath, "templates", []string{}, false, "", "", ""),
VaultStore: store.NewVaultStore("testVault", vaultPath, path.Join(vaultPath, "templates"), []string{}, false),
}
}
@ -53,7 +54,9 @@ var Fixture = NewDBFixture()
func TestMain(m *testing.M) {
code := m.Run()
os.RemoveAll(Fixture.DBPath)
os.Exit(code)
}

View file

@ -1,32 +0,0 @@
package utils
import (
"context"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
)
func CreateCfe(
ctx context.Context,
dbCtx persistence.IDbContext,
noteID, key string,
values []string,
) error {
for _, value := range values {
cfe := models.NewCustomFrontMatter(noteID, key, value)
statement, err := dbCtx.Prepare(
`INSERT INTO custom_frontmatter (note_id, key, value)
VALUES (?, ?, ?) ON CONFLICT DO NOTHING;`)
if err != nil {
return err
}
if _, err := statement.ExecContext(ctx, cfe.NodeID(), cfe.Key(), cfe.Value()); err != nil {
return err
}
}
return nil
}

View file

@ -1,30 +0,0 @@
package utils
import (
"context"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
)
func CreateNote(
ctx context.Context,
dbCtx persistence.IDbContext,
noteID,
path,
title,
slug string,
) error {
statement, err := dbCtx.Prepare(`
INSERT INTO note (id, title, path, slug, created_at, updated_at)
VALUES (?, ?, ?, ?, datetime('now'), datetime('now'))
ON CONFLICT DO NOTHING;`)
if err != nil {
return err
}
if _, err := statement.ExecContext(ctx, noteID, title, path, slug); err != nil {
return err
}
return nil
}

View file

@ -1,2 +0,0 @@
// Package utils provides testing utilities for integration tests
package utils