forked from UlricQin/beego-blog
/
main_controller.go
64 lines (52 loc) · 1.46 KB
/
main_controller.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
package controllers
import (
"github.com/ulricqin/beego-blog/g"
"github.com/ulricqin/beego-blog/models/blog"
"github.com/ulricqin/beego-blog/models/catalog"
)
type MainController struct {
BaseController
}
func (this *MainController) Get() {
this.Data["Catalogs"] = catalog.All()
this.Data["PageTitle"] = "首页"
this.Layout = "layout/default.html"
this.TplNames = "index.html"
}
func (this *MainController) Read() {
ident := this.GetString(":ident")
b := blog.OneByIdent(ident)
if b == nil {
this.Ctx.WriteString("no such article")
return
}
b.Views = b.Views + 1
blog.Update(b, "")
this.Data["Blog"] = b
this.Data["Content"] = g.RenderMarkdown(blog.ReadBlogContent(b).Content)
this.Data["PageTitle"] = b.Title
this.Data["Catalog"] = catalog.OneById(b.CatalogId)
this.Layout = "layout/default.html"
this.TplNames = "article/read.html"
}
func (this *MainController) ListByCatalog() {
cata := this.Ctx.Input.Param(":ident")
if cata == "" {
this.Ctx.WriteString("catalog ident is blank")
return
}
limit := this.GetIntWithDefault("limit", 10)
c := catalog.OneByIdent(cata)
if c == nil {
this.Ctx.WriteString("catalog:" + cata + " not found")
return
}
ids := blog.Ids(c.Id)
pager := this.SetPaginator(limit, int64(len(ids)))
blogs := blog.ByCatalog(c.Id, pager.Offset(), limit)
this.Data["Catalog"] = c
this.Data["Blogs"] = blogs
this.Data["PageTitle"] = c.Name
this.Layout = "layout/default.html"
this.TplNames = "article/by_catalog.html"
}