Validation is cheap so do it more

This commit is contained in:
2026-03-19 00:49:35 +00:00
parent de6c9d6ae5
commit 4e5b535f09
3 changed files with 47 additions and 24 deletions

View File

@@ -85,10 +85,14 @@ func (s *SQLiteStore) SaveNote(ctx context.Context, note models.Note) (models.No
}
defer tx.Rollback()
if err := note.Validate(); err != nil {
return models.Note{}, err
}
if note.ID == 0 {
result, err := tx.ExecContext(ctx, `
INSERT INTO notes (content, last_update) VALUES (?, ?);
`, note.Content, note.LastUpdate)
INSERT INTO notes (title, content, last_update) VALUES (?, ?, ?);
`, note.Title, note.Content, note.LastUpdate)
if err != nil {
return models.Note{}, fmt.Errorf("failed to insert note: %w", err)
}
@@ -99,14 +103,14 @@ func (s *SQLiteStore) SaveNote(ctx context.Context, note models.Note) (models.No
note.ID = int(id)
} else {
_, err := s.scanNote(tx.QueryRowContext(ctx, `
SELECT id, content, last_update FROM notes WHERE id = ?;
SELECT id, title, content, last_update FROM notes WHERE id = ?;
`, note.ID))
if err != nil {
return models.Note{}, fmt.Errorf("cannot update note not found: %w", err)
}
_, err = tx.ExecContext(ctx, `
UPDATE notes SET content = ?, last_update = ? WHERE id = ?;
`, note.Content, note.LastUpdate, note.ID)
UPDATE notes SET title = ?, content = ?, last_update = ? WHERE id = ?;
`, note.Title, note.Content, note.LastUpdate, note.ID)
if err != nil {
return models.Note{}, fmt.Errorf("failed to update note: %w", err)
}
@@ -120,7 +124,7 @@ func (s *SQLiteStore) SaveNote(ctx context.Context, note models.Note) (models.No
func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, error) {
row := s.read.QueryRowContext(ctx, `
SELECT id, content, last_update FROM notes WHERE id = ?;
SELECT id, title, content, last_update FROM notes WHERE id = ?;
`, id)
note, err := s.scanNote(row)
if err != nil {
@@ -134,7 +138,7 @@ func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, err
func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) {
rows, err := s.read.QueryContext(ctx, `
SELECT id, content, last_update FROM notes ORDER BY id ASC;
SELECT id, title, content, last_update FROM notes ORDER BY id ASC;
`)
if err != nil {
return nil, fmt.Errorf("failed to query all notes: %w", err)
@@ -177,8 +181,11 @@ func (s *SQLiteStore) validateSchema(ctx context.Context) error {
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
last_update TIMESTAMP NOT NULL,
content TEXT NOT NULL
title TEXT NOT NULL,
content BLOB NOT NULL
);
CREATE UNIQUE INDEX idx_id ON notes(id);
CREATE INDEX idx_title ON notes(title);
`, nil)
return err
}
@@ -193,7 +200,7 @@ func (s *SQLiteStore) getWriteTransaction(ctx context.Context) (*sql.Tx, error)
func (s *SQLiteStore) scanNote(results scannable) (models.Note, error) {
var note models.Note
err := results.Scan(&note.ID, &note.Content, &note.LastUpdate)
err := results.Scan(&note.ID, &note.Title, &note.Content, &note.LastUpdate)
if err != nil {
return models.Note{}, fmt.Errorf("failed to scan note: %w", err)
}