diff --git a/core/handlers/completion/complete_link_handler.go b/core/handlers/completion/complete_link_handler.go index 4e460ef..4ea8f17 100644 --- a/core/handlers/completion/complete_link_handler.go +++ b/core/handlers/completion/complete_link_handler.go @@ -4,20 +4,25 @@ import ( "context" "encoding/json" "log/slog" + + "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) type completeLinkCommand struct { Query string `json:"query"` } -type completionItem struct { - Slug string `json:"slug"` +type completeLinkResult struct { + Slug string `json:"slug"` + Display string `json:"display"` } -type CompleteLinkHandler struct{} +type CompleteLinkHandler struct { + linkRepo repositories.ILinkRepository +} -func NewCompleteLinkHandler() *CompleteLinkHandler { - return &CompleteLinkHandler{} +func NewCompleteLinkHandler(lr repositories.ILinkRepository) *CompleteLinkHandler { + return &CompleteLinkHandler{linkRepo: lr} } func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { @@ -25,12 +30,16 @@ func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) ( if err := json.Unmarshal(raw, &cmd); err != nil { return nil, err } - slog.Debug("handling complete link command", "query", cmd.Query) + links, err := h.linkRepo.Search(ctx, cmd.Query) + if err != nil { + slog.Error("Failed to search links", "error", err) + return nil, err + } - // this is just a stub and the client is expecting the title/display too - return []completionItem{ - {Slug: "standard-streams"}, - {Slug: "unit-of-work"}, - {Slug: "treesitter-basics"}, - }, nil + items := make([]completeLinkResult, len(links)) + for i, link := range links { + items[i] = completeLinkResult{Slug: link.TargetSlug(), Display: link.Display()} + } + + return items, nil } diff --git a/persistence/repositories/link_repository.go b/persistence/repositories/link_repository.go index dc45cbe..957b921 100644 --- a/persistence/repositories/link_repository.go +++ b/persistence/repositories/link_repository.go @@ -12,14 +12,16 @@ type ILinkRepository interface { Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error GetByNoteID(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) ([]*models.Link, error) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, targetSlug string) ([]*models.Link, error) - Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) + Search(ctx context.Context, query string) ([]*models.Link, error) Delete(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) error } -type linkRepository struct{} +type linkRepository struct { + readDBContext persistence.ReadContext +} -func NewLinkRepository() ILinkRepository { - return &linkRepository{} +func NewLinkRepository(rdb persistence.ReadContext) ILinkRepository { + return &linkRepository{readDBContext: rdb} } func (r *linkRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error { @@ -81,8 +83,8 @@ func (r *linkRepository) GetBySlug(ctx context.Context, dbContext persistence.ID return links, nil } -func (r *linkRepository) Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) { - rows, err := dbContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%") +func (r *linkRepository) Search(ctx context.Context, query string) ([]*models.Link, error) { + rows, err := r.readDBContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%") if err != nil { return nil, err }