From b376d197b51f2e67c314e23b8ac484dc2ae8f4a5 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Sun, 22 Apr 2018 16:31:19 -0700 Subject: wip single category on feed --- models/feed/feed.go | 54 ++++++++++++++++++++++++++++++++++++++++++----------- models/item/item.go | 22 ++++++++++++++-------- 2 files changed, 57 insertions(+), 19 deletions(-) (limited to 'models') diff --git a/models/feed/feed.go b/models/feed/feed.go index 3d2473b..17f344f 100644 --- a/models/feed/feed.go +++ b/models/feed/feed.go @@ -8,16 +8,21 @@ import ( ) type Feed struct { - Id int64 `json:"_id" xml:"-"` - Url string `json:"url" xml:"xmlUrl,attr"` - WebUrl string `json:"web_url" xml:"htmlUrl,attr"` - Title string `json:"title" xml:"text,attr"` + Id int64 `json:"_id" xml:"-"` + Url string `json:"url" xml:"xmlUrl,attr"` + WebUrl string `json:"web_url" xml:"htmlUrl,attr"` + Title string `json:"title" xml:"text,attr"` + Category string `json:"category"` // for OPML output purposes XMLName string `json:"-" xml:"outline"` Type string `json:"-" xml:"type,attr"` } +type Category struct { + Title string `json:"title"` +} + func NewFeed(url string) error { url = ResolveFeedURL(url) stmt, err := models.DB.Prepare("INSERT INTO feed(url) VALUES(?)") @@ -37,7 +42,8 @@ func All() ([]*Feed, error) { func filter(where string) ([]*Feed, error) { // todo: add back in title - rows, err := models.DB.Query(`SELECT id, url, web_url, title + rows, err := models.DB.Query(`SELECT + id, url, web_url, title, category FROM feed ` + where) if err != nil { return nil, err @@ -47,7 +53,7 @@ func filter(where string) ([]*Feed, error) { feeds := make([]*Feed, 0) for rows.Next() { f := new(Feed) - err := rows.Scan(&f.Id, &f.Url, &f.WebUrl, &f.Title) + err := rows.Scan(&f.Id, &f.Url, &f.WebUrl, &f.Title, &f.Category) f.Type = "rss" if err != nil { return nil, err @@ -74,23 +80,23 @@ func (f *Feed) Update() { } models.DB.Query(`UPDATE feed - SET title=?, url=?, web_url=? - WHERE id=?`, f.Title, f.Url, f.WebUrl, f.Id) + SET title=?, url=?, web_url=?, category=? + WHERE id=?`, f.Title, f.Url, f.WebUrl, f.Category, f.Id) } func (f *Feed) Delete() { log.Println("lets delete some shiteeee") _, err := models.DB.Exec(`DELETE FROM feed - WHERE id=?`, f.Id) + WHERE id=?`, f.Id) if err != nil { log.Println(err) } } func (f *Feed) ByUrl(url string) error { - err := models.DB.QueryRow(`SELECT id, url, title + err := models.DB.QueryRow(`SELECT id, url, title, category FROM feed - WHERE url = ?`, url).Scan(&f.Id, &f.Url, &f.Title) + WHERE url = ?`, url).Scan(&f.Id, &f.Url, &f.Title, &f.Category) if err != nil { return err } @@ -167,3 +173,29 @@ func ResolveFeedURL(url string) string { } return f } + +func Categories() ([]*Category, error) { + rows, err := models.DB.Query(`SELECT + DISTINCT category + FROM feed + WHERE category!="" + ORDER BY lower(category) ASC`) + if err != nil { + return nil, err + } + defer rows.Close() + + categories := make([]*Category, 0) + for rows.Next() { + c := new(Category) + err := rows.Scan(&c.Title) + if err != nil { + return nil, err + } + categories = append(categories, c) + } + if err = rows.Err(); err != nil { + return nil, err + } + return categories, nil +} diff --git a/models/item/item.go b/models/item/item.go index c5ffff9..b3dc5eb 100644 --- a/models/item/item.go +++ b/models/item/item.go @@ -19,9 +19,10 @@ type Item struct { Description string `json:"description"` PublishDate string `json:"publish_date"` - FeedId int64 - FeedTitle string `json:"feed_title"` - FeedUrl string `json:"feed_url"` + FeedId int64 + FeedTitle string `json:"feed_title"` + FeedUrl string `json:"feed_url"` + FeedCategory string `json:"feed_category"` ReadState bool `json:"read"` Starred bool `json:"starred"` @@ -112,16 +113,16 @@ func (i *Item) GetFullContent() { } } -func Filter(max_id int64, feed_id int64, unread_only bool, starred_only bool) ([]*Item, error) { +func Filter(max_id int64, feed_id int64, category string, unread_only bool, starred_only bool) ([]*Item, error) { var args []interface{} query := `SELECT item.id, item.title, item.url, item.description, item.read_state, item.starred, item.publish_date, item.full_content, item.header_image, - feed.url, feed.title - FROM item,feed - WHERE item.feed_id=feed.id ` + feed.url, feed.title, feed.category + FROM item,feed + WHERE item.feed_id=feed.id ` if max_id != 0 { query = query + "AND item.id < ? " @@ -133,6 +134,11 @@ func Filter(max_id int64, feed_id int64, unread_only bool, starred_only bool) ([ args = append(args, feed_id) } + if category != "" { + query = query + " AND feed.category=? " + args = append(args, category) + } + if unread_only { query = query + " AND item.read_state=0 " } @@ -157,7 +163,7 @@ func Filter(max_id int64, feed_id int64, unread_only bool, starred_only bool) ([ items := make([]*Item, 0) for rows.Next() { i := new(Item) - err := rows.Scan(&i.Id, &i.Title, &i.Url, &i.Description, &i.ReadState, &i.Starred, &i.PublishDate, &i.FullContent, &i.HeaderImage, &i.FeedUrl, &i.FeedTitle) + err := rows.Scan(&i.Id, &i.Title, &i.Url, &i.Description, &i.ReadState, &i.Starred, &i.PublishDate, &i.FullContent, &i.HeaderImage, &i.FeedUrl, &i.FeedTitle, &i.FeedCategory) if err != nil { log.Println(err) return nil, err -- cgit v1.2.3