diff options
author | Steve <nilslice@gmail.com> | 2017-03-11 13:11:42 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-11 13:11:42 -0800 |
commit | e89d972a87ecc3cecede3817a71d87566becff53 (patch) | |
tree | 0b1b72e772226d44ed0d96b04252c70127ab923d | |
parent | 8c5343745b09b0993bf869ef03d44b3a75d29a0e (diff) |
keep interface implementation minimal
removing the methods that aren't explicitly required to handle the functionality of the Updateable implementation
-rw-r--r-- | examples/updateable/content/song.go | 83 |
1 files changed, 20 insertions, 63 deletions
diff --git a/examples/updateable/content/song.go b/examples/updateable/content/song.go index ba806bd..0de3658 100644 --- a/examples/updateable/content/song.go +++ b/examples/updateable/content/song.go @@ -79,69 +79,6 @@ func init() { // String defines the display name of a Song in the CMS list-view func (s *Song) String() string { return s.Title } -// Accept implements api.Externalable, and allows external POST requests from clients -// to add content as long as the request contains the json tag names of the Song -// struct fields, and is multipart encoded -func (s *Song) Accept(res http.ResponseWriter, req *http.Request) error { - // do form data validation for required fields - required := []string{ - "title", - "artist", - "rating", - "opinion", - "spotify_url", - } - - for _, r := range required { - if req.PostFormValue(r) == "" { - err := fmt.Errorf("request missing required field: %s", r) - return err - } - } - - return nil -} - -// BeforeAccept is only called if the Song type implements api.Externalable -// It is called before Accept, and returning an error will cancel the request -// causing the system to reject the data sent in the POST -func (s *Song) BeforeAccept(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 -} - -// 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 @@ -173,6 +110,26 @@ 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 +} + // AfterAccept is called after Accept, 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 |