Merge pull request #25 from KristianJBorgwarth/feat/goto-note

feat(goto_note): resolve links and returns note paths
This commit is contained in:
Kristian 2026-04-14 21:14:30 +02:00 committed by GitHub
commit 0098badafa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 70 additions and 8 deletions

View file

@ -22,7 +22,7 @@ func main() {
uow := repositories.NewUnitOfWork();
linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext())
tagRepo := repositories.NewTagRepository()
noteRepo := repositories.NewNoteRepository()
noteRepo := repositories.NewNoteRepository(*persistence.NewReadContext())
tagService := services.NewTagService(tagRepo)
linkService := services.NewLinkService(linkRepo)
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
@ -30,6 +30,7 @@ func main() {
server.RegisterHandler("vault/init", vault.NewInitializeHandler())
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler(linkRepo))
if err := server.Run(os.Stdin, os.Stdout); err != nil {

View file

@ -0,0 +1,56 @@
package note
import (
"context"
"encoding/json"
"log/slog"
"strings"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type gotoNoteCommand struct {
Link string `json:"link"`
}
type gotoNoteResult struct {
Path string `json:"path"`
}
type GotoNoteHandler struct {
noteRepo repositories.INoteRepository
}
func NewGotoNoteHandler(noteRepo repositories.INoteRepository) *GotoNoteHandler {
return &GotoNoteHandler{noteRepo}
}
func (h *GotoNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
var cmd gotoNoteCommand
if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err
}
cmd.Link = h.resolveLink(cmd.Link)
note, err := h.noteRepo.GetBySlug(ctx, cmd.Link)
if err != nil {
return nil, err
}
if note == nil {
slog.Debug("Note not found for link", "link", cmd.Link)
return nil, nil
}
return gotoNoteResult{Path: note.Path()}, nil
}
func (h *GotoNoteHandler) resolveLink(link string) string {
if len(link) < 5 || link[:2] != "[[" || link[len(link)-2:] != "]]" {
return link
}
content := link[2 : len(link)-2]
if before, _, ok := strings.Cut(content, "|"); ok {
return before
}
return content
}

View file

@ -54,7 +54,7 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
}
defer h.uow.Rollback()
note, err := h.noteRepo.GetBySlug(ctx, tx, file.Slug)
note, err := h.noteRepo.GetBySlug(ctx, file.Slug)
if err != nil {
slog.Debug("Failed to get note by slug", "slug", file.Slug, "error", err)
return nil, err

View file

@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
@ -82,6 +83,7 @@ func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) {
Error: err,
}
slog.Debug("Responding to request", "response", resp)
s.write(w, resp)
}

BIN
dendrite

Binary file not shown.

View file

@ -12,12 +12,14 @@ import (
type INoteRepository interface {
Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
GetBySlug(ctx context.Context, slug string) (*models.Note, error)
}
type noteRepository struct{}
type noteRepository struct{
readDBContext persistence.ReadContext
}
func NewNoteRepository() INoteRepository {
func NewNoteRepository(persistence.ReadContext) INoteRepository {
return &noteRepository{}
}
@ -46,9 +48,9 @@ func (r *noteRepository) Update(ctx context.Context, dbContext persistence.IDbCo
return nil
}
func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) {
func (r *noteRepository) GetBySlug(ctx context.Context, slug string) (*models.Note, error) {
query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?`
row := dbContext.QueryRowContext(ctx, query, slug)
row := r.readDBContext.QueryRowContext(ctx, query, slug)
var id, title, path, createdAt, updatedAt string
err := row.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt)

View file

@ -8,6 +8,7 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -17,7 +18,7 @@ func newCreateNoteHandler() *note.CreateNoteHandler {
return note.NewCreateNoteHandler(
repositories.NewUnitOfWork(),
services.NewTagService(repositories.NewTagRepository()),
repositories.NewNoteRepository(),
repositories.NewNoteRepository(*persistence.NewReadContext()),
)
}