Skip to content

Commit 9fc69fd

Browse files
committed
refactor(storage): update IconByFeedID to handle sql.ErrNoRows
1 parent cddd2eb commit 9fc69fd

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

internal/api/icon.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ import (
1313
func (h *handler) getIconByFeedID(w http.ResponseWriter, r *http.Request) {
1414
feedID := request.RouteInt64Param(r, "feedID")
1515

16-
if !h.store.HasFeedIcon(feedID) {
17-
json.NotFound(w, r)
18-
return
19-
}
20-
2116
icon, err := h.store.IconByFeedID(request.UserID(r), feedID)
2217
if err != nil {
2318
json.ServerError(w, r, err)

internal/storage/icon.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212
"miniflux.app/v2/internal/model"
1313
)
1414

15-
// HasFeedIcon checks if the given feed has an icon.
15+
// HasFeedIcon reports whether the specified feed already has an associated icon record.
1616
func (s *Storage) HasFeedIcon(feedID int64) bool {
1717
var result bool
1818
query := `SELECT true FROM feed_icons WHERE feed_id=$1 LIMIT 1`
1919
s.db.QueryRow(query, feedID).Scan(&result)
2020
return result
2121
}
2222

23-
// IconByID returns an icon by the ID.
23+
// IconByID fetches a single icon by its internal identifier, returning nil when it is not found.
2424
func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
2525
var icon model.Icon
2626
query := `
@@ -33,16 +33,17 @@ func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
3333
FROM icons
3434
WHERE id=$1`
3535
err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
36-
if err == sql.ErrNoRows {
36+
switch {
37+
case err == sql.ErrNoRows:
3738
return nil, nil
38-
} else if err != nil {
39-
return nil, fmt.Errorf("store: unable to fetch icon #%d: %w", iconID, err)
39+
case err != nil:
40+
return nil, fmt.Errorf("store: cannot load icon id=%d: %w", iconID, err)
41+
default:
42+
return &icon, nil
4043
}
41-
42-
return &icon, nil
4344
}
4445

45-
// IconByExternalID returns an icon by the External Icon ID.
46+
// IconByExternalID fetches an icon using its external identifier, returning nil when no match exists.
4647
func (s *Storage) IconByExternalID(externalIconID string) (*model.Icon, error) {
4748
var icon model.Icon
4849
query := `
@@ -56,16 +57,17 @@ func (s *Storage) IconByExternalID(externalIconID string) (*model.Icon, error) {
5657
WHERE external_id=$1
5758
`
5859
err := s.db.QueryRow(query, externalIconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
59-
if err == sql.ErrNoRows {
60+
switch {
61+
case err == sql.ErrNoRows:
6062
return nil, nil
61-
} else if err != nil {
62-
return nil, fmt.Errorf("store: unable to fetch icon %s: %w", externalIconID, err)
63+
case err != nil:
64+
return nil, fmt.Errorf("store: cannot load icon external_id=%s: %w", externalIconID, err)
65+
default:
66+
return &icon, nil
6367
}
64-
65-
return &icon, nil
6668
}
6769

68-
// IconByFeedID returns a feed icon.
70+
// IconByFeedID returns the icon linked to the given feed for the specified user, or nil if none is set.
6971
func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
7072
query := `
7173
SELECT
@@ -83,14 +85,17 @@ func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
8385
`
8486
var icon model.Icon
8587
err := s.db.QueryRow(query, userID, feedID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
86-
if err != nil {
87-
return nil, fmt.Errorf(`store: unable to fetch icon: %v`, err)
88+
switch {
89+
case err == sql.ErrNoRows:
90+
return nil, nil
91+
case err != nil:
92+
return nil, fmt.Errorf("store: cannot load icon for feed_id=%d user_id=%d: %w", feedID, userID, err)
93+
default:
94+
return &icon, nil
8895
}
89-
90-
return &icon, nil
9196
}
9297

93-
// StoreFeedIcon creates or updates a feed icon.
98+
// StoreFeedIcon creates or reuses an icon by hash and associates it with the given feed atomically.
9499
func (s *Storage) StoreFeedIcon(feedID int64, icon *model.Icon) error {
95100
tx, err := s.db.Begin()
96101
if err != nil {
@@ -140,7 +145,7 @@ func (s *Storage) StoreFeedIcon(feedID int64, icon *model.Icon) error {
140145
return nil
141146
}
142147

143-
// Icons returns all icons that belongs to a user.
148+
// Icons lists all icons currently associated with any feed owned by the given user.
144149
func (s *Storage) Icons(userID int64) (model.Icons, error) {
145150
query := `
146151
SELECT

0 commit comments

Comments
 (0)