From e0575939c31d8b091ee5fb6ff8fff92b7e735357 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 23 Sep 2015 20:22:12 -0700 Subject: [PATCH] finish up route/editPostHandle --- news/news.go | 4 ++-- route_handlers.go | 37 ++++++++++++++++--------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/news/news.go b/news/news.go index 2d345be..023b01f 100644 --- a/news/news.go +++ b/news/news.go @@ -37,11 +37,11 @@ type NewsContainer struct { Children map[int]*NewsContainer } -func New(id int) *News { +/*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 { diff --git a/route_handlers.go b/route_handlers.go index ebaeb8b..2a650a5 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -217,43 +217,38 @@ func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, se return } - news := news.New(news_id) + news, err := news.Get(db, news_id) + if err != nil { + fmt.Println("Error trying to edit news item that doesn't exist") + session.AddFlash("Error trying to save news item", flash_err) + session.Save(r, w) + http.Redirect(w, r, "/news", http.StatusFound) + return + } 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 { 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}) + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": false, "notes": news.Notes, "category_id": news.Category_id}) return } - - news.Category_id = category_id - err = news.Insert(db) + err = news.Update(db) if err != nil { - session.AddFlash("Error saving news: "+err.Error(), flash_err) - session.Values["title"] = news.Title - session.Values["link"] = news.Url - session.Values["notes"] = news.Notes - session.Values["popup"] = popup - session.Save(r, w) - http.Redirect(w, r, "/add", http.StatusFound) + var flashes = make(map[string]interface{}) + flashes["error"] = []string{ "Error saving news: "+err.Error() } + ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": false, "notes": news.Notes, "category_id": news.Category_id}) + return } else { - // TODO auto close? redirect - session.AddFlash("Added news \""+news.Title+"\"", flash_info) + session.AddFlash("Updated news \""+news.Title+"\"", flash_info) session.Save(r, w) - if popup == "1" { - http.Redirect(w, r, "/added", http.StatusFound) - } else { - http.Redirect(w, r, "/", http.StatusFound) - } + http.Redirect(w, r, "/", http.StatusFound) } }