Implement get notes

This commit is contained in:
Micheal Wilkinson
2026-03-17 23:27:17 +00:00
parent a8b7b8e71b
commit c453f1582a

View File

@@ -22,6 +22,10 @@ type SQLiteStore struct {
write *sql.DB write *sql.DB
} }
type scannable interface {
Scan(dest ...any) error
}
func NewSQLiteStore(ctx context.Context, dbPath string) (*SQLiteStore, error) { func NewSQLiteStore(ctx context.Context, dbPath string) (*SQLiteStore, error) {
sqliteDB := &SQLiteStore{} sqliteDB := &SQLiteStore{}
@@ -129,7 +133,26 @@ func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, err
} }
func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) { func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) {
return nil, fmt.Errorf("not implemented") rows, err := s.read.QueryContext(ctx, `
SELECT id, content, last_update FROM notes ORDER BY last_update DESC;
`)
if err != nil {
return nil, fmt.Errorf("failed to query all notes: %w", err)
}
defer rows.Close()
var notes []models.Note
for rows.Next() {
note, err := s.scanNote(rows)
if err != nil {
return nil, fmt.Errorf("failed to scan note: %w", err)
}
notes = append(notes, note)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over notes: %w", err)
}
return notes, nil
} }
func (s *SQLiteStore) validateSchema(ctx context.Context) error { func (s *SQLiteStore) validateSchema(ctx context.Context) error {
@@ -151,7 +174,7 @@ func (s *SQLiteStore) getWriteTransaction(ctx context.Context) (*sql.Tx, error)
return tx, nil return tx, nil
} }
func (s *SQLiteStore) scanNote(results *sql.Row) (models.Note, error) { func (s *SQLiteStore) scanNote(results scannable) (models.Note, error) {
var note models.Note var note models.Note
err := results.Scan(&note.ID, &note.Content, &note.LastUpdate) err := results.Scan(&note.ID, &note.Content, &note.LastUpdate)
if err != nil { if err != nil {