transmet/news/news.go

121 lines
3.2 KiB
Go
Raw Normal View History

package news
import (
"time"
"database/sql"
_ "github.com/lib/pq"
"fmt"
2015-05-13 08:21:06 +02:00
"github.com/dballard/transmet/categories"
)
type News struct {
news_id int
Url string
Title string
2015-05-12 07:06:28 +02:00
Category_id int
Date time.Time
2015-05-12 07:06:28 +02:00
Notes string
2015-05-12 07:50:09 +02:00
Expoerted bool
}
type NewsContainer struct {
Name string
News []News
2015-05-13 17:26:30 +02:00
Category *categories.Category
Children map[int]*NewsContainer
}
func (news *News) Insert(db *sql.DB) error {
2015-05-12 07:06:28 +02:00
_, err := db.Exec("INSERT INTO news (url, title, category_id, notes) VALUES($1, $2, $3, $4)", news.Url, news.Title, news.Category_id, news.Notes );
if err != nil {
fmt.Println("Error inserting news: ", err)
return err
}
return nil
2015-05-12 07:50:09 +02:00
}
2015-05-13 08:21:06 +02:00
func nullStringToString(str *sql.NullString) string {
if str.Valid {
return str.String
} else {
return ""
}
}
2015-05-13 17:26:30 +02:00
func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
container := &NewsContainer{ Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{} }
flat[category_id] = container
parent := categories.CategoriesFlat[category_id].Parent
if parent.Valid {
if _, ok := flat[int(parent.Int64)]; !ok {
addContainer(int(parent.Int64), flat, tree)
}
flat[int(parent.Int64)].Children[category_id] = container
} else {
tree[category_id] = container
}
}
2015-05-16 21:48:42 +02:00
// returns a tree of news items, the total count, and error
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
2015-05-13 08:21:06 +02:00
categories.LoadCategories(db)
2015-05-14 17:29:01 +02:00
rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported is null order by category_id ASC")
2015-05-12 07:50:09 +02:00
if err != nil {
fmt.Println("DB errpr reading unexported news: ", err)
2015-05-16 21:48:42 +02:00
return nil, 0, err
2015-05-12 07:50:09 +02:00
}
newsTree := map[int]*NewsContainer{}
newsFlat := map[int]*NewsContainer{}
2015-05-16 21:48:42 +02:00
count := 0
for rows.Next() {
news := News{}
2015-05-13 08:21:06 +02:00
var url, title, notes sql.NullString
var date, category_id sql.NullInt64
err := rows.Scan(&url, &title, &category_id, &news.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
2015-05-16 21:48:42 +02:00
return nil, 0, err
2015-05-13 08:21:06 +02:00
}
news.Url = nullStringToString(&url)
news.Title = nullStringToString(&title)
news.Notes = nullStringToString(&notes)
if date.Valid {
news.Date = time.Unix(date.Int64, 0)
} else {
news.Date = time.Now()
}
cid := 1
if category_id.Valid {
cid = int(category_id.Int64)
}
2015-05-13 17:26:30 +02:00
if _, ok := newsFlat[cid]; !ok {
addContainer(cid, newsFlat, newsTree)
2015-05-13 08:21:06 +02:00
}
2015-05-13 17:26:30 +02:00
container := newsFlat[cid]
2015-05-13 08:21:06 +02:00
container.News = append(container.News, news)
2015-05-16 21:48:42 +02:00
count++
}
2015-05-12 07:50:09 +02:00
2015-05-16 21:48:42 +02:00
return newsTree, count, nil
2015-05-13 17:26:30 +02:00
}
func (this *NewsContainer) HeaderDepth(start int) int {
return start + this.Category.Depth()
2015-05-15 16:26:50 +02:00
}
// Mark the current batch (news.exported is null) as exported in this batch (exported = now())
func MarkExported(db *sql.DB) error {
now := time.Now()
_, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now)
if err != nil {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
}