13.5 实现博客的增删改

前面介绍了beego框架实现的整体构思以及部分实现的伪代码,这小节介绍通过beego建立一个博客系统,包括博客浏览、添加、修改、删除等操作。

博客目录

博客目录如下所示:

  1. .
  2. ├── controllers
  3. ├── delete.go
  4. ├── edit.go
  5. ├── index.go
  6. ├── new.go
  7. └── view.go
  8. ├── main.go
  9. ├── models
  10. └── model.go
  11. └── views
  12. ├── edit.tpl
  13. ├── index.tpl
  14. ├── layout.tpl
  15. ├── new.tpl
  16. └── view.tpl

博客路由

博客主要的路由规则如下所示:

  1. //显示博客首页
  2. beego.Router("/", &controllers.IndexController{})
  3. //查看博客详细信息
  4. beego.Router("/view/:id([0-9]+)", &controllers.ViewController{})
  5. //新建博客博文
  6. beego.Router("/new", &controllers.NewController{})
  7. //删除博文
  8. beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{})
  9. //编辑博文
  10. beego.Router("/edit/:id([0-9]+)", &controllers.EditController{})

数据库结构

数据库设计最简单的博客信息

  1. CREATE TABLE entries (
  2. id INT AUTO_INCREMENT,
  3. title TEXT,
  4. content TEXT,
  5. created DATETIME,
  6. primary key (id)
  7. );

控制器

IndexController:

  1. type IndexController struct {
  2. beego.Controller
  3. }
  4. func (this *IndexController) Get() {
  5. this.Data["blogs"] = models.GetAll()
  6. this.Layout = "layout.tpl"
  7. this.TplName = "index.tpl"
  8. }

ViewController:

  1. type ViewController struct {
  2. beego.Controller
  3. }
  4. func (this *ViewController) Get() {
  5. id, _ := strconv.Atoi(this.Ctx.Input.Params()[":id"])
  6. this.Data["Post"] = models.GetBlog(id)
  7. this.Layout = "layout.tpl"
  8. this.TplName = "view.tpl"
  9. }

NewController

  1. type NewController struct {
  2. beego.Controller
  3. }
  4. func (this *NewController) Get() {
  5. this.Layout = "layout.tpl"
  6. this.TplName = "new.tpl"
  7. }
  8. func (this *NewController) Post() {
  9. inputs := this.Input()
  10. var blog models.Blog
  11. blog.Title = inputs.Get("title")
  12. blog.Content = inputs.Get("content")
  13. blog.Created = time.Now()
  14. models.SaveBlog(blog)
  15. this.Ctx.Redirect(302, "/")
  16. }

EditController

  1. type EditController struct {
  2. beego.Controller
  3. }
  4. func (this *EditController) Get() {
  5. id, _ := strconv.Atoi(this.Ctx.Input.Params()[":id"])
  6. this.Data["Post"] = models.GetBlog(id)
  7. this.Layout = "layout.tpl"
  8. this.TplName = "edit.tpl"
  9. }
  10. func (this *EditController) Post() {
  11. inputs := this.Input()
  12. var blog models.Blog
  13. blog.Id, _ = strconv.Atoi(inputs.Get("id"))
  14. blog.Title = inputs.Get("title")
  15. blog.Content = inputs.Get("content")
  16. blog.Created = time.Now()
  17. models.SaveBlog(blog)
  18. this.Ctx.Redirect(302, "/")
  19. }

DeleteController

  1. type DeleteController struct {
  2. beego.Controller
  3. }
  4. func (this *DeleteController) Get() {
  5. id, _ := strconv.Atoi(this.Ctx.Input.Params()[":id"])
  6. blog := models.GetBlog(id)
  7. this.Data["Post"] = blog
  8. models.DelBlog(blog)
  9. this.Ctx.Redirect(302, "/")
  10. }

model层

  1. package models
  2. import (
  3. "database/sql"
  4. "github.com/astaxie/beedb"
  5. _ "github.com/ziutek/mymysql/godrv"
  6. "time"
  7. )
  8. type Blog struct {
  9. Id int `PK`
  10. Title string
  11. Content string
  12. Created time.Time
  13. }
  14. func GetLink() beedb.Model {
  15. db, err := sql.Open("mymysql", "blog/astaxie/123456")
  16. if err != nil {
  17. panic(err)
  18. }
  19. orm := beedb.New(db)
  20. return orm
  21. }
  22. func GetAll() (blogs []Blog) {
  23. db := GetLink()
  24. db.FindAll(&blogs)
  25. return
  26. }
  27. func GetBlog(id int) (blog Blog) {
  28. db := GetLink()
  29. db.Where("id=?", id).Find(&blog)
  30. return
  31. }
  32. func SaveBlog(blog Blog) (bg Blog) {
  33. db := GetLink()
  34. db.Save(&blog)
  35. return bg
  36. }
  37. func DelBlog(blog Blog) {
  38. db := GetLink()
  39. db.Delete(&blog)
  40. return
  41. }

view层

layout.tpl

  1. <html>
  2. <head>
  3. <title>My Blog</title>
  4. <style>
  5. #menu {
  6. width: 200px;
  7. float: right;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <ul id="menu">
  13. <li><a href="/">Home</a></li>
  14. <li><a href="/new">New Post</a></li>
  15. </ul>
  16. {{.LayoutContent}}
  17. </body>
  18. </html>

index.tpl

  1. <h1>Blog posts</h1>
  2. <ul>
  3. {{range .blogs}}
  4. <li>
  5. <a href="/view/{{.Id}}">{{.Title}}</a>
  6. from {{.Created}}
  7. <a href="/edit/{{.Id}}">Edit</a>
  8. <a href="/delete/{{.Id}}">Delete</a>
  9. </li>
  10. {{end}}
  11. </ul>

view.tpl

  1. <h1>{{.Post.Title}}</h1>
  2. {{.Post.Created}}<br/>
  3. {{.Post.Content}}

new.tpl

  1. <h1>New Blog Post</h1>
  2. <form action="" method="post">
  3. 标题:<input type="text" name="title"><br>
  4. 内容:<textarea name="content" colspan="3" rowspan="10"></textarea>
  5. <input type="submit">
  6. </form>

edit.tpl

  1. <h1>Edit {{.Post.Title}}</h1>
  2. <h1>New Blog Post</h1>
  3. <form action="" method="post">
  4. 标题:<input type="text" name="title" value="{{.Post.Title}}"><br>
  5. 内容:<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea>
  6. <input type="hidden" name="id" value="{{.Post.Id}}">
  7. <input type="submit">
  8. </form>