feat(completeion_link): add completion commands for link
This commit is contained in:
parent
c872cf8b57
commit
af4b8a4ad5
3 changed files with 70 additions and 0 deletions
47
core/handlers/completion/complete_link_handler.go
Normal file
47
core/handlers/completion/complete_link_handler.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package completion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/utils"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||
)
|
||||
|
||||
type completeLinkCommand struct {
|
||||
LinkQuery string `json:"linkQuery"`
|
||||
}
|
||||
|
||||
type CompleteLinkHandler struct{
|
||||
uow *repositories.UnitOfWork
|
||||
}
|
||||
|
||||
func NewCompleteLinkHandler() *CompleteLinkHandler {
|
||||
return &CompleteLinkHandler{repositories.NewUnitOfWork()}
|
||||
}
|
||||
|
||||
func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
|
||||
var cmd completeLinkCommand
|
||||
|
||||
if err := json.Unmarshal(raw, &cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx, err := h.uow.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer h.uow.Rollback()
|
||||
linkRepo := repositories.NewLinkRepository(tx)
|
||||
|
||||
links, err := linkRepo.Search(ctx, cmd.LinkQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slugs := utils.Select(links, func(l *models.Link) string {return l.TargetSlug()});
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
2
core/handlers/completion/doc.go
Normal file
2
core/handlers/completion/doc.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Package completion provides handlers for completion commands
|
||||
package completion
|
||||
|
|
@ -10,6 +10,7 @@ import (
|
|||
type ILinkRepository interface {
|
||||
GetByNoteID(ctx context.Context, fromNoteID string) ([]*models.Link, error)
|
||||
GetBySlug(ctx context.Context, targetSlug string) ([]*models.Link, error)
|
||||
Search(ctx context.Context, query string) ([]*models.Link, error)
|
||||
}
|
||||
|
||||
type linkRepository struct {
|
||||
|
|
@ -59,3 +60,23 @@ func (r *linkRepository) GetBySlug(ctx context.Context, targetSlug string) ([]*m
|
|||
|
||||
return links, nil
|
||||
}
|
||||
|
||||
func (r *linkRepository) Search(ctx context.Context, query string) ([]*models.Link, error) {
|
||||
rows, err := r.Transaction.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM links WHERE raw LIKE ?`, "%"+query+"%")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var links []*models.Link
|
||||
for rows.Next() {
|
||||
var id, fromNoteID, targetSlug, raw, display string
|
||||
var line, col int
|
||||
if err := rows.Scan(&id, &fromNoteID, &targetSlug, &raw, &display, &line, &col); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
links = append(links, models.NewLink(id, fromNoteID, targetSlug, raw, display, line, col))
|
||||
}
|
||||
|
||||
return links, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue