feat: inserts last 10 articles into DB on feed add
This commit is contained in:
parent
0fc94606d6
commit
13ba132159
5 changed files with 84 additions and 23 deletions
|
|
@ -18,7 +18,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
feed "freed/internal"
|
"freed/internal"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -35,7 +35,7 @@ Supported types currently are:
|
||||||
- RSS
|
- RSS
|
||||||
- Youtube Channel links`,
|
- Youtube Channel links`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if err := feed.Add(args[0]); err != nil {
|
if err := internal.AddFeed(args[0]); err != nil {
|
||||||
// TODO: Prettify this/all errors
|
// TODO: Prettify this/all errors
|
||||||
fmt.Printf("Error adding feed: %v\n", err)
|
fmt.Printf("Error adding feed: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
61
internal/database/article.go
Normal file
61
internal/database/article.go
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
Name string
|
||||||
|
Url string
|
||||||
|
ReadAt *time.Time
|
||||||
|
FeedId int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Article) Insert() (int64, error) {
|
||||||
|
result, err := db.Exec("INSERT INTO article (name, url, readAt, feedId) VALUES (?, ?, ?, ?)", a.Name, a.Url, a.ReadAt, a.FeedId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := result.LastInsertId()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InsertMultipleArticles(articles []Article) error {
|
||||||
|
if len(articles) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := db.Begin()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
stmt, err := tx.Prepare("INSERT INTO article (name, url, readAt, feedId) VALUES (?, ?, ?, ?)")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
for _, article := range articles {
|
||||||
|
fmt.Printf("article %+v", article)
|
||||||
|
_, err := stmt.Exec(article.Name, article.Url, article.ReadAt, article.FeedId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ CREATE table article (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name text NOT NULL,
|
name text NOT NULL,
|
||||||
url text NOT NULL UNIQUE,
|
url text NOT NULL UNIQUE,
|
||||||
read INTEGER DEFAULT 0,
|
|
||||||
readAt text,
|
readAt text,
|
||||||
feedId INTEGER,
|
feedId INTEGER,
|
||||||
FOREIGN KEY (feedId) REFERENCES feed(id)
|
FOREIGN KEY (feedId) REFERENCES feed(id)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package feed
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/mmcdole/gofeed"
|
"github.com/mmcdole/gofeed"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Add(feedUrl string) error {
|
func AddFeed(feedUrl string) error {
|
||||||
if _, err := url.ParseRequestURI(feedUrl); err != nil {
|
if _, err := url.ParseRequestURI(feedUrl); err != nil {
|
||||||
return fmt.Errorf("The given URL does not seem to be valid: %s", err)
|
return fmt.Errorf("The given URL does not seem to be valid: %s", err)
|
||||||
}
|
}
|
||||||
|
|
@ -19,27 +19,39 @@ func Add(feedUrl string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("feed: %+v", feed)
|
|
||||||
|
|
||||||
// TODO: I have everything here (feed), might as well store it right away
|
|
||||||
// So, implement a loop to do that here
|
|
||||||
|
|
||||||
f := database.Feed{
|
f := database.Feed{
|
||||||
Name: feed.Title,
|
Name: feed.Title,
|
||||||
Url: feedUrl,
|
Url: feedUrl,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.Insert(); err != nil {
|
feedId, err := f.Insert()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
articles := make([]database.Article, 0, 10)
|
||||||
|
|
||||||
|
for i, v := range feed.Items {
|
||||||
|
if i < 10 {
|
||||||
|
a := database.Article{
|
||||||
|
Name: v.Title,
|
||||||
|
Url: v.Link,
|
||||||
|
FeedId: feedId,
|
||||||
|
}
|
||||||
|
|
||||||
|
articles = append(articles, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
database.InsertMultipleArticles(articles)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseByUrl(u string) (*gofeed.Feed, error) {
|
func parseByUrl(u string) (*gofeed.Feed, error) {
|
||||||
fp := gofeed.NewParser()
|
fp := gofeed.NewParser()
|
||||||
feed, err := fp.ParseURL(u)
|
|
||||||
|
|
||||||
|
feed, err := fp.ParseURL(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
package feed
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
|
||||||
gotErr := Add("gibberish")
|
|
||||||
|
|
||||||
if gotErr == nil {
|
|
||||||
t.Errorf("Expected Add function to throw an error if an invalid URL is provided")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue