From 2fce1e1ac99f3839429b2185b29aa3b56b1b1943 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 10 Jun 2017 12:30:32 +0800 Subject: [PATCH] Format and remove 13.5.md spaces --- zh/13.5.md | 332 ++++++++++++++++++++++++++--------------------------- 1 file changed, 166 insertions(+), 166 deletions(-) diff --git a/zh/13.5.md b/zh/13.5.md index 9a64f130..d42e5617 100644 --- a/zh/13.5.md +++ b/zh/13.5.md @@ -26,173 +26,173 @@ 博客主要的路由规则如下所示: ```Go - //显示博客首页 - beego.Router("/", &controllers.IndexController{}) - //查看博客详细信息 - beego.Router("/view/:id([0-9]+)", &controllers.ViewController{}) - //新建博客博文 - beego.Router("/new", &controllers.NewController{}) - //删除博文 - beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{}) - //编辑博文 - beego.Router("/edit/:id([0-9]+)", &controllers.EditController{}) +//显示博客首页 +beego.Router("/", &controllers.IndexController{}) +//查看博客详细信息 +beego.Router("/view/:id([0-9]+)", &controllers.ViewController{}) +//新建博客博文 +beego.Router("/new", &controllers.NewController{}) +//删除博文 +beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{}) +//编辑博文 +beego.Router("/edit/:id([0-9]+)", &controllers.EditController{}) ``` ## 数据库结构 数据库设计最简单的博客信息 ```sql - CREATE TABLE entries ( - id INT AUTO_INCREMENT, - title TEXT, - content TEXT, - created DATETIME, - primary key (id) - ); +CREATE TABLE entries ( + id INT AUTO_INCREMENT, + title TEXT, + content TEXT, + created DATETIME, + primary key (id) +); ``` ## 控制器 IndexController: ```Go - type IndexController struct { - beego.Controller - } +type IndexController struct { + beego.Controller +} - func (this *IndexController) Get() { - this.Data["blogs"] = models.GetAll() - this.Layout = "layout.tpl" - this.TplNames = "index.tpl" - } +func (this *IndexController) Get() { + this.Data["blogs"] = models.GetAll() + this.Layout = "layout.tpl" + this.TplNames = "index.tpl" +} ``` ViewController: ```Go - type ViewController struct { - beego.Controller - } +type ViewController struct { + beego.Controller +} - func (this *ViewController) Get() { - id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) - this.Data["Post"] = models.GetBlog(id) - this.Layout = "layout.tpl" - this.TplNames = "view.tpl" - } +func (this *ViewController) Get() { + id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) + this.Data["Post"] = models.GetBlog(id) + this.Layout = "layout.tpl" + this.TplNames = "view.tpl" +} ``` NewController ```Go - type NewController struct { - beego.Controller - } +type NewController struct { + beego.Controller +} - func (this *NewController) Get() { - this.Layout = "layout.tpl" - this.TplNames = "new.tpl" - } +func (this *NewController) Get() { + this.Layout = "layout.tpl" + this.TplNames = "new.tpl" +} - func (this *NewController) Post() { - inputs := this.Input() - var blog models.Blog - blog.Title = inputs.Get("title") - blog.Content = inputs.Get("content") - blog.Created = time.Now() - models.SaveBlog(blog) - this.Ctx.Redirect(302, "/") - } +func (this *NewController) Post() { + inputs := this.Input() + var blog models.Blog + blog.Title = inputs.Get("title") + blog.Content = inputs.Get("content") + blog.Created = time.Now() + models.SaveBlog(blog) + this.Ctx.Redirect(302, "/") +} ``` EditController ```Go - type EditController struct { - beego.Controller - } +type EditController struct { + beego.Controller +} - func (this *EditController) Get() { - id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) - this.Data["Post"] = models.GetBlog(id) - this.Layout = "layout.tpl" - this.TplNames = "edit.tpl" - } +func (this *EditController) Get() { + id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) + this.Data["Post"] = models.GetBlog(id) + this.Layout = "layout.tpl" + this.TplNames = "edit.tpl" +} - func (this *EditController) Post() { - inputs := this.Input() - var blog models.Blog - blog.Id, _ = strconv.Atoi(inputs.Get("id")) - blog.Title = inputs.Get("title") - blog.Content = inputs.Get("content") - blog.Created = time.Now() - models.SaveBlog(blog) - this.Ctx.Redirect(302, "/") - } +func (this *EditController) Post() { + inputs := this.Input() + var blog models.Blog + blog.Id, _ = strconv.Atoi(inputs.Get("id")) + blog.Title = inputs.Get("title") + blog.Content = inputs.Get("content") + blog.Created = time.Now() + models.SaveBlog(blog) + this.Ctx.Redirect(302, "/") +} ``` DeleteController ```Go - type DeleteController struct { - beego.Controller - } +type DeleteController struct { + beego.Controller +} - func (this *DeleteController) Get() { - id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) - blog := models.GetBlog(id) - this.Data["Post"] = blog - models.DelBlog(blog) - this.Ctx.Redirect(302, "/") - } +func (this *DeleteController) Get() { + id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"]) + blog := models.GetBlog(id) + this.Data["Post"] = blog + models.DelBlog(blog) + this.Ctx.Redirect(302, "/") +} ``` ## model层 ```Go - package models +package models - import ( - "database/sql" - "github.com/astaxie/beedb" - _ "github.com/ziutek/mymysql/godrv" - "time" - ) +import ( + "database/sql" + "github.com/astaxie/beedb" + _ "github.com/ziutek/mymysql/godrv" + "time" +) - type Blog struct { - Id int `PK` - Title string - Content string - Created time.Time +type Blog struct { + Id int `PK` + Title string + Content string + Created time.Time +} + +func GetLink() beedb.Model { + db, err := sql.Open("mymysql", "blog/astaxie/123456") + if err != nil { + panic(err) } + orm := beedb.New(db) + return orm +} - func GetLink() beedb.Model { - db, err := sql.Open("mymysql", "blog/astaxie/123456") - if err != nil { - panic(err) - } - orm := beedb.New(db) - return orm - } +func GetAll() (blogs []Blog) { + db := GetLink() + db.FindAll(&blogs) + return +} - func GetAll() (blogs []Blog) { - db := GetLink() - db.FindAll(&blogs) - return - } +func GetBlog(id int) (blog Blog) { + db := GetLink() + db.Where("id=?", id).Find(&blog) + return +} - func GetBlog(id int) (blog Blog) { - db := GetLink() - db.Where("id=?", id).Find(&blog) - return - } +func SaveBlog(blog Blog) (bg Blog) { + db := GetLink() + db.Save(&blog) + return bg +} - func SaveBlog(blog Blog) (bg Blog) { - db := GetLink() - db.Save(&blog) - return bg - } - - func DelBlog(blog Blog) { - db := GetLink() - db.Delete(&blog) - return - } +func DelBlog(blog Blog) { + db := GetLink() + db.Delete(&blog) + return +} ``` ## view层 @@ -200,75 +200,75 @@ DeleteController layout.tpl ```html - - - My Blog - - - + + + My Blog + + + - + - {{.LayoutContent}} +{{.LayoutContent}} - - + + ``` index.tpl ```html -

Blog posts

+

Blog posts

- + ``` view.tpl ```html -

{{.Post.Title}}

- {{.Post.Created}}
+

{{.Post.Title}}

+{{.Post.Created}}
- {{.Post.Content}} +{{.Post.Content}} ``` new.tpl ```html -

New Blog Post

-
- 标题:
- 内容: - -
+

New Blog Post

+
+标题:
+内容: + +
``` edit.tpl ```html -

Edit {{.Post.Title}}

+

Edit {{.Post.Title}}

-

New Blog Post

-
- 标题:
- 内容: - - -
+

New Blog Post

+
+标题:
+内容: + + +
``` ## links * [目录]()