feat(link_repo): added slug and id query
This commit is contained in:
parent
a74d8dc5a7
commit
75155dc536
1 changed files with 61 additions and 0 deletions
61
persistence/repositories/link_repository.go
Normal file
61
persistence/repositories/link_repository.go
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue