From 4e632602f45f343bdb1a17b19b2af03224396c79 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 21 Sep 2015 08:26:31 -0700 Subject: [PATCH] getting close to edit working --- news/news.go | 108 ++++++++++++------------ route_handlers.go | 36 ++++++-- templates/pages/{add.html => post.html} | 11 +-- 3 files changed, 87 insertions(+), 68 deletions(-) rename templates/pages/{add.html => post.html} (72%) diff --git a/news/news.go b/news/news.go index 1d7deab..2d345be 100644 --- a/news/news.go +++ b/news/news.go @@ -37,6 +37,12 @@ type NewsContainer struct { Children map[int]*NewsContainer } +func New(id int) *News { + news := &News{} + news.id = id + return news +} + // Insert News item into DB 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) @@ -76,6 +82,54 @@ func Get(db *sql.DB, id int) (*News, error) { return news, err } +// Load and return in NewsContainer format all the unexported news items +func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) { + categories.LoadCategories(db) // required by addContainer + + rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by timestamp DESC") + if err != nil { + fmt.Println("DB errpr reading LoadPage news: ", err) + return nil, 0, err + } + defer rows.Close() + + return convertSqlToNews(rows) +} + +// Load and return in NewsContainer format all the unexported news items +func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { + categories.LoadCategories(db) // required by addContainer + + rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by category_id ASC") + if err != nil { + fmt.Println("DB errpr reading unexported news: ", err) + return nil, 0, err + } + defer rows.Close() + + return convertSqlToNewsContainer(rows) +} + +// Helper fn - formating - math +func (this *NewsContainer) HeaderDepth(start int) int { + return start + this.Category.Depth() +} + +// Mark the current batch (news.exported is null) as exported in this batch (exported = now()) +func MarkExported(db *sql.DB) error { + now := time.Now() + + _, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now) + if err != nil { + fmt.Println("DB errror: news.MarkExported():", err) + } + return err +} + +func (news *News) Id() int { + return news.id +} + func nullStringToString(str *sql.NullString) string { if str.Valid { return str.String @@ -107,42 +161,6 @@ func addContainer(category_id int, flat, tree map[int]*NewsContainer) { } } -// Load and return in NewsContainer format all the unexported news items -func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) { - categories.LoadCategories(db) // required by addContainer - - rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by timestamp DESC") - if err != nil { - fmt.Println("DB errpr reading LoadPage news: ", err) - return nil, 0, err - } - defer rows.Close() - - return convertSqlToNews(rows) -} - -// Load and return in NewsContainer format all the unexported news items -func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) { - categories.LoadCategories(db) // required by addContainer - - rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by category_id ASC") - if err != nil { - fmt.Println("DB errpr reading unexported news: ", err) - return nil, 0, err - } - defer rows.Close() - - return convertSqlToNewsContainer(rows) -} - -//id int -//Url string -//Title string -//Category_id int -//Date time.Time -//Notes string -//Expoerted bool -//SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes, exported" func scanNews(rows *sql.Rows) (*News, error) { news := &News{} var url, title, notes sql.NullString @@ -207,22 +225,4 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err return newsTree, count, nil } -// Helper fn - formating - math -func (this *NewsContainer) HeaderDepth(start int) int { - return start + this.Category.Depth() -} -// Mark the current batch (news.exported is null) as exported in this batch (exported = now()) -func MarkExported(db *sql.DB) error { - now := time.Now() - - _, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now) - if err != nil { - fmt.Println("DB errror: news.MarkExported():", err) - } - return err -} - -func (news *News) Id() int { - return news.id -} diff --git a/route_handlers.go b/route_handlers.go index 1fae0e9..eb5b7e5 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -146,7 +146,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses popup := r.URL.Query().Get("popup") - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup}) + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { @@ -158,7 +158,10 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses popup := r.FormValue("popup") category_id, err := strconv.Atoi(r.FormValue("category")) if err != nil { - category_id = -1 + var flashes = make(map[string]interface{}) + flashes["error"] = []string{ "Category required: " +err.Error() } + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id}) + return } news.Category_id = category_id @@ -166,7 +169,8 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses if err != nil { var flashes = make(map[string]interface{}) flashes["error"] = []string{ "Error saving news: "+err.Error() } - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id}) + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id}) + return } else { session.AddFlash("Added news \""+news.Title+"\"", flash_info) session.Save(r, w) @@ -178,13 +182,11 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses } } -// func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { categories.LoadCategories(db) flashes := GetFlashes(session) - id, idErr := strconv.Atoi(r.FormValue("id")) if idErr != nil { session.AddFlash("Could not understand news id", flash_err) @@ -201,23 +203,39 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se return } - session.Save(r, w) - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": newsItem.Url, "title": newsItem.Title, "notes": newsItem.Notes, "popup": false, "category": newsItem.Category_id}) + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": newsItem.Url, "title": newsItem.Title, "notes": newsItem.Notes, "popup": false, "category": newsItem.Category_id, "id": newsItem.Id()}) return } func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { - news := news.News{} + news_id, err := strconv.Atoi(r.FormValue("id")) + if err != nil { + session.AddFlash("Error trying to save news item", flash_err) + session.Save(r, w) + http.Redirect(w, r, "/news", http.StatusFound) + return + } + + news := news.New(news_id) news.Title = r.FormValue("title") news.Notes = r.FormValue("notes") news.Url = r.FormValue("link") + popup := r.FormValue("popup") + + + category_id, err := strconv.Atoi(r.FormValue("category")) if err != nil { - category_id = -1 + var flashes = make(map[string]interface{}) + flashes["error"] = []string{ "Category required: " +err.Error() } + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id}) + return } + + news.Category_id = category_id err = news.Insert(db) diff --git a/templates/pages/add.html b/templates/pages/post.html similarity index 72% rename from templates/pages/add.html rename to templates/pages/post.html index 0290c4b..badd676 100644 --- a/templates/pages/add.html +++ b/templates/pages/post.html @@ -2,7 +2,8 @@

Add Link

{{template "flashes" .}}
- + +
Link:
Title:
@@ -10,7 +11,7 @@
@@ -23,8 +24,8 @@ {{define "option-category"}} - - {{range $child := .Children}} - {{template "option-category" $child}} + + {{range $child := $category.Children}} + {{template "option-category" dict "category" $child "category_id" .category_id}} {{end}} {{end}} \ No newline at end of file