feat(link): resolve link and return note path
This commit is contained in:
parent
f8ba52e62e
commit
225f8a8c6d
4 changed files with 36 additions and 4 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -2,12 +2,15 @@ package note
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||
)
|
||||
|
||||
type gotoNoteCommand struct {
|
||||
Slug string `json:"slug"`
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
type gotoNoteResult struct {
|
||||
|
|
@ -18,10 +21,36 @@ type GotoNoteHandler struct {
|
|||
noteRepo repositories.INoteRepository
|
||||
}
|
||||
|
||||
func (h *GotoNoteHandler) Handle(ctx context.Context, command gotoNoteCommand) (gotoNoteResult, error) {
|
||||
note, err := h.noteRepo.GetBySlug(ctx, command.Slug)
|
||||
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 gotoNoteResult{}, err
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
BIN
dendrite
Binary file not shown.
Loading…
Add table
Reference in a new issue