From 11a50e973cbec2732a5bcdd69a7eef6471404a27 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 23 Sep 2015 21:12:57 -0700 Subject: [PATCH] Edit post works --- news/news.go | 9 ++++++++ route_handlers.go | 12 +++++------ templates/pages/export.html | 41 +++++++++++-------------------------- templates/pages/news.html | 16 +++++++++++++++ templates/pages/post.html | 6 +++--- 5 files changed, 46 insertions(+), 38 deletions(-) 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 @@

List

{{template "flashes" .}} -
- -
-
- -
- -
- -
-Drag this link to bookmark bar and click anywhere to add a link -
-
-Click this to mark the current queue of news items as exported (clearing them) -
- -
 
- -
- - -
-
-
{{.count}} news items
+
+
+ +
+ +
+ Click this to mark the current queue of news items as exported (clearing them) +
+
+
{{.count}} news items
+ +
{{end}} -{{define "launch-add"}} -javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='{{.url}}add',l=d.location,e=encodeURIComponent,u=f+'?popup=1&url='+e(l.href)+'&title='+e(d.title);a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=410'))l.href=u;};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();void(0) -{{end}} \ No newline at end of file diff --git a/templates/pages/news.html b/templates/pages/news.html index db67378..910419d 100644 --- a/templates/pages/news.html +++ b/templates/pages/news.html @@ -1,6 +1,17 @@ {{define "body"}}

News

{{template "flashes" .}} +
+ + +
+ Drag this link to bookmark bar and click anywhere to add a link +
+
+
 
+
{{range $news_post := .news}} {{template "row-news" dict "post" $news_post "categories" $.categories}} @@ -37,3 +48,8 @@
{{end}} + + +{{define "launch-add"}} +javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='{{.url}}add',l=d.location,e=encodeURIComponent,u=f+'?popup=1&url='+e(l.href)+'&title='+e(d.title);a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=410'))l.href=u;};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();void(0) +{{end}} \ No newline at end of file diff --git a/templates/pages/post.html b/templates/pages/post.html index a9a3412..d036d78 100644 --- a/templates/pages/post.html +++ b/templates/pages/post.html @@ -1,7 +1,7 @@ {{define "body"}} -

Add Link

+

{{if eq .mode "add"}}Add Link{{else}}Edit News{{end}}

{{template "flashes" .}} -
+
@@ -16,7 +16,7 @@
Notes:
-
+