func (d *PostDriver) beforeSave(p *blog.Post) *ds.Key { if p.Id == "" { p.Id, _ = wcg.UUID() p.CreatedAt = time.Now() } p.UpdatedAt = time.Now() return d.NewKey(p.Id, 0, nil) }
func updateAttributesFromForm(res *wcg.Response, req *wcg.Request, p *blog.Post) error { var err error if err = postFormValidator.Eval(req.HttpRequest().PostForm); err != nil { return err } p.Title = req.Form("title") p.Content = req.Form("content") p.Tags = blog.ParseTagString(req.Form("tags")) p.IsDraft = req.Form("is_draft") == "true" p.IsHidden = req.Form("is_hidden") == "true" p.PublishAt, err = wcg.ParseDateTime(req.Form("publish_at")) return nil }
func setupPages(app *App) { app.Page.Navi("/", "記事一覧"). AssetPath("/blogapp/index.js"). Templates("./blogapp/list.html", "./blogapp/post_part.html"). Handler( func(res *wcg.Response, req *wcg.Request, p *lib.Page) { result, err := queryPosts(res, req, 20) if err != nil { panic(err) } p.Ogp("type", "blog") res.SetLocal("Posts", result.Posts) res.SetLocal("Next", result.Next) res.SetLocal("Previous", result.Previous) }, ) app.Page.Navi("/admin/editor.html", "記事管理"). RequireAuth(lib.Admin). Templates("./blogapp/admin/editor.html"). AssetPath("/blogapp/admin/editor.js") app.Page.Page("/posts/:id.html", ""). Templates("./blogapp/show.html", "./blogapp/post_part.html"). AssetPath("/blogapp/posts/show.js"). Handler( func(res *wcg.Response, req *wcg.Request, p *lib.Page) { var post blog.Post driver := NewPostDriver(lib.NewAppContextFromRequest(req)) key := driver.NewKey(req.Param("id"), 0, nil) err := driver.Get(key, &post) if err != nil { lib.Error(res, req, err) return } p.Title = post.Title p.Canonical = wcg.AbsoluteUrl(req, app.Page.Path(fmt.Sprintf("/posts/%s.html", post.Id))) p.Ogp("title", fmt.Sprintf("%s: %s", p.App.Title, p.Title)) p.Ogp("url", p.Canonical) p.Ogp("type", "article") p.Ogp("description", post.Summary()) imageUrl := post.ExtractFirstImageUrl() if imageUrl != "" { p.Ogp("image", imageUrl) } res.SetLocal("Post", post) }) }