Edit post works

This commit is contained in:
Dan Ballard 2015-09-23 21:12:57 -07:00
parent e0575939c3
commit 11a50e973c
5 changed files with 46 additions and 38 deletions

View File

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

View File

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

View File

@ -2,37 +2,20 @@
<h2 class="list-heading">List</h2>
{{template "flashes" .}}
<div class="row">
<div class="col-xs-2">
<a class="btn btn-lg btn-primary btn-block" href="{{template "launch-add" .}}">Add to transmet</a>
</div>
<div class="col-xs-4"></div>
<div class="col-xs-4"></div>
<div class="col-xs-2">
<button class="confirm-export btn btn-lg btn-primary btn-block" type="submit">Export</button>
</div>
<div class="col-xs-6">
Drag this link to bookmark bar and click anywhere to add a link
</div>
<div class="col-xs-6">
Click this to mark the current queue of news items as exported (clearing them)
</div>
<div class="col-xs-12">&nbsp;</div>
</div>
<textarea class="form-control" name="template" placeholder="Template" rows="16" cols="80">{{.template}}</textarea>
<div class="ros">
<div class="col-xs-10"></div>
<div class="col-xs-2"><b>{{.count}}</b> news items</div>
<div class="row">
<div class="col-xs-2">
<button class="confirm-export btn btn-lg btn-primary btn-block" type="submit">Export</button>
</div>
<div class="col-xs-6">
Click this to mark the current queue of news items as exported (clearing them)
</div>
<div class="col-xs-2"></div>
<div class="col-xs-2"><b>{{.count}}</b> news items</div>
</div>
{{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}}

View File

@ -1,6 +1,17 @@
{{define "body"}}
<h2 class="news-heading">News</h2>
{{template "flashes" .}}
<div class="row">
<div class="col-xs-2">
<a class="btn btn-lg btn-primary btn-block" href="{{template "launch-add" .}}">Add to transmet</a>
</div>
<div class="col-xs-6">
Drag this link to bookmark bar and click anywhere to add a link
</div>
<div class="col-xs-4"></div>
<div class="col-xs-12">&nbsp;</div>
</div>
<div class="news-posts">
{{range $news_post := .news}}
{{template "row-news" dict "post" $news_post "categories" $.categories}}
@ -37,3 +48,8 @@
</div>
</div>
{{end}}
<!-- JS for the launcher of the add bookmarklet -->
{{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}}

View File

@ -1,7 +1,7 @@
{{define "body"}}
<h2 class="form-add-heading">Add Link</h2>
<h2 class="form-add-heading">{{if eq .mode "add"}}Add Link{{else}}Edit News{{end}}</h2>
{{template "flashes" .}}
<form class="form-add" action="/add" method="post" role="form" class="container col-form">
<form class="form-add" action="{{if eq "add" .mode}}/add{{else}}/edit{{end}}" method="post" role="form" class="container col-form">
<input type="hidden" name="popup" value="{{.popup}}" />
<input type="hidden" name="id" value="{{.id}}" />
<div class="row">
@ -16,7 +16,7 @@
</select>
</div>
<div class="col-xs-2">Notes:</div><div class="col-xs-10"><textarea class="form-control" name="notes" placeholder="Notes" rows="5" cols="80">{{.notes}}</textarea></div>
<div class="col-xs-2"></div><div class="col-xs-10"><input class="add-submit btn btn-lg btn-primary btn-block" type="submit" value="Add Link" /></div>
<div class="col-xs-2"></div><div class="col-xs-10"><input class="add-submit btn btn-lg btn-primary btn-block" type="submit" value="{{if eq .mode "add"}}Add Link{{else}}Commit Edit{{end}}" /></div>
</div>
</form>