work on news loading
This commit is contained in:
parent
277718e907
commit
4d05a266d1
56
news/news.go
56
news/news.go
|
@ -5,6 +5,8 @@ import (
|
|||
"database/sql"
|
||||
_ "github.com/lib/pq"
|
||||
"fmt"
|
||||
"github.com/dballard/transmet/categories"
|
||||
|
||||
)
|
||||
|
||||
type News struct {
|
||||
|
@ -32,8 +34,18 @@ func (news *News) Insert(db *sql.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func nullStringToString(str *sql.NullString) string {
|
||||
if str.Valid {
|
||||
return str.String
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
categories.LoadCategories(db)
|
||||
|
||||
rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false order by category_id ASC")
|
||||
if err != nil {
|
||||
fmt.Println("DB errpr reading unexported news: ", err)
|
||||
return nil, err
|
||||
|
@ -43,10 +55,48 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
|
|||
|
||||
for rows.Next() {
|
||||
news := News{}
|
||||
var url sql.NullString
|
||||
err := rows.Scan(&news.Url, &news.Title, &news.Category_id, &news.Date, &news.Notes)
|
||||
var url, title, notes sql.NullString
|
||||
var date, category_id sql.NullInt64
|
||||
err := rows.Scan(&url, &title, &category_id, &news.Date, ¬es)
|
||||
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 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)
|
||||
}
|
||||
|
||||
var container *NewsContainer
|
||||
var ok bool
|
||||
fmt.Println("cid: ", cid)
|
||||
if container, ok = newsFlat[cid]; !ok {
|
||||
fmt.Println("ADDING")
|
||||
// need new container for new cid
|
||||
container = &NewsContainer{ Name: categories.CategoriesFlat[cid].Name, News: []News{}, Children: map[int]*NewsContainer{} }
|
||||
newsFlat[cid] = container
|
||||
parent := categories.CategoriesFlat[cid].Parent
|
||||
if parent.Valid {
|
||||
fmt.Println("parent: ", parent.Int64)
|
||||
newsFlat[int(parent.Int64)].Children[cid] = container
|
||||
} else {
|
||||
fmt.Println("no parent")
|
||||
newsTree[cid] = container
|
||||
}
|
||||
}
|
||||
container.News = append(container.News, news)
|
||||
}
|
||||
|
||||
return newsTree, nil
|
||||
}
|
|
@ -190,7 +190,13 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
|
|||
session, _ := store.Get(r, "c_user")
|
||||
flashes := GetFlashes(session)
|
||||
session.Save(r, w)
|
||||
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes})
|
||||
|
||||
news, err := news.Unexported(db)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news})
|
||||
}
|
||||
|
||||
func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||
|
|
Loading…
Reference in New Issue