2015-05-10 21:55:21 +00:00
|
|
|
package news
|
|
|
|
|
|
|
|
import (
|
2015-09-13 21:57:22 +00:00
|
|
|
"database/sql"
|
2015-05-10 21:55:21 +00:00
|
|
|
"fmt"
|
2015-05-13 06:21:06 +00:00
|
|
|
"github.com/dballard/transmet/categories"
|
2015-09-13 21:57:22 +00:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"time"
|
2015-09-20 14:51:36 +00:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2015-05-10 21:55:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type News struct {
|
2015-09-13 21:57:22 +00:00
|
|
|
id int
|
|
|
|
Url string
|
|
|
|
Title string
|
|
|
|
Category_id int
|
|
|
|
Date time.Time
|
|
|
|
Notes string
|
2015-05-10 21:55:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 14:51:36 +00:00
|
|
|
const (
|
2015-11-11 17:28:07 +00:00
|
|
|
SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes"
|
2015-09-20 14:51:36 +00:00
|
|
|
)
|
|
|
|
|
2015-07-14 15:10:22 +00:00
|
|
|
/* Storage Node containing:
|
|
|
|
* Name - categry name
|
|
|
|
* News children
|
|
|
|
* Category - category of this node
|
|
|
|
* Children - sub containers: mapped to sub categories
|
2015-09-13 21:57:22 +00:00
|
|
|
*/
|
2015-05-12 15:32:47 +00:00
|
|
|
type NewsContainer struct {
|
2015-09-13 21:57:22 +00:00
|
|
|
Name string
|
|
|
|
News []News
|
|
|
|
Category *categories.Category
|
|
|
|
Children map[int]*NewsContainer
|
2015-05-12 15:32:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 03:22:12 +00:00
|
|
|
/*func New(id int) *News {
|
2015-09-21 15:26:31 +00:00
|
|
|
news := &News{}
|
|
|
|
news.id = id
|
|
|
|
return news
|
2015-09-24 03:22:12 +00:00
|
|
|
}*/
|
2015-09-21 15:26:31 +00:00
|
|
|
|
2015-07-14 15:10:22 +00:00
|
|
|
// Insert News item into DB
|
2015-05-10 21:55:21 +00:00
|
|
|
func (news *News) Insert(db *sql.DB) error {
|
2015-09-13 21:57:22 +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)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error inserting news: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-05-12 05:50:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 04:12:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-09-05 17:49:29 +00:00
|
|
|
func Delete(db *sql.DB, id int) error {
|
2015-09-13 21:57:22 +00:00
|
|
|
_, err := db.Exec("DELETE FROM news WHERE id = $1", id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error deleting news: ", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-05 17:49:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 14:51:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:26:31 +00:00
|
|
|
// 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
|
|
|
|
|
2015-11-11 17:28:07 +00:00
|
|
|
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news order by timestamp DESC")
|
2015-09-21 15:26:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-13 06:21:06 +00:00
|
|
|
func nullStringToString(str *sql.NullString) string {
|
2015-09-13 21:57:22 +00:00
|
|
|
if str.Valid {
|
|
|
|
return str.String
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2015-05-13 06:21:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 14:51:36 +00:00
|
|
|
func nullBoolToBool(b *sql.NullBool) bool {
|
|
|
|
if b.Valid {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 15:10:22 +00:00
|
|
|
// Init and add a news container to the Data Structs
|
2015-05-13 15:26:30 +00:00
|
|
|
func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
|
2015-09-13 21:57:22 +00:00
|
|
|
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 {
|
2015-05-13 15:26:30 +00:00
|
|
|
if _, ok := flat[int(parent.Int64)]; !ok {
|
2015-09-13 21:57:22 +00:00
|
|
|
addContainer(int(parent.Int64), flat, tree)
|
2015-05-13 15:26:30 +00:00
|
|
|
}
|
|
|
|
flat[int(parent.Int64)].Children[category_id] = container
|
2015-09-13 21:57:22 +00:00
|
|
|
} else {
|
|
|
|
tree[category_id] = container
|
|
|
|
}
|
2015-05-13 15:26:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 14:51:36 +00:00
|
|
|
func scanNews(rows *sql.Rows) (*News, error) {
|
|
|
|
news := &News{}
|
|
|
|
var url, title, notes sql.NullString
|
|
|
|
var category_id sql.NullInt64
|
2015-11-11 17:28:07 +00:00
|
|
|
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, ¬es)
|
2015-09-20 14:51:36 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-31 03:48:55 +00:00
|
|
|
func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
|
|
|
|
news := []*News{}
|
2015-08-17 14:16:54 +00:00
|
|
|
count := 0
|
|
|
|
for rows.Next() {
|
2015-09-20 14:51:36 +00:00
|
|
|
newsItem, err := scanNews(rows)
|
|
|
|
|
2015-09-13 21:57:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
news = append(news, newsItem)
|
|
|
|
count++
|
2015-08-17 14:16:54 +00:00
|
|
|
}
|
2015-09-13 21:57:22 +00:00
|
|
|
|
2015-08-17 14:16:54 +00:00
|
|
|
return news, count, nil
|
|
|
|
}
|
2015-09-13 21:57:22 +00:00
|
|
|
|
2015-07-21 14:10:57 +00:00
|
|
|
func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, error) {
|
2015-05-12 15:32:47 +00:00
|
|
|
newsTree := map[int]*NewsContainer{}
|
|
|
|
newsFlat := map[int]*NewsContainer{}
|
2015-05-16 19:48:42 +00:00
|
|
|
count := 0
|
2015-05-12 15:32:47 +00:00
|
|
|
for rows.Next() {
|
2015-09-20 14:51:36 +00:00
|
|
|
news, err := scanNews(rows)
|
|
|
|
|
2015-09-13 21:57:22 +00:00
|
|
|
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]
|
2015-09-20 14:51:36 +00:00
|
|
|
container.News = append(container.News, *news)
|
2015-09-13 21:57:22 +00:00
|
|
|
count++
|
2015-05-12 15:32:47 +00:00
|
|
|
}
|
2015-09-13 21:57:22 +00:00
|
|
|
|
2015-05-16 19:48:42 +00:00
|
|
|
return newsTree, count, nil
|
2015-05-13 15:26:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 03:48:55 +00:00
|
|
|
|