13.5 Adding, deleting and updating blogs

We’ve already introduced the entire concept behind the Beego framework through examples and pseudo-code. This section will describe how to implement a blogging system using Beego, including the ability to browse, add, modify and delete blog posts.

Blog directory

Our blog’s directory structure can be seen below:

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

Blog routing

Our blog’s main routing rules are as follows:

  1. //Show blog Home
  2. beego.RegisterController("/", &controllers.IndexController{})
  3. //View blog details
  4. beego.RegisterController("/view/: id([0-9]+)", &controllers.ViewController{})
  5. //Create blog Bowen
  6. beego.RegisterController("/new", &controllers.NewController{})
  7. //Delete Bowen
  8. beego.RegisterController("/delete/: id([0-9]+)", &controllers.DeleteController{})
  9. //Edit Bowen
  10. beego.RegisterController("/edit/: id([0-9]+)", &controllers.EditController{})

Database structure

A trivial database table to store basic blog information:

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

Controller

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.TplNames = "index.tpl"
  8. }

ViewController:

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

NewController

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

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 layer

  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 layer

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. Title:<input type="text" name="title"><br>
  4. Content<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. Title:<input type="text" name="title" value="{{.Post.Title}}"><br>
  5. Content<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>