prep work for news page

This commit is contained in:
Dan Ballard 2015-07-14 08:10:22 -07:00
parent ff1b804cd6
commit 05fe8b575b
5 changed files with 32 additions and 4 deletions

View File

@ -5,7 +5,7 @@ $(document).ready( function () {
"text": "Mark current batch exported?", "text": "Mark current batch exported?",
"title": "Export confrimation", "title": "Export confrimation",
confirm: function() { confirm: function() {
window.location = "/export"; window.location = "/export-commit";
}, },
}); });

View File

@ -19,6 +19,12 @@ type News struct {
Expoerted bool Expoerted bool
} }
/* Storage Node containing:
* Name - categry name
* News children
* Category - category of this node
* Children - sub containers: mapped to sub categories
*/
type NewsContainer struct { type NewsContainer struct {
Name string Name string
News []News News []News
@ -26,6 +32,7 @@ type NewsContainer struct {
Children map[int]*NewsContainer Children map[int]*NewsContainer
} }
// Insert News item into DB
func (news *News) Insert(db *sql.DB) error { 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 ); _, 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 { if err != nil {
@ -43,6 +50,7 @@ func nullStringToString(str *sql.NullString) string {
} }
} }
// Init and add a news container to the Data Structs
func addContainer(category_id int, flat, tree map[int]*NewsContainer) { func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
container := &NewsContainer{ Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{} } container := &NewsContainer{ Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{} }
flat[category_id] = container flat[category_id] = container
@ -57,7 +65,7 @@ func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
} }
} }
// returns a tree of news items, the total count, and error // Load and return in NewsContainer format all the unexported news items
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
categories.LoadCategories(db) categories.LoadCategories(db)
@ -106,6 +114,7 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
return newsTree, count, nil return newsTree, count, nil
} }
// Helper fn - formating - math
func (this *NewsContainer) HeaderDepth(start int) int { func (this *NewsContainer) HeaderDepth(start int) int {
return start + this.Category.Depth() return start + this.Category.Depth()
} }

View File

@ -215,7 +215,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
fmt.Println("Exec err: ", err) fmt.Println("Exec err: ", err)
} }
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "count": count, "url": config.Url}) ShowTemplate("export", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "count": count, "url": config.Url})
} }
func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -321,6 +321,22 @@ func categoryDeleteHandler(w http.ResponseWriter, r *http.Request, user *user.Us
http.Redirect(w, r, "/categories", http.StatusFound) http.Redirect(w, r, "/categories", http.StatusFound)
} }
func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session)
session.Save(r, w)
var offset = 0
var amount = 100
argOffset, eOffset := strconv.Atoi(r.FormValue("offset"))
if eOffset == nil {
offset = amount * argOffset
}
news.LoadPage(db, offset, amount)
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) {
fname := path.Base(req.URL.Path) fname := path.Base(req.URL.Path)
http.ServeFile(res, req, "./"+fname) http.ServeFile(res, req, "./"+fname)
@ -339,7 +355,9 @@ func init_route_handlers() {
r.HandleFunc("/add", getPostHandler(userHandler(addFormHandler), userHandler(addPostHandler))) r.HandleFunc("/add", getPostHandler(userHandler(addFormHandler), userHandler(addPostHandler)))
r.HandleFunc("/", userHandler(templateFormHandler)) r.HandleFunc("/", userHandler(templateFormHandler))
r.HandleFunc("/export", userHandler(exportHandler)) r.HandleFunc("/news", userHandler(newsFormHandler))
r.HandleFunc("/export", userHandler(templateFormHandler))
r.HandleFunc("/export-commit", userHandler(exportHandler))
r.HandleFunc("/added", userHandler(addedHandler)) r.HandleFunc("/added", userHandler(addedHandler))
r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler))) r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler)))

View File

@ -33,6 +33,7 @@
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="/add">add</a></li> <li><a href="/add">add</a></li>
<li><a href="/export">export</a></li>
<li><a href="/categories">categories</a></li> <li><a href="/categories">categories</a></li>
</ul> </ul>