PageRenderTime 45ms CodeModel.GetById 32ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/models/pages.go

http://github.com/TheOnly92/bloody.go
Go | 119 lines | 105 code | 13 blank | 1 comment | 30 complexity | 2cca9e0c5e56f0380aa24a05f02e6c68 MD5 | raw file
  1package main
  2
  3import (
  4	"launchpad.net/gobson/bson"
  5	"launchpad.net/mgo"
  6	"time"
  7	//"math"
  8)
  9
 10type Page struct {
 11	Id			bson.ObjectId "_id,omitempty"
 12	Title		string
 13	Slug		string
 14	Content		string
 15	Created		int64
 16	Modified	int64
 17}
 18
 19type PageModel struct {
 20	c			mgo.Collection
 21}
 22
 23func PageModelInit() *PageModel {
 24	p := new(PageModel)
 25	p.c = mSession.DB(config.Get("mongodb")).C("pages")
 26	return p
 27}
 28
 29func (page *PageModel) Sidebar() []map[string]string {
 30	var result *Page
 31	results := []map[string]string{}
 32	callback := func() error {
 33		results = append(results, map[string]string {"Title": result.Title, "Slug": result.Slug})
 34		return nil
 35	}
 36	err := page.c.Find(nil).Sort(bson.M{"title":-1}).For(&result, callback)
 37	if err != nil {
 38		panic(err)
 39	}
 40	return results
 41}
 42
 43func (page *PageModel) List() []map[string]string {
 44	var result *Page
 45	results := []map[string]string{}
 46	callback := func() error {
 47		t := time.Unix(result.Created, 0)
 48		results = append(results, map[string]string {"Title":result.Title, "Date":t.Format("2006 Jan 02 15:04"), "Id": objectIdHex(result.Id.String())})
 49		return nil
 50	}
 51	var err error
 52	err = page.c.Find(nil).Sort(bson.M{"timestamp":-1}).For(&result, callback)
 53	if err != nil {
 54		panic(err)
 55	}
 56	return results
 57}
 58
 59func (page *PageModel) Create(title string, content string) {
 60	t := time.Now()
 61	slug := toAscii(title)
 62	if page.GetBySlug(slug) != nil {
 63		slug += "-2"
 64	}
 65	err := page.c.Insert(&Page{"", title, slug, content, t.Unix(), 0})
 66	if err != nil {
 67		panic(err)
 68	}
 69}
 70
 71func (page *PageModel) Update(title string, content string, id string) {
 72	result := page.Get(id)
 73	result.Title = title
 74	slug := toAscii(title)
 75	tmp := page.GetBySlug(slug)
 76	if tmp != nil {
 77		if tmp.Id != result.Id {
 78			slug += "-2"
 79		}
 80	}
 81	result.Slug = slug
 82	result.Content = content
 83	result.Modified = time.Now().Unix()
 84	err := page.c.Update(bson.M{"_id":bson.ObjectIdHex(id)},result)
 85	if err != nil {
 86		panic(err)
 87	}
 88}
 89
 90func (page *PageModel) Delete(id string) {
 91	err := page.c.Remove(bson.M{"_id":bson.ObjectIdHex(id)})
 92	if err != nil {
 93		panic(err)
 94	}
 95}
 96
 97func (page *PageModel) Get(id string) *Page {
 98	var result *Page
 99	err := page.c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result)
100	if err != nil && err != mgo.NotFound {
101		panic(err)
102	} else if err == mgo.NotFound {
103		return nil
104	}
105	
106	return result
107}
108
109func (page *PageModel) GetBySlug(slug string) *Page {
110	var result *Page
111	err := page.c.Find(bson.M{"slug": slug}).One(&result)
112	if err != nil && err != mgo.NotFound {
113		panic(err)
114	} else if err == mgo.NotFound {
115		return nil
116	}
117	
118	return result
119}