diff options
author | Steve <nilslice@gmail.com> | 2016-10-13 13:02:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-13 13:02:27 -0700 |
commit | 9c8ed5c6a88901bf139bf9bb7b884b222c6053ce (patch) | |
tree | a4aef6a41f4777ae037656820edefb80da62c03a | |
parent | 7556e81dcf93c9f93c65aae46ceb871dd6f99812 (diff) | |
parent | 468d9fb3355bd7771af3592becb201b6e9b1e68b (diff) |
Merge pull request #1 from bosssauce/ponzu-dev
[fundamental-feature] Delete Content
-rw-r--r-- | management/editor/editor.go | 29 | ||||
-rw-r--r-- | system/admin/handlers.go | 31 | ||||
-rw-r--r-- | system/admin/server.go | 1 | ||||
-rw-r--r-- | system/admin/static/dashboard/css/admin.css | 4 | ||||
-rw-r--r-- | system/db/content.go | 18 |
5 files changed, 81 insertions, 2 deletions
diff --git a/management/editor/editor.go b/management/editor/editor.go index 06d5a59..f8b1970 100644 --- a/management/editor/editor.go +++ b/management/editor/editor.go @@ -46,9 +46,34 @@ func Form(post Editable, fields ...Field) ([]byte, error) { addPostDefaultFieldsToEditorView(post, editor) submit := ` -<div class="input-field"> - <button class="right waves-effect waves-light btn green" type="submit">Save</button> +<div class="input-field post-controls"> + <button class="right waves-effect waves-light btn green save-post" type="submit">Save</button> + <button class="right waves-effect waves-light btn red delete-post" type="submit">Delete</button> </div> + +<script> + $(function() { + var form = $('form'), + del = form.find('button.delete-post'), + id = form.find('input[name=id]'); + + // hide delete button if this is a new post, or a non-post editor page + if (id.val() === '-1' || form.attr('action') !== '/admin/edit') { + del.hide(); + } + + del.on('click', function(e) { + e.preventDefault(); + var action = form.attr('action'); + action = action + '/delete'; + form.attr('action', action); + + if (confirm("[Ponzu] Please confirm:\n\nAre you sure you want to delete this post?\nThis cannot be undone.")) { + form.submit(); + } + }); + }); +</script> ` editor.ViewBuf.Write([]byte(submit + `</td></tr></tbody></table>`)) diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 2be8d8e..d492335 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -513,6 +513,37 @@ func editHandler(res http.ResponseWriter, req *http.Request) { } } +func deleteHandler(res http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + res.WriteHeader(http.StatusMethodNotAllowed) + return + } + + err := req.ParseMultipartForm(1024 * 1024 * 4) // maxMemory 4MB + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + id := req.FormValue("id") + t := req.FormValue("type") + + if id == "" || t == "" { + res.WriteHeader(http.StatusBadRequest) + return + } + + err = db.DeleteContent(t + ":" + id) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + redir := strings.TrimSuffix(req.URL.Scheme+req.URL.Host+req.URL.Path, "/edit/delete") + redir = redir + "/posts?type=" + t + http.Redirect(res, req, redir, http.StatusFound) +} + func editUploadHandler(res http.ResponseWriter, req *http.Request) { if req.Method != http.MethodPost { res.WriteHeader(http.StatusMethodNotAllowed) diff --git a/system/admin/server.go b/system/admin/server.go index 6f4a96e..b3b128d 100644 --- a/system/admin/server.go +++ b/system/admin/server.go @@ -25,6 +25,7 @@ func Run() { http.HandleFunc("/admin/posts/search", user.Auth(searchHandler)) http.HandleFunc("/admin/edit", user.Auth(editHandler)) + http.HandleFunc("/admin/edit/delete", user.Auth(deleteHandler)) http.HandleFunc("/admin/edit/upload", user.Auth(editUploadHandler)) pwd, err := os.Getwd() diff --git a/system/admin/static/dashboard/css/admin.css b/system/admin/static/dashboard/css/admin.css index 888dbf1..7da4154 100644 --- a/system/admin/static/dashboard/css/admin.css +++ b/system/admin/static/dashboard/css/admin.css @@ -154,6 +154,10 @@ footer p { color: #9e9e9e; } +.post-controls .save-post { + margin-left: 10px; +} + /* OVERRIDE Bootstrap + Materialize conflicts */ .iso-texteditor.input-field label { diff --git a/system/db/content.go b/system/db/content.go index d637085..1e5b95a 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -137,6 +137,24 @@ func postToJSON(ns string, data url.Values) ([]byte, error) { return j, nil } +// DeleteContent removes an item from the database. Deleting a non-existent item +// will return a nil error. +func DeleteContent(target string) error { + t := strings.Split(target, ":") + ns, id := t[0], t[1] + + err := store.Update(func(tx *bolt.Tx) error { + tx.Bucket([]byte(ns)).Delete([]byte(id)) + return nil + }) + + if err != nil { + return err + } + + return nil +} + // Content retrives one item from the database. Non-existent values will return an empty []byte // The `target` argument is a string made up of namespace:id (string:int) func Content(target string) ([]byte, error) { |