From fad9ad2be4fc800f8710e2a498cc8f536af8827c Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 17 Nov 2019 19:44:12 -0800 Subject: Display list of feeds per category --- storage/feed.go | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'storage') diff --git a/storage/feed.go b/storage/feed.go index f6ad021..6db3488 100644 --- a/storage/feed.go +++ b/storage/feed.go @@ -139,7 +139,6 @@ func (s *Storage) Feeds(userID int64) (model.Feeds, error) { // FeedsWithCounters returns all feeds of the given user with counters of read and unread entries. func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) { - feeds := make(model.Feeds, 0) query := ` SELECT f.id, @@ -166,7 +165,43 @@ func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) { f.user_id=$1 ORDER BY f.parsing_error_count DESC, unread_count DESC, lower(f.title) ASC ` - rows, err := s.db.Query(query, userID) + return s.fetchFeedsWithCounters(query, userID) +} + +// FeedsByCategoryWithCounters returns all feeds of the given user/category with counters of read and unread entries. +func (s *Storage) FeedsByCategoryWithCounters(userID, categoryID int64) (model.Feeds, error) { + query := ` + SELECT + f.id, + f.feed_url, + f.site_url, + f.title, + f.etag_header, + f.last_modified_header, + f.user_id, + f.checked_at at time zone u.timezone, + f.parsing_error_count, f.parsing_error_msg, + f.scraper_rules, f.rewrite_rules, f.crawler, f.user_agent, + f.username, f.password, f.disabled, + f.category_id, c.title as category_title, + fi.icon_id, + u.timezone, + (SELECT count(*) FROM entries WHERE entries.feed_id=f.id AND status='unread') as unread_count, + (SELECT count(*) FROM entries WHERE entries.feed_id=f.id AND status='read') as read_count + FROM feeds f + LEFT JOIN categories c ON c.id=f.category_id + LEFT JOIN feed_icons fi ON fi.feed_id=f.id + LEFT JOIN users u ON u.id=f.user_id + WHERE + f.user_id=$1 AND f.category_id=$2 + ORDER BY f.parsing_error_count DESC, unread_count DESC, lower(f.title) ASC + ` + return s.fetchFeedsWithCounters(query, userID, categoryID) +} + +func (s *Storage) fetchFeedsWithCounters(query string, args ...interface{}) (model.Feeds, error) { + feeds := make(model.Feeds, 0) + rows, err := s.db.Query(query, args...) if err != nil { return nil, fmt.Errorf(`store: unable to fetch feeds: %v`, err) } @@ -176,7 +211,7 @@ func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) { var feed model.Feed var iconID interface{} var tz string - feed.Category = &model.Category{UserID: userID} + feed.Category = &model.Category{} err := rows.Scan( &feed.ID, @@ -213,6 +248,7 @@ func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) { } feed.CheckedAt = timezone.Convert(tz, feed.CheckedAt) + feed.Category.UserID = feed.UserID feeds = append(feeds, &feed) } -- cgit v1.2.3