82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
|
)
|
|
|
|
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 {
|
|
Transaction *sql.Tx
|
|
}
|
|
|
|
func NewLinkRepository(tx *sql.Tx) ILinkRepository {
|
|
return &linkRepository{Transaction: tx}
|
|
}
|
|
|
|
func (r *linkRepository) GetByNoteID(ctx context.Context, fromNoteID string) ([]*models.Link, error) {
|
|
rows, err := r.Transaction.QueryContext(ctx, "SELECT id, from_note_id, target_slug, raw, display, line, col FROM links WHERE from_note_id = ?", fromNoteID)
|
|
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
|
|
}
|
|
|
|
func (r *linkRepository) GetBySlug(ctx context.Context, targetSlug string) ([]*models.Link, error) {
|
|
rows, err := r.Transaction.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM links WHERE target_slug = ?`, targetSlug)
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|