// HandleUpdate serves POST or PUT /tags/1/update func HandleUpdate(context router.Context) error { // Find the tag tag, err := tags.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, tag) if err != nil { return router.NotAuthorizedError(err) } // Update the tag params, err := context.Params() if err != nil { return router.InternalError(err) } err = tag.Update(params.Map()) if err != nil { return router.InternalError(err) } // Redirect to tag return router.Redirect(context, tag.URLShow()) }
// HandleUpdateShow serves a get request at /tags/1/update (show form to update) func HandleUpdateShow(context router.Context) error { // Find the tag tag, err := tags.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, tag) if err != nil { return router.NotAuthorizedError(err) } // Render the template view := view.New(context) view.AddKey("tag", tag) return view.Render() }
// HandleDestroy responds to POST /tags/1/destroy func HandleDestroy(context router.Context) error { // Set the tag on the context for checking tag, err := tags.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, tag) if err != nil { return router.NotAuthorizedError(err) } // Destroy the tag tag.Destroy() // Redirect to tags root return router.Redirect(context, tag.URLIndex()) }
// HandleCreate responds to POST tags/create 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 := tags.Create(params.Map()) if err != nil { context.Logf("#info Failed to create tag %v", params) return router.InternalError(err) } // Log creation context.Logf("#info Created tag id,%d", id) // Redirect to the new tag tag, err := tags.Find(id) if err != nil { context.Logf("#error Error creating tag,%s", err) } // Always regenerate dotted ids - we fetch all tags first to avoid db calls q := tags.Query().Select("select id,parent_id from tags").Order("id asc") tagsList, err := tags.FindAll(q) if err == nil { dottedParams := map[string]string{} dottedParams["dotted_ids"] = tag.CalculateDottedIds(tagsList) tags.Query().Where("id=?", tag.Id).Update(dottedParams) } return router.Redirect(context, tag.URLIndex()) }