diff --git a/news/news.go b/news/news.go index 842c480..d13dde0 100644 --- a/news/news.go +++ b/news/news.go @@ -66,11 +66,17 @@ func addContainer(category_id int, flat, tree map[int]*NewsContainer) { } // Load and return in NewsContainer format all the unexported news items -func Unexported(db *sql.DB, offset, amount int) (map[int]*NewsContainer, int, error) { +func LoadPage(db *sql.DB, offset, amount int) (map[int]*NewsContainer, int, error) { categories.LoadCategories(db) // required by addContainer rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported is null order by category_id ASC") - + if err != nil { + fmt.Println("DB errpr reading LoadPage news: ", err) + return nil, 0, err + } + defer rows.Close() + + return convertSqlToNewsContainer(rows) } // Load and return in NewsContainer format all the unexported news items @@ -83,6 +89,11 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { return nil, 0, err } defer rows.Close() + + return convertSqlToNewsContainer(rows) +} + +func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, error) { newsTree := map[int]*NewsContainer{} newsFlat := map[int]*NewsContainer{} count := 0 @@ -103,12 +114,14 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { if date.Valid { news.Date = time.Unix(date.Int64, 0) } else { - news.Date = time.Now() + news.Date = time.Now() } - cid := 1 + var cid int if category_id.Valid { cid = int(category_id.Int64) + } else { + continue // needs a category id } if _, ok := newsFlat[cid]; !ok { diff --git a/route_handlers.go b/route_handlers.go index 9a33248..c0a0dc8 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -332,9 +332,13 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se offset = amount * argOffset } - news.LoadPage(db, offset, amount) - - ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree}) + news, count, err := news.LoadPage(db, offset, amount) + if err != nil { + session.AddFlash("Error loading news", flash_err) + } + + ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count}) + } func ServeFileHandler(res http.ResponseWriter, req *http.Request) { diff --git a/templates/pages/news.html b/templates/pages/news.html new file mode 100644 index 0000000..3985edd --- /dev/null +++ b/templates/pages/news.html @@ -0,0 +1,14 @@ +{{define "body"}} +