finish db fn, start template

This commit is contained in:
Dan Ballard 2015-07-21 07:10:57 -07:00
parent 2a5a2f451e
commit d860a5ba02
3 changed files with 38 additions and 7 deletions

View File

@ -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 // 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 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") 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 // 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 return nil, 0, err
} }
defer rows.Close() defer rows.Close()
return convertSqlToNewsContainer(rows)
}
func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, error) {
newsTree := map[int]*NewsContainer{} newsTree := map[int]*NewsContainer{}
newsFlat := map[int]*NewsContainer{} newsFlat := map[int]*NewsContainer{}
count := 0 count := 0
@ -106,9 +117,11 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
news.Date = time.Now() news.Date = time.Now()
} }
cid := 1 var cid int
if category_id.Valid { if category_id.Valid {
cid = int(category_id.Int64) cid = int(category_id.Int64)
} else {
continue // needs a category id
} }
if _, ok := newsFlat[cid]; !ok { if _, ok := newsFlat[cid]; !ok {

View File

@ -332,9 +332,13 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
offset = amount * argOffset offset = amount * argOffset
} }
news.LoadPage(db, offset, amount) 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})
ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree})
} }
func ServeFileHandler(res http.ResponseWriter, req *http.Request) { func ServeFileHandler(res http.ResponseWriter, req *http.Request) {

14
templates/pages/news.html Normal file
View File

@ -0,0 +1,14 @@
{{define "body"}}
<h2 class="news-heading">News</h2>
{{template "flashes" .}}
{{range $news_post := .news}}
{{template "row-news" $news_post}}
{{end}}
{{end}}
<!-- print a news row -->
{{define "row-news"}}
{{end}}