// HandleCreate handles the POST of the create form for posts func HandleCreate(context router.Context) error { // Authorise err := authorise.Path(context) if err != nil { return router.NotAuthorizedError(err) } // Setup context params, err := context.Params() if err != nil { return router.InternalError(err) } id, err := posts.Create(params.Map()) if err != nil { return router.InternalError(err) } // Log creation context.Logf("#info Created post id,%d", id) // Redirect to the new post m, err := posts.Find(id) if err != nil { return router.InternalError(err) } return router.Redirect(context, m.URLIndex()) }
// HandleUpdate handles the POST of the form to update a post func HandleUpdate(context router.Context) error { // Find the post post, err := posts.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise update post err = authorise.Resource(context, post) if err != nil { return router.NotAuthorizedError(err) } // Update the post from params params, err := context.Params() if err != nil { return router.InternalError(err) } err = post.Update(params.Map()) if err != nil { return router.InternalError(err) } // Redirect to post return router.Redirect(context, post.URLShow()) }
// HandleUpdateShow renders the form to update a post func HandleUpdateShow(context router.Context) error { // Find the post post, err := posts.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise update post err = authorise.Resource(context, post) if err != nil { return router.NotAuthorizedError(err) } // Find users for author details users, err := users.FindAll(users.Admins()) if err != nil { return router.NotFoundError(err) } // Render the template view := view.New(context) view.AddKey("post", post) view.AddKey("users", users) return view.Render() }
// HandleDestroy handles a DESTROY request for posts func HandleDestroy(context router.Context) error { // Find the post post, err := posts.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise destroy post err = authorise.Resource(context, post) if err != nil { return router.NotAuthorizedError(err) } // Destroy the post post.Destroy() // Redirect to posts root return router.Redirect(context, post.URLIndex()) }
// HandleShow displays a single post func HandleShow(context router.Context) error { // Find the post post, err := posts.Find(context.ParamInt("id")) if err != nil { return router.InternalError(err) } // Authorise access only if not published if !post.IsPublished() { err = authorise.Resource(context, post) if err != nil { return router.NotAuthorizedError(err) } } // Render the template view := view.New(context) view.AddKey("post", post) return view.Render() }