Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
0
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
		}
	}
}
Ejemplo n.º 4
0
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
}
Ejemplo n.º 5
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)
	}
}