transmet/news/news.go

52 lines
1.1 KiB
Go

package news
import (
"time"
"database/sql"
_ "github.com/lib/pq"
"fmt"
)
type News struct {
news_id int
Url string
Title string
Category_id int
Date time.Time
Notes string
Expoerted bool
}
type NewsContainer struct {
Name string
News []News
Children map[int]*NewsContainer
}
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
}
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")
if err != nil {
fmt.Println("DB errpr reading unexported news: ", err)
return nil, err
}
newsTree := map[int]*NewsContainer{}
newsFlat := map[int]*NewsContainer{}
for rows.Next() {
news := News{}
var url sql.NullString
err := rows.Scan(&news.Url, &news.Title, &news.Category_id, &news.Date, &news.Notes)
}
return newsTree, nil
}