diff --git a/news/news.go b/news/news.go index 023b01f..9802b87 100644 --- a/news/news.go +++ b/news/news.go @@ -53,6 +53,15 @@ func (news *News) Insert(db *sql.DB) error { return nil } +// Update updatable parts of news itemn in DB +func (news *News) Update(db *sql.DB) error { + _, err := db.Exec("UPDATE news SET title=$1, url=$2, category_id=$3, notes=$4 WHERE id=$5", news.Title, news.Url, news.Category_id, news.Notes, news.id) + if err != nil { + fmt.Println("Error updating news: ", err) + } + return err +} + func Delete(db *sql.DB, id int) error { _, err := db.Exec("DELETE FROM news WHERE id = $1", id) if err != nil { diff --git a/route_handlers.go b/route_handlers.go index 2a650a5..0adaabf 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("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup}) + ShowTemplate("post", w, map[string]interface{}{"mode": "add", "user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup, "category_id": -1}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { @@ -160,7 +160,7 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses 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{}{"mode": "add", "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 @@ -169,7 +169,7 @@ 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("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{}{"mode": "add", "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) @@ -204,7 +204,7 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se } session.Save(r, w) - 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_id": newsItem.Category_id, "id": newsItem.Id()}) + ShowTemplate("post", w, map[string]interface{}{"mode": "edit", "user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": newsItem.Url, "title": newsItem.Title, "notes": newsItem.Notes, "popup": false, "category_id": newsItem.Category_id, "id": newsItem.Id()}) return } @@ -234,7 +234,7 @@ func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, se 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": false, "notes": news.Notes, "category_id": news.Category_id}) + ShowTemplate("post", w, map[string]interface{}{"mode": "edit", "user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": false, "notes": news.Notes, "category_id": news.Category_id, "id": news.Id()}) return } news.Category_id = category_id @@ -243,7 +243,7 @@ func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, se if err != nil { 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}) + ShowTemplate("post", w, map[string]interface{}{"mode": "edit", "user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": false, "notes": news.Notes, "category_id": news.Category_id, "id": news.Id()}) return } else { session.AddFlash("Updated news \""+news.Title+"\"", flash_info) diff --git a/templates/pages/export.html b/templates/pages/export.html index 70024d0..5a40e07 100644 --- a/templates/pages/export.html +++ b/templates/pages/export.html @@ -2,37 +2,20 @@