Skip to content

Commit

Permalink
feat: Implement and test FeedStore.ViewEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
bow committed Oct 13, 2023
1 parent 9a1ee42 commit fb49f94
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 54 deletions.
49 changes: 0 additions & 49 deletions internal/store/edit_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,4 @@ func (s *SQLite) EditEntries(
return entries, nil
}

func getEntry(ctx context.Context, tx *sql.Tx, entryDBID DBID) (*Entry, error) {

sql1 := `
SELECT
e.id AS id,
e.feed_id AS feed_id,
e.title AS title,
e.is_read AS is_read,
e.external_id AS ext_id,
e.description AS description,
e.content AS content,
e.url AS url,
e.update_time AS update_time,
e.publication_time AS publication_time
FROM
entries e
WHERE
e.id = $1
ORDER BY
COALESCE(e.update_time, e.publication_time) DESC
`
scanRow := func(row *sql.Row) (*Entry, error) {
var entry Entry
if err := row.Scan(
&entry.DBID,
&entry.FeedDBID,
&entry.Title,
&entry.IsRead,
&entry.ExtID,
&entry.Description,
&entry.Content,
&entry.URL,
&entry.Updated,
&entry.Published,
); err != nil {
return nil, err
}
return &entry, nil
}

stmt1, err := tx.PrepareContext(ctx, sql1)
if err != nil {
return nil, err
}
defer stmt1.Close()

return scanRow(stmt1.QueryRowContext(ctx, entryDBID))
}

var setEntryIsRead = setTableField[bool]("entries", "is_read")
63 changes: 60 additions & 3 deletions internal/store/view_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ package store
import (
"context"
"database/sql"
"fmt"
"errors"
)

func (s *SQLite) ViewEntry(ctx context.Context, _ DBID) (*Entry, error) {
func (s *SQLite) ViewEntry(ctx context.Context, entryID DBID) (*Entry, error) {
s.mu.Lock()
defer s.mu.Unlock()

var entry *Entry
dbFunc := func(ctx context.Context, tx *sql.Tx) error {
return fmt.Errorf("unimplemented")
ientry, err := getEntry(ctx, tx, entryID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return EntryNotFoundError{entryID}
}
return err
}
entry = ientry
return nil
}

fail := failF("SQLite.ViewFeed")
Expand All @@ -26,3 +34,52 @@ func (s *SQLite) ViewEntry(ctx context.Context, _ DBID) (*Entry, error) {
}
return entry, nil
}

func getEntry(ctx context.Context, tx *sql.Tx, entryDBID DBID) (*Entry, error) {

sql1 := `
SELECT
e.id AS id,
e.feed_id AS feed_id,
e.title AS title,
e.is_read AS is_read,
e.external_id AS ext_id,
e.description AS description,
e.content AS content,
e.url AS url,
e.update_time AS update_time,
e.publication_time AS publication_time
FROM
entries e
WHERE
e.id = $1
ORDER BY
COALESCE(e.update_time, e.publication_time) DESC
`
scanRow := func(row *sql.Row) (*Entry, error) {
var entry Entry
if err := row.Scan(
&entry.DBID,
&entry.FeedDBID,
&entry.Title,
&entry.IsRead,
&entry.ExtID,
&entry.Description,
&entry.Content,
&entry.URL,
&entry.Updated,
&entry.Published,
); err != nil {
return nil, err
}
return &entry, nil
}

stmt1, err := tx.PrepareContext(ctx, sql1)
if err != nil {
return nil, err
}
defer stmt1.Close()

return scanRow(stmt1.QueryRowContext(ctx, entryDBID))
}
23 changes: 21 additions & 2 deletions internal/store/view_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ func TestViewEntryOk(t *testing.T) {
context.Background(),
keys[dbFeeds[1].Title].Entries["Entry X2"],
)
r.EqualError(err, "SQLite.ViewFeed: unimplemented")
a.Nil(dbEntry)
r.NoError(err)
r.NotNil(dbEntry)

a.Equal("Entry X2", dbEntry.Title)
a.True(dbEntry.IsRead)
}

func TestViewEntryErr(t *testing.T) {
t.Parallel()

a := assert.New(t)
r := require.New(t)
st := newTestStore(t)

r.Equal(0, st.countFeeds())

dbEntry, err := st.ViewEntry(context.Background(), 86)
r.Nil(dbEntry)
r.Error(err)

a.EqualError(err, "SQLite.ViewFeed: entry with ID=86 not found")
}

0 comments on commit fb49f94

Please sign in to comment.