diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-29 00:18:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-29 00:18:40 +0300 |
| commit | 655b01e5b19b72dc133e7b24c658e0ec3f611bb0 (patch) | |
| tree | fbfcd935fe6e447a308a37b80fcf2752f74a2512 /internal/repository/note.go | |
| parent | 5b5978dabf2011a64720998cd03cbb01c706475d (diff) | |
feat: SQLite schema migrations, repository interfaces, and concrete SQLite implementations with :memory: table-driven tests (task l9)
Diffstat (limited to 'internal/repository/note.go')
| -rw-r--r-- | internal/repository/note.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/repository/note.go b/internal/repository/note.go new file mode 100644 index 0000000..0809e57 --- /dev/null +++ b/internal/repository/note.go @@ -0,0 +1,45 @@ +package repository + +import ( + "context" + "fmt" + + "github.com/paul/kiss-media-player/internal/model" +) + +// UpsertNote inserts or replaces a note for a user and media. +func (s *SQLite) UpsertNote(ctx context.Context, note *model.Note) error { + _, err := s.db.ExecContext(ctx, + `INSERT INTO media_notes (media_id, user_id, content, created_at, updated_at) VALUES (?, ?, ?, ?, ?) + ON CONFLICT(media_id, user_id) DO UPDATE SET content = excluded.content, updated_at = excluded.updated_at`, + note.MediaID, note.UserID, note.Content, note.CreatedAt, note.UpdatedAt, + ) + if err != nil { + return fmt.Errorf("upsert note: %w", err) + } + return nil +} + +// GetNote retrieves a note for a user and media. +func (s *SQLite) GetNote(ctx context.Context, mediaID, userID int64) (*model.Note, error) { + row := s.db.QueryRowContext(ctx, + `SELECT id, media_id, user_id, content, created_at, updated_at FROM media_notes WHERE media_id = ? AND user_id = ?`, + mediaID, userID, + ) + var n model.Note + if err := row.Scan(&n.ID, &n.MediaID, &n.UserID, &n.Content, &n.CreatedAt, &n.UpdatedAt); err != nil { + return nil, err + } + return &n, nil +} + +// DeleteNote removes a note for a user and media. +func (s *SQLite) DeleteNote(ctx context.Context, mediaID, userID int64) error { + _, err := s.db.ExecContext(ctx, + `DELETE FROM media_notes WHERE media_id = ? AND user_id = ?`, mediaID, userID, + ) + if err != nil { + return fmt.Errorf("delete note: %w", err) + } + return nil +} |
