github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/stories/actions/destroy.go (about) 1 package storyactions 2 3 import ( 4 "net/http" 5 6 "github.com/fragmenta/auth/can" 7 "github.com/fragmenta/mux" 8 "github.com/fragmenta/server" 9 10 "github.com/bitcubate/cryptojournal/src/lib/session" 11 "github.com/bitcubate/cryptojournal/src/stories" 12 ) 13 14 // HandleDestroy responds to /stories/n/destroy by deleting the story. 15 func HandleDestroy(w http.ResponseWriter, r *http.Request) error { 16 17 // Fetch the params 18 params, err := mux.Params(r) 19 if err != nil { 20 return server.InternalError(err) 21 } 22 23 // Find the story 24 story, err := stories.Find(params.GetInt(stories.KeyName)) 25 if err != nil { 26 return server.NotFoundError(err) 27 } 28 29 // Check the authenticity token 30 err = session.CheckAuthenticity(w, r) 31 if err != nil { 32 return err 33 } 34 35 // Authorise destroy story 36 err = can.Destroy(story, session.CurrentUser(w, r)) 37 if err != nil { 38 return server.NotAuthorizedError(err) 39 } 40 41 // Destroy the story 42 story.Destroy() 43 44 // Redirect to stories root 45 return server.Redirect(w, r, story.IndexURL()) 46 47 }