implements list command
This commit is contained in:
parent
b54f62378b
commit
09f4b2a838
5 changed files with 127 additions and 5 deletions
|
|
@ -1,16 +1,24 @@
|
|||
package database
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type FeedType string
|
||||
|
||||
type Feed struct {
|
||||
ID int64
|
||||
Name string
|
||||
Url string
|
||||
AddedAt *time.Time
|
||||
CreatedAt *time.Time
|
||||
LastSyncedAt *time.Time
|
||||
}
|
||||
|
||||
type FeedWithArticleCount struct {
|
||||
Feed
|
||||
ArticleCount int
|
||||
}
|
||||
|
||||
func (f Feed) Insert() (int64, error) {
|
||||
result, err := db.Exec("INSERT INTO feed (name, url) VALUES (?,?)", f.Name, f.Url)
|
||||
|
||||
|
|
@ -26,3 +34,28 @@ func (f Feed) Insert() (int64, error) {
|
|||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func FindAllFeedsWithArticleCount() (*[]FeedWithArticleCount, error) {
|
||||
rows, err := db.Query("SELECT f.id, f.name, f.url, f.createdAt, f.lastSyncedAt, COUNT(a.id) FROM feed as f LEFT JOIN article as a ON a.feedId = f.id;")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var feeds []FeedWithArticleCount
|
||||
|
||||
for rows.Next() {
|
||||
var feed FeedWithArticleCount
|
||||
err := rows.Scan(&feed.ID, &feed.Name, &feed.Url, &feed.CreatedAt, &feed.LastSyncedAt, &feed.ArticleCount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
feeds = append(feeds, feed)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &feeds, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import (
|
|||
"fmt"
|
||||
"freed/internal/database"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
func AddFeed(feedUrl string) (string, int, error) {
|
||||
|
|
@ -50,6 +52,34 @@ func AddFeed(feedUrl string) (string, int, error) {
|
|||
return feed.Title, len(articles), nil
|
||||
}
|
||||
|
||||
func GetAllFeedsAsTable() (pterm.TableData, error) {
|
||||
feeds, err := database.FindAllFeedsWithArticleCount()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tableData := pterm.TableData{}
|
||||
headerRow := []string{"ID", "Name", "Url", "Item Count", "Created At", "Last Synced At"}
|
||||
|
||||
tableData = append(tableData, headerRow)
|
||||
|
||||
for _, feed := range *feeds {
|
||||
rowData := []string{
|
||||
fmt.Sprintf("[%d]", feed.ID),
|
||||
feed.Name,
|
||||
feed.Url,
|
||||
strconv.Itoa(feed.ArticleCount),
|
||||
pterm.Gray(feed.CreatedAt),
|
||||
pterm.Gray(feed.LastSyncedAt),
|
||||
}
|
||||
|
||||
tableData = append(tableData, rowData)
|
||||
}
|
||||
|
||||
return tableData, nil
|
||||
}
|
||||
|
||||
func parseByUrl(u string) (*gofeed.Feed, error) {
|
||||
fp := gofeed.NewParser()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue