forked from UlricQin/beego-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_controller.go
152 lines (127 loc) · 3.81 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"github.com/vincentruan/beego-blog/engine"
"github.com/vincentruan/beego-blog/g"
"github.com/vincentruan/beego-blog/models"
"github.com/vincentruan/beego-blog/models/blog"
"github.com/vincentruan/beego-blog/models/catalog"
"time"
)
type MainController struct {
BaseController
}
func (this *MainController) Get() {
catalogs := catalog.All()
this.Data["Catalogs"] = catalogs
this.Data["PageTitle"] = "首页"
this.Data["CatalogSize"] = len(catalogs)
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
}
if !this.IsAdmin && blog.IsAuth(b.Id) {
this.Ctx.WriteString("You do not have permission!")
}
beego.BeeLogger.Debug("User read blog [%d] [%s]", b.Id, b.Title)
//in here blog cache must be set
if bg := g.BlogCacheGet(fmt.Sprintf("%d", b.Id)); bg != nil {
b1 := bg.(models.Blog)
b1.Views = b1.Views + 1
b.Views = b1.Views
g.BlogCachePut(fmt.Sprintf("%d", b.Id), b1)
}
//b.Views = b.Views + 1
//blog.Update(b, "")
this.Data["Blog"] = b
beginT := time.Now()
if content := g.BlogContentCacheGet(b.Id); content != "" {
beego.Debug("Get blog content by cache in ", time.Since(beginT))
this.Data["Content"] = content
} else {
beginT = time.Now()
content = g.RenderMarkdown(blog.ReadBlogContent(b).Content)
beego.Debug("Get blog content by render in ", time.Since(beginT))
g.BlogContentCachePut(b.Id, content)
this.Data["Content"] = content
}
//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
}
if c.IsAuth && !this.IsAdmin {
beego.Debug("User can NOT access this page as verification is need!")
this.SetPaginator(10, 0)
this.Data["Catalog"] = c
this.Data["Blogs"] = make([]*models.Blog, 0, 0)
this.Data["PageTitle"] = c.Name
} else {
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"
}
func (this *MainController) Query() {
this.Data["PageTitle"] = "搜索博客"
this.Data["BlogTitle"] = "Vincent"
this.Layout = "layout/default.html"
this.TplNames = "so/so.html"
}
func (this *MainController) DoQuery() {
query := this.GetString("q")
beego.BeeLogger.Debug("Search [%s]", query)
var result []models.Blog
if query != "" {
result = engine.SearchResult(query, this.IsAdmin)
} else {
result = make([]models.Blog, 0)
}
beego.BeeLogger.Debug("查询结果 %d 条", len(result))
this.Data["json"] = result
this.ServeJson()
}
func (this *MainController) ElasticQuery() {
this.Data["PageTitle"] = "搜索博客"
this.Data["BlogTitle"] = "Vincent"
this.Layout = "layout/default.html"
this.TplNames = "so/query.html"
}
func (this *MainController) DoElasticQuery() {
query := this.GetString("q")
beego.BeeLogger.Debug("Search [%s]", query)
var result []models.Blog
if query != "" {
result = engine.ElasticSearch(query, this.IsAdmin)
} else {
result = make([]models.Blog, 0)
}
beego.BeeLogger.Debug("查询结果 %d 条", len(result))
this.Data["json"] = result
this.ServeJson()
}