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
}
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 {

View File

@ -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)
}
}