Skip to content

Commit

Permalink
Merge pull request #98 from kkeuning/updateable-example-logging
Browse files Browse the repository at this point in the history
logging id rather than title in AfterAcceptUpdate
  • Loading branch information
nilslice authored Mar 12, 2017
2 parents faf6b65 + 5798736 commit 0eaddb8
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions examples/updateable/content/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ func init() {
// String defines the display name of a Song in the CMS list-view
func (s *Song) String() string { return s.Title }

// BeforeAcceptUpdate is only called if the Song type implements api.Updateable
// It is called before AcceptUpdate, and returning an error will cancel the request
// causing the system to reject the data sent in the POST
func (s *Song) BeforeAcceptUpdate(res http.ResponseWriter, req *http.Request) error {
// do initial user authentication here on the request, checking for a
// token or cookie, or that certain form fields are set and valid

// for example, this will check if the request was made by a CMS admin user:
if !user.IsValid(req) {
addr := req.RemoteAddr
err := fmt.Errorf("request rejected, invalid user. IP: %s", addr)
return err
}

// you could then to data validation on the request post form, or do it in
// the Accept method, which is called after BeforeAccept

return nil
}

// AcceptUpdate is called after BeforeAccept and is where you may influence the
// merge process. For example, maybe you don't want an empty string for the Title
// or Artist field to be accepted by the update request. Updates will always merge
Expand All @@ -90,7 +110,6 @@ func (s *Song) AcceptUpdate(res http.ResponseWriter, req *http.Request) error {
// On update its fine if fields are missing, but we don't want
// title overwritten by a blank or empty string since that would
// break the display name. Artist is also required to be non-blank.

var required = map[string]interface{}{
"title": nil,
"artist": nil,
Expand All @@ -110,33 +129,13 @@ func (s *Song) AcceptUpdate(res http.ResponseWriter, req *http.Request) error {
return nil
}

// BeforeAcceptUpdate is only called if the Song type implements api.Updateable
// It is called before AcceptUpdate, and returning an error will cancel the request
// causing the system to reject the data sent in the POST
func (s *Song) BeforeAcceptUpdate(res http.ResponseWriter, req *http.Request) error {
// do initial user authentication here on the request, checking for a
// token or cookie, or that certain form fields are set and valid

// for example, this will check if the request was made by a CMS admin user:
if !user.IsValid(req) {
addr := req.RemoteAddr
err := fmt.Errorf("request rejected, invalid user. IP: %s", addr)
return err
}

// you could then to data validation on the request post form, or do it in
// the Accept method, which is called after BeforeAccept

return nil
}

// AfterAcceptUpdate is called after AcceptUpdate, and is useful for logging or triggering
// notifications, etc. after the data is saved to the database, etc.
// The request has a context containing the databse 'target' affected by the
// request.
func (s *Song) AfterAcceptUpdate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("Song updated by:", addr, "with title", req.PostFormValue("title"))
log.Println("Song updated by:", addr, "id:", req.URL.Query().Get("id"))

return nil
}

0 comments on commit 0eaddb8

Please sign in to comment.