From 1b15bb3c1a2dc8f3a73260e4034913089d7bbcfc Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Sun, 21 May 2017 22:54:44 -0700 Subject: make a proper item for Hookable methods We have all of the information, it's nice to be access the item's members in a natural way, without having to look at the reqest. --- system/api/create.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'system/api') diff --git a/system/api/create.go b/system/api/create.go index 3328bd6..3b748cc 100644 --- a/system/api/create.go +++ b/system/api/create.go @@ -12,6 +12,8 @@ import ( "github.com/ponzu-cms/ponzu/system/admin/upload" "github.com/ponzu-cms/ponzu/system/db" "github.com/ponzu-cms/ponzu/system/item" + + "github.com/gorilla/schema" ) // Createable accepts or rejects external POST requests to endpoints such as: @@ -131,6 +133,17 @@ func createContentHandler(res http.ResponseWriter, req *http.Request) { return } + // Let's be nice and make a proper item for the Hookable methods + dec := schema.NewDecoder() + dec.IgnoreUnknownKeys(true) + dec.SetAliasTag("json") + err = dec.Decode(post, req.PostForm) + if err != nil { + log.Println("Error decoding post form for edit handler:", t, err) + res.WriteHeader(http.StatusBadRequest) + return + } + err = hook.BeforeAPICreate(res, req) if err != nil { log.Println("[Create] error calling BeforeAccept:", err) -- cgit v1.2.3 From c672491d2beeb1fe9c39753192786b8620e73c39 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Wed, 24 May 2017 01:03:29 -0700 Subject: provide delete hooks with the item --- system/api/delete.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'system/api') diff --git a/system/api/delete.go b/system/api/delete.go index 36f2b1b..178e82f 100644 --- a/system/api/delete.go +++ b/system/api/delete.go @@ -65,6 +65,18 @@ func deleteContentHandler(res http.ResponseWriter, req *http.Request) { return } + data, err := db.Content(t + ":" + id) + if err != nil { + log.Println("Error in db.Content ", t+":"+id, err) + res.WriteHeader(http.StatusBadRequest) + return + } + + err = json.Unmarshal(data, post) + if err != nil { + log.Println("Error unmarshalling ", t, "=", id, err, " Hooks will be called on a zero-value.") + } + err = hook.BeforeAPIDelete(res, req) if err != nil { log.Println("[Delete] error calling BeforeAPIDelete:", err) -- cgit v1.2.3