package news import ( "database/sql" "fmt" "github.com/dballard/transmet/categories" _ "github.com/lib/pq" "time" "errors" "strconv" ) type News struct { id int Url string Title string Category_id int Date time.Time Notes string Expoerted bool } const ( SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes, exported" ) /* Storage Node containing: * Name - categry name * News children * Category - category of this node * Children - sub containers: mapped to sub categories */ type NewsContainer struct { Name string News []News Category *categories.Category Children map[int]*NewsContainer } /*func New(id int) *News { news := &News{} news.id = id return news }*/ // Insert News item into DB func (news *News) Insert(db *sql.DB) error { _, 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 } // Update updatable parts of news itemn in DB func (news *News) Update(db *sql.DB) error { _, err := db.Exec("UPDATE news SET title=$1, url=$2, category_id=$3, notes=$4 WHERE id=$5", news.Title, news.Url, news.Category_id, news.Notes, news.id) if err != nil { fmt.Println("Error updating news: ", err) } return err } func Delete(db *sql.DB, id int) error { _, err := db.Exec("DELETE FROM news WHERE id = $1", id) if err != nil { fmt.Println("Error deleting news: ", err) return err } return nil } func Get(db *sql.DB, id int) (*News, error) { rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news WHERE id=$1", id) if err != nil { fmt.Println("Error getting news (", id, "): ", err) return nil, err } defer rows.Close() var news *News if rows.Next() { news, err = scanNews(rows) if err != nil { return nil, err } } return news, err } // Load and return in NewsContainer format all the unexported news items func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) { categories.LoadCategories(db) // required by addContainer rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by timestamp DESC") if err != nil { fmt.Println("DB errpr reading LoadPage news: ", err) return nil, 0, err } defer rows.Close() return convertSqlToNews(rows) } // Load and return in NewsContainer format all the unexported news items func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { categories.LoadCategories(db) // required by addContainer rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by category_id ASC") if err != nil { fmt.Println("DB errpr reading unexported news: ", err) return nil, 0, err } defer rows.Close() return convertSqlToNewsContainer(rows) } // Helper fn - formating - math func (this *NewsContainer) HeaderDepth(start int) int { return start + this.Category.Depth() } // 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 } func (news *News) Id() int { return news.id } func nullStringToString(str *sql.NullString) string { if str.Valid { return str.String } else { return "" } } func nullBoolToBool(b *sql.NullBool) bool { if b.Valid { return true } else { return false } } // Init and add a news container to the Data Structs 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 } } func scanNews(rows *sql.Rows) (*News, error) { news := &News{} var url, title, notes sql.NullString var category_id sql.NullInt64 var exported sql.NullBool err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, ¬es, &exported) if err != nil { fmt.Println("Error reading news from DB: " + err.Error()) return nil, err } news.Url = nullStringToString(&url) news.Title = nullStringToString(&title) news.Notes = nullStringToString(¬es) news.Expoerted = nullBoolToBool(&exported) if category_id.Valid { news.Category_id = int(category_id.Int64) } else { return nil, errors.New("Error reading news item from DB, no category_id for " + strconv.Itoa(news.Id())) } return news, nil } func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) { news := []*News{} count := 0 for rows.Next() { newsItem, err := scanNews(rows) if err != nil { return nil, 0, err } news = append(news, newsItem) count++ } return news, count, nil } func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, error) { newsTree := map[int]*NewsContainer{} newsFlat := map[int]*NewsContainer{} count := 0 for rows.Next() { news, err := scanNews(rows) if err != nil { return nil, 0, err } if _, ok := newsFlat[news.Category_id]; !ok { addContainer(news.Category_id, newsFlat, newsTree) } container := newsFlat[news.Category_id] container.News = append(container.News, *news) count++ } return newsTree, count, nil }