/
article.go
96 lines (85 loc) · 2.04 KB
/
article.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package route
import (
"fmt"
"github.com/fuxiaohei/purine/src/mapi"
"github.com/fuxiaohei/purine/src/model"
"github.com/fuxiaohei/purine/src/route/base"
"github.com/lunny/tango"
"github.com/tango-contrib/xsrf"
)
var (
blogParamRule = "id/link"
pageParamRule = "link"
)
type Article struct {
base.ThemeRender
tango.Ctx
xsrf.Checker
}
func (a *Article) Get() {
param, err := mapi.Article.ParseRoute(blogParamRule, a.Param("*article"))
if err != nil {
a.RenderError(500, err)
return
}
// get article
res := mapi.Call(mapi.Article.Get, param)
if res.Status {
article := res.Data["article"].(*model.Article)
if article.Status != model.ARTICLE_STATUS_PUBLISH {
a.RenderError(404, nil)
return
}
a.Assign("Article", article)
a.Assign("Title", article.Title+" - "+a.GetGeneralByKey("title"))
// get comments
opt := &mapi.CommentListOption{
ArticleId: article.Id,
Page: 1,
Size: 999,
Status: model.COMMENT_STATUS_APPROVED,
}
res = mapi.Call(mapi.Comment.List, opt)
if res.Status {
comments := res.Data["comments"].([]*model.Comment)
a.Assign("Comments", comments)
}
prepareComment(article, a.ThemeRender)
a.Assign("XsrfHtml", a.XsrfFormHtml())
a.Render("article.tmpl")
return
}
// not found
a.RenderError(404, nil)
}
type Page struct {
base.ThemeRender
tango.Ctx
}
func (p *Page) Get() {
param, err := mapi.Page.ParseRoute(pageParamRule, p.Param("*page"))
if err != nil {
p.RenderError(500, err)
return
}
res := mapi.Call(mapi.Page.Get, param)
if res.Status {
page := res.Data["page"].(*model.Page)
if page.Status != model.PAGE_STATUS_PUBLISH {
p.RenderError(404, nil)
return
}
p.Assign("Page", page)
p.Title(page.Title + " - " + p.GetGeneralByKey("title"))
p.Render("page.tmpl")
return
}
p.RenderError(404, nil)
}
// prepare comment for theme
func prepareComment(v interface{}, r base.ThemeRender) {
if a, ok := v.(*model.Article); ok {
r.Assign("EnableComment", a.IsEnableComment())
r.Assign("CommentUrl", fmt.Sprintf("/comment/article/%d", a.Id))
}
}