diff --git a/cmd/main.go b/cmd/main.go index a950bc3..810c6dd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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 { diff --git a/core/handlers/note/goto_note_handler.go b/core/handlers/note/goto_note_handler.go new file mode 100644 index 0000000..0b51a9a --- /dev/null +++ b/core/handlers/note/goto_note_handler.go @@ -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 +} diff --git a/core/handlers/note/save_note_handler.go b/core/handlers/note/save_note_handler.go index cdd4e1e..6fb7534 100644 --- a/core/handlers/note/save_note_handler.go +++ b/core/handlers/note/save_note_handler.go @@ -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 diff --git a/core/server/server.go b/core/server/server.go index 4a1c8dd..82f55e1 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -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) } diff --git a/dendrite b/dendrite index ef81490..cc35ace 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index 9b7d1cc..5f8741f 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -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 ¬eRepository{} } @@ -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) diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index fcbdeda..66a607d 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -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()), ) }