finish up route/editPostHandle

This commit is contained in:
Dan Ballard 2015-09-23 20:22:12 -07:00
parent 0b17312c22
commit e0575939c3
2 changed files with 18 additions and 23 deletions

View File

@ -37,11 +37,11 @@ type NewsContainer struct {
Children map[int]*NewsContainer Children map[int]*NewsContainer
} }
func New(id int) *News { /*func New(id int) *News {
news := &News{} news := &News{}
news.id = id news.id = id
return news return news
} }*/
// Insert News item into DB // Insert News item into DB
func (news *News) Insert(db *sql.DB) error { func (news *News) Insert(db *sql.DB) error {

View File

@ -217,45 +217,40 @@ func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
return 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.Title = r.FormValue("title")
news.Notes = r.FormValue("notes") news.Notes = r.FormValue("notes")
news.Url = r.FormValue("link") news.Url = r.FormValue("link")
popup := r.FormValue("popup")
category_id, err := strconv.Atoi(r.FormValue("category")) category_id, err := strconv.Atoi(r.FormValue("category"))
if err != nil { if err != nil {
var flashes = make(map[string]interface{}) var flashes = make(map[string]interface{})
flashes["error"] = []string{ "Category required: " +err.Error() } 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 return
} }
news.Category_id = category_id news.Category_id = category_id
err = news.Insert(db) err = news.Update(db)
if err != nil { if err != nil {
session.AddFlash("Error saving news: "+err.Error(), flash_err) var flashes = make(map[string]interface{})
session.Values["title"] = news.Title flashes["error"] = []string{ "Error saving news: "+err.Error() }
session.Values["link"] = news.Url 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})
session.Values["notes"] = news.Notes return
session.Values["popup"] = popup
session.Save(r, w)
http.Redirect(w, r, "/add", http.StatusFound)
} else { } else {
// TODO auto close? redirect session.AddFlash("Updated news \""+news.Title+"\"", flash_info)
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
session.Save(r, w) 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)
} }
} }
}
func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {