feat(link_repo): use rdb for queries

This commit is contained in:
Kristian Borgwarth 2026-04-14 05:41:29 +02:00
parent acdf6bf216
commit 840460da57
2 changed files with 29 additions and 18 deletions

View file

@ -4,20 +4,25 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"log/slog" "log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
) )
type completeLinkCommand struct { type completeLinkCommand struct {
Query string `json:"query"` Query string `json:"query"`
} }
type completionItem struct { type completeLinkResult struct {
Slug string `json:"slug"` Slug string `json:"slug"`
Display string `json:"display"`
} }
type CompleteLinkHandler struct{} type CompleteLinkHandler struct {
linkRepo repositories.ILinkRepository
}
func NewCompleteLinkHandler() *CompleteLinkHandler { func NewCompleteLinkHandler(lr repositories.ILinkRepository) *CompleteLinkHandler {
return &CompleteLinkHandler{} return &CompleteLinkHandler{linkRepo: lr}
} }
func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { 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 { if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err 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 items := make([]completeLinkResult, len(links))
return []completionItem{ for i, link := range links {
{Slug: "standard-streams"}, items[i] = completeLinkResult{Slug: link.TargetSlug(), Display: link.Display()}
{Slug: "unit-of-work"}, }
{Slug: "treesitter-basics"},
}, nil return items, nil
} }

View file

@ -12,14 +12,16 @@ type ILinkRepository interface {
Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error
GetByNoteID(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) ([]*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) 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 Delete(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) error
} }
type linkRepository struct{} type linkRepository struct {
readDBContext persistence.ReadContext
}
func NewLinkRepository() ILinkRepository { func NewLinkRepository(rdb persistence.ReadContext) ILinkRepository {
return &linkRepository{} return &linkRepository{readDBContext: rdb}
} }
func (r *linkRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error { 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 return links, nil
} }
func (r *linkRepository) Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) { func (r *linkRepository) Search(ctx context.Context, 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+"%") 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 { if err != nil {
return nil, err return nil, err
} }