func DelBlog(id int) int { db := orm.NewOrm() blog := GetBlog(id) if _, err := db.Delete(&blog); err == nil { delete(blogMap, id) return 1 } return 0 }
func GetBlog(id int) (blog Blog) { blog, ok := blogMap[id] if !ok { db := orm.NewOrm() blog = Blog{Index: id} db.Read(&blog) } return blog }
func initMap() { if len(blogMap) == 0 { o := orm.NewOrm() var blog []Blog o.QueryTable("blog").All(&blog) for _, v := range blog { blogMap[v.Index] = v } } }
func SaveBlog(blog Blog) int { db := orm.NewOrm() blogs := Blog{Index: blog.Index} if db.Read(&blogs) == nil { blogs.Title = blog.Title blogs.Content = blog.Content blogs.Created = utils.GetNow() blogMap[blog.Index] = blogs db.Update(&blogs) return 1 } return 0 }
func SaveNew(blog Blog) { db := orm.NewOrm() var blogs Blog ok, id := true, 0 for ok { id = utils.GetRandId() _, ok = blogMap[id] } blogs.Index = id blogs.Title = blog.Title blogs.Content = blog.Content blogs.Created = utils.GetNow() blogMap[blogs.Index] = blogs _, err := db.Insert(&blogs) if err != nil { fmt.Println(err) } }