2015-05-10 21:55:21 +00:00
|
|
|
package news
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type News struct {
|
|
|
|
news_id int
|
|
|
|
Url string
|
|
|
|
Title string
|
2015-05-12 05:06:28 +00:00
|
|
|
Category_id int
|
2015-05-10 21:55:21 +00:00
|
|
|
Date time.Time
|
2015-05-12 05:06:28 +00:00
|
|
|
Notes string
|
2015-05-12 05:50:09 +00:00
|
|
|
Expoerted bool
|
2015-05-10 21:55:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:32:47 +00:00
|
|
|
type NewsContainer struct {
|
|
|
|
Name string
|
|
|
|
News []News
|
|
|
|
Children map[int]*NewsContainer
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:55:21 +00:00
|
|
|
func (news *News) Insert(db *sql.DB) error {
|
2015-05-12 05:06:28 +00: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 );
|
2015-05-10 21:55:21 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error inserting news: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-05-12 05:50:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:32:47 +00:00
|
|
|
func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
|
|
|
|
rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false")
|
2015-05-12 05:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("DB errpr reading unexported news: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-12 15:32:47 +00:00
|
|
|
newsTree := map[int]*NewsContainer{}
|
|
|
|
newsFlat := map[int]*NewsContainer{}
|
2015-05-12 05:50:09 +00:00
|
|
|
|
2015-05-12 15:32:47 +00:00
|
|
|
for rows.Next() {
|
|
|
|
news := News{}
|
|
|
|
var url sql.NullString
|
|
|
|
err := rows.Scan(&news.Url, &news.Title, &news.Category_id, &news.Date, &news.Notes)
|
|
|
|
}
|
2015-05-12 05:50:09 +00:00
|
|
|
|
|
|
|
|
2015-05-12 15:32:47 +00:00
|
|
|
return newsTree, nil
|
2015-05-10 21:55:21 +00:00
|
|
|
}
|