// HandleUpdate handles POST or PUT /images/1/update func HandleUpdate(context router.Context) error { // Find the image image, err := images.Find(context.ParamInt("id")) if err != nil { context.Logf("#error Error finding image %s", err) return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, image) if err != nil { return router.NotAuthorizedError(err) } // Update the image params, err := context.Params() if err != nil { return router.InternalError(err) } err = image.Update(params.Map()) if err != nil { return router.InternalError(err) } // We redirect back to source if redirect param is set return router.Redirect(context, image.URLUpdate()) }
// HandleShow serve a get request at /users/1 func HandleShow(context router.Context) error { // Find the user user, err := users.Find(context.ParamInt("id")) if err != nil { context.Logf("#error parsing user id: %s", err) return router.NotFoundError(err) } userMeta := fmt.Sprintf("%s – %s", user.Name, user.Summary) // Set up view view := view.New(context) // Find the first image which matches this user image, err := images.Find(user.ImageID) if err == nil { // only add image key if we have one view.AddKey("image", image) } // Render the Template view.AddKey("user", user) view.AddKey("meta_title", userMeta) view.AddKey("meta_desc", userMeta) view.AddKey("meta_keywords", user.Keywords()) return view.Render() }
// HandleCreate responds to POST images/create func HandleCreate(context router.Context) error { // Authorise err := authorise.Path(context) if err != nil { return router.NotAuthorizedError(err) } // We expect only one image, what about replacing the existing when updating? // At present we just create a new image files, err := context.ParamFiles("image") if err != nil { return router.InternalError(err) } if len(files) == 0 { return router.NotFoundError(nil) } // Get the params params, err := context.Params() if err != nil { return router.InternalError(err) } id, err := images.Create(params.Map(), files[0]) if err != nil { context.Logf("#error Error creating image,%s", err) return router.InternalError(err) } // Log creation context.Logf("#info Created image id,%d", id) // Redirect to the new image m, err := images.Find(id) if err != nil { context.Logf("#error Error creating image,%s", err) } return router.Redirect(context, m.URLShow()) }
// HandleUpdateShow serves a get request at /images/1/update (show form to update) func HandleUpdateShow(context router.Context) error { // Setup context for template view := view.New(context) image, err := images.Find(context.ParamInt("id")) if err != nil { context.Logf("#error Error finding image %s", err) return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, image) if err != nil { return router.NotAuthorizedError(err) } view.AddKey("redirect", context.Param("redirect")) view.AddKey("image", image) return view.Render() }
// HandleShow serves a get request at /images/1 func HandleShow(context router.Context) error { // Find the image image, err := images.Find(context.ParamInt("id")) if err != nil { return router.InternalError(err) } // Authorise err = authorise.Resource(context, image) if err != nil { return router.NotAuthorizedError(err) } // Serve template view := view.New(context) view.AddKey("image", image) return view.Render() }
// POST /images/1/destroy func HandleDestroy(context router.Context) error { // Set the image on the context for checking image, err := images.Find(context.ParamInt("id")) if err != nil { return router.NotFoundError(err) } // Authorise err = authorise.Resource(context, image) if err != nil { return router.NotAuthorizedError(err) } // Destroy the image image.Destroy() // Redirect to sites - better than images root as a default // but should never be used if we have a redirect return router.Redirect(context, "/sites") }