-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
133 lines (100 loc) · 3.67 KB
/
app.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
package controllers
import (
"github.com/dchest/captcha"
"github.com/jgraham909/revmgo"
"github.com/revel/revel"
"github.com/timothyye/gocasts/app/models"
"golang.org/x/crypto/bcrypt"
"labix.org/v2/mgo/bson"
)
type App struct {
*revel.Controller
revmgo.MongoController
}
func (c App) ShowCast(id string) revel.Result {
t := models.Casts{}
c.MongoSession.DB("gocasts").C("casts").FindId(bson.ObjectIdHex(id)).One(&t)
cast := models.CastsView{Id: t.Id.Hex(), Author: t.Author, AuthorUrl: t.AuthorUrl,
VisitCount: t.VisitCount, Title: t.Title, Intro: t.Intro,
ShowNotes: t.ShowNotes, Url: t.Url, LogoUrl: t.LogoUrl, Date: t.Date, Tags: t.Tags}
return c.Render(cast)
}
func (c App) SearchTag(tag string) revel.Result {
casts := []models.Casts{}
viewCasts := []models.CastsView{}
c.MongoSession.DB("gocasts").C("casts").Find(bson.M{"tags": bson.RegEx{tag, ""}}).Sort("-date").All(&casts)
for _, t := range casts {
viewCasts = append(viewCasts,
models.CastsView{Id: t.Id.Hex(), Author: t.Author, AuthorUrl: t.AuthorUrl,
VisitCount: t.VisitCount, Title: t.Title, Intro: t.Intro,
ShowNotes: t.ShowNotes, Url: t.Url, LogoUrl: t.LogoUrl, Date: t.Date, Tags: t.Tags})
}
num := len(viewCasts)
pers := 12
pager := NewPaginator(c.Request.Request, pers, num)
return c.Render(viewCasts, pager, num, tag)
}
func (c App) Index() revel.Result {
num, _ := c.MongoSession.DB("gocasts").C("casts").Count()
pers := 6
pager := NewPaginator(c.Request.Request, pers, num)
casts := []models.Casts{}
viewCasts := []models.CastsView{}
c.MongoSession.DB("gocasts").C("casts").Find(nil).Limit(pers).Skip(pager.Offset()).Sort("-date").All(&casts)
for _, t := range casts {
viewCasts = append(viewCasts,
models.CastsView{Id: t.Id.Hex(), Author: t.Author, AuthorUrl: t.AuthorUrl,
VisitCount: t.VisitCount, Title: t.Title, Intro: t.Intro,
ShowNotes: t.ShowNotes, Url: t.Url, LogoUrl: t.LogoUrl, Date: t.Date})
}
return c.Render(viewCasts, pager, num)
}
func (c App) CastsList() revel.Result {
num, _ := c.MongoSession.DB("gocasts").C("casts").Count()
pers := 8
pager := NewPaginator(c.Request.Request, pers, num)
casts := []models.Casts{}
viewCasts := []models.CastsView{}
c.MongoSession.DB("gocasts").C("casts").Find(nil).Limit(pers).Skip(pager.Offset()).Sort("-date").All(&casts)
for _, t := range casts {
viewCasts = append(viewCasts,
models.CastsView{Id: t.Id.Hex(), Author: t.Author, AuthorUrl: t.AuthorUrl,
VisitCount: t.VisitCount, Title: t.Title, Intro: t.Intro,
ShowNotes: t.ShowNotes, Url: t.Url, LogoUrl: t.LogoUrl, Date: t.Date, Tags: t.Tags})
}
return c.Render(viewCasts, pager, num)
}
func (c App) About() revel.Result {
return c.Render()
}
func (c App) LoginView() revel.Result {
if c.Session["user"] != "" {
return c.Redirect(Admin.Index)
}
CaptchaId := captcha.NewLen(4)
return c.Render(CaptchaId)
}
func (c App) Login(username, password, captcha_id, captcha_value string) revel.Result {
c.Validation.Required(username).Message("请输入用户名")
c.Validation.Required(password).Message("请输入密码")
if !captcha.VerifyString(captcha_id, captcha_value) {
c.Validation.Error("验证码不正确")
}
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.LoginView)
}
id := models.Identity{}
c.MongoSession.DB("gocasts").C("id").Find(bson.M{"email": "admin@gocasts.net"}).One(&id)
if username == "admin@gocasts.net" && bcrypt.CompareHashAndPassword(id.Password, []byte(password)) == nil {
c.Session["user"] = "admin"
return c.Redirect(Admin.Index)
}
return c.Redirect(App.LoginView)
}
func (c App) Logout() revel.Result {
//Clear session
c.Session["user"] = ""
return c.Redirect(App.Index)
}