# 13.5 Add, delete and update blogs Introduced in front beego framework to achieve the overall concept and the partial implementation of the pseudo- code, which subsections describe through beego build a blog system, including blog browse, add, modify, or delete operation. ## Blog directory Blog directories are as follows: /main.go /views: /view.tpl /new.tpl /layout.tpl /index.tpl /edit.tpl /models/model.go /controllers: /index.go /view.go /new.go /delete.go /edit.go ## Blog routing Blog main routing rules are as follows: //Show blog Home beego.RegisterController("/", &controllers.IndexController{}) //View blog details beego.RegisterController("/view/: id([0-9]+)", &controllers.ViewController{}) //Create blog Bowen beego.RegisterController("/new", &controllers.NewController{}) //Delete Bowen beego.RegisterController("/delete/: id([0-9]+)", &controllers.DeleteController{}) //Edit Bowen beego.RegisterController("/edit/: id([0-9]+)", &controllers.EditController{}) ## Database structure The easiest database design blog information CREATE TABLE entries ( id INT AUTO_INCREMENT, title TEXT, content TEXT, created DATETIME, primary key (id) ); ## Controller IndexController: type IndexController struct { beego.Controller } func (this *IndexController) Get() { this.Data["blogs"] = models.GetAll() this.Layout = "layout.tpl" this.TplNames = "index.tpl" } ViewController: type ViewController struct { beego.Controller } func (this *ViewController) Get() { inputs := this.Input() id, _ := strconv.Atoi(this.Ctx.Params[":id"]) this.Data["Post"] = models.GetBlog(id) this.Layout = "layout.tpl" this.TplNames = "view.tpl" } NewController type NewController struct { beego.Controller } 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, "/") } EditController type EditController struct { beego.Controller } func (this *EditController) Get() { inputs := this.Input() id, _ := strconv.Atoi(this.Ctx.Params[":id"]) this.Data["Post"] = models.GetBlog(id) this.Layout = "layout.tpl" this.TplNames = "new.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, "/") } DeleteController type DeleteController struct { beego.Controller } func (this *DeleteController) Get() { id, _ := strconv.Atoi(this.Ctx.Params[":id"]) this.Data["Post"] = models.DelBlog(id) this.Ctx.Redirect(302, "/") } ## Model layer package models 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 } 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 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 DelBlog(blog Blog) { db := GetLink() db.Delete(&blog) return } ## View layer layout.tpl My Blog {{.LayoutContent}} index.tpl

Blog posts

view.tpl

{{.Post.Title}}

{{.Post.Created}}
{{.Post.Content}} new.tpl

New Blog Post

Title:
Content
edit.tpl

Edit {{.Post.Title}}

New Blog Post

Title:
Content
## Links - [Directory](preface.md) - Previous section: [Logs and configurations](13.4.md) - Next section: [Summary](13.6.md)