From 7872296b838f4d1b26c6a0a01d79d27fe5ab44cc Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 15 Apr 2023 21:07:16 +0200 Subject: Move asset store into its own package --- src/post/asset/asset.go | 116 +++++++++++++++++++++++++++++++++++++++++++ src/post/asset/asset_test.go | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 src/post/asset/asset.go create mode 100644 src/post/asset/asset_test.go (limited to 'src/post/asset') diff --git a/src/post/asset/asset.go b/src/post/asset/asset.go new file mode 100644 index 0000000..d20b347 --- /dev/null +++ b/src/post/asset/asset.go @@ -0,0 +1,116 @@ +package asset + +import ( + "bytes" + "database/sql" + "errors" + "fmt" + "io" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/post" +) + +var ( + // ErrNotFound is used to indicate an Asset could not be found in the + // Store. + ErrNotFound = errors.New("asset not found") +) + +// Store implements the storage and retrieval of binary assets, which are +// intended to be used by posts (e.g. images). +type Store interface { + + // Set sets the id to the contents of the given io.Reader. + Set(id string, from io.Reader) error + + // Get writes the id's body to the given io.Writer, or returns + // ErrNotFound. + Get(id string, into io.Writer) error + + // Delete's the body stored for the id, if any. + Delete(id string) error + + // List returns all ids which are currently stored. + List() ([]string, error) +} + +type store struct { + db *post.SQLDB +} + +// NewStore initializes a new Store using an existing SQLDB. +func NewStore(db *post.SQLDB) Store { + return &store{ + db: db, + } +} + +func (s *store) Set(id string, from io.Reader) error { + + body, err := io.ReadAll(from) + if err != nil { + return fmt.Errorf("reading body fully into memory: %w", err) + } + + _, err = s.db.Exec( + `INSERT INTO assets (id, body) + VALUES (?, ?) + ON CONFLICT (id) DO UPDATE SET body=excluded.body`, + id, body, + ) + + if err != nil { + return fmt.Errorf("inserting into assets: %w", err) + } + + return nil +} + +func (s *store) Get(id string, into io.Writer) error { + + var body []byte + + err := s.db.QueryRow(`SELECT body FROM assets WHERE id = ?`, id).Scan(&body) + + if errors.Is(err, sql.ErrNoRows) { + return ErrNotFound + } else if err != nil { + return fmt.Errorf("selecting from assets: %w", err) + } + + if _, err := io.Copy(into, bytes.NewReader(body)); err != nil { + return fmt.Errorf("writing body to io.Writer: %w", err) + } + + return nil +} + +func (s *store) Delete(id string) error { + _, err := s.db.Exec(`DELETE FROM assets WHERE id = ?`, id) + return err +} + +func (s *store) List() ([]string, error) { + + rows, err := s.db.Query(`SELECT id FROM assets ORDER BY id ASC`) + + if err != nil { + return nil, fmt.Errorf("querying: %w", err) + } + + defer rows.Close() + + var ids []string + + for rows.Next() { + + var id string + if err := rows.Scan(&id); err != nil { + return nil, fmt.Errorf("scanning row: %w", err) + } + + ids = append(ids, id) + } + + return ids, nil +} diff --git a/src/post/asset/asset_test.go b/src/post/asset/asset_test.go new file mode 100644 index 0000000..574cc08 --- /dev/null +++ b/src/post/asset/asset_test.go @@ -0,0 +1,92 @@ +package asset + +import ( + "bytes" + "io" + "testing" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/post" + "github.com/stretchr/testify/assert" +) + +type testHarness struct { + store Store +} + +func newTestHarness(t *testing.T) *testHarness { + + db := post.NewInMemSQLDB() + t.Cleanup(func() { db.Close() }) + + store := NewStore(db) + + return &testHarness{ + store: store, + } +} + +func (h *testHarness) assertGet(t *testing.T, exp, id string) { + t.Helper() + buf := new(bytes.Buffer) + err := h.store.Get(id, buf) + assert.NoError(t, err) + assert.Equal(t, exp, buf.String()) +} + +func (h *testHarness) assertNotFound(t *testing.T, id string) { + t.Helper() + err := h.store.Get(id, io.Discard) + assert.ErrorIs(t, ErrNotFound, err) +} + +func TestStore(t *testing.T) { + + testStore := func(t *testing.T, h *testHarness) { + t.Helper() + + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") + + err := h.store.Set("foo", bytes.NewBufferString("FOO")) + assert.NoError(t, err) + + h.assertGet(t, "FOO", "foo") + h.assertNotFound(t, "bar") + + err = h.store.Set("foo", bytes.NewBufferString("FOOFOO")) + assert.NoError(t, err) + + h.assertGet(t, "FOOFOO", "foo") + h.assertNotFound(t, "bar") + + assert.NoError(t, h.store.Delete("foo")) + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") + + assert.NoError(t, h.store.Delete("bar")) + h.assertNotFound(t, "foo") + h.assertNotFound(t, "bar") + + // test list + + ids, err := h.store.List() + assert.NoError(t, err) + assert.Empty(t, ids) + + err = h.store.Set("foo", bytes.NewBufferString("FOOFOO")) + assert.NoError(t, err) + err = h.store.Set("foo", bytes.NewBufferString("FOOFOO")) + assert.NoError(t, err) + err = h.store.Set("bar", bytes.NewBufferString("FOOFOO")) + assert.NoError(t, err) + + ids, err = h.store.List() + assert.NoError(t, err) + assert.Equal(t, []string{"bar", "foo"}, ids) + } + + t.Run("sql", func(t *testing.T) { + h := newTestHarness(t) + testStore(t, h) + }) +} -- cgit v1.2.3