forked from willnix/tinkerBlog
/
blog.go
75 lines (57 loc) · 1.86 KB
/
blog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/russross/blackfriday"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"strconv"
"time"
)
// struct for dbquery results
// mgo requires the string literal tags
type dbBlogEntry struct {
ObjId bson.ObjectId `_id,omitempty`
Id int `form:"id"`
Title string `form:"title"`
Author string `form:"author"`
Text string `form:"text"`
Written time.Time `form:"written"`
}
func BlogEntryList(ren render.Render, db *mgo.Database) {
var results []dbBlogEntry
// Load all Blogentries in the results slice
// (sorted descending according to id)
db.C("blogEntries").Find(nil).Sort("-id").All(&results)
for i, _ := range results {
results[i].Text = string(blackfriday.MarkdownCommon([]byte(results[i].Text)))
}
// render the template using the results from the db
ren.HTML(200, "blogEntryList", results)
}
func BlogEntry(ren render.Render, db *mgo.Database, args martini.Params) {
var result dbBlogEntry
Id, _ := strconv.Atoi(args["Id"])
// Find Blogentry by Id (should be only one)
db.C("blogEntries").Find(bson.M{"id": Id}).One(&result)
fmt.Println(string(blackfriday.MarkdownCommon([]byte(result.Text))))
result.Text = string(blackfriday.MarkdownCommon([]byte(result.Text)))
// render the template using the result from the db
ren.HTML(200, "blogEntry", result)
}
func addBlogEntrySubmit(blogEntry dbBlogEntry, ren render.Render, db *mgo.Database) {
blogEntry.Written = time.Now()
db.C("blogEntries").Insert(blogEntry)
// render the template using the result from the db
ren.HTML(200, "addBlogEntry", nil)
}
func addBlogEntry(ren render.Render) {
ren.HTML(200, "addBlogEntry", nil)
}
func About(ren render.Render) {
ren.HTML(200, "about", nil)
}
func Impressum(ren render.Render) {
ren.HTML(200, "impressum", nil)
}