forked from UlricQin/beego-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_controller.go
111 lines (98 loc) · 2.51 KB
/
rss_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
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/validation"
"github.com/vincentruan/beego-blog/handler"
. "github.com/vincentruan/beego-blog/models"
"github.com/vincentruan/beego-blog/models/rss"
"github.com/vincentruan/beego-blog/utilities/mongo"
"gopkg.in/mgo.v2/bson"
"strconv"
"time"
)
type RSSController struct {
AdminController
}
func (this *RSSController) LoadPage() {
this.Data["IsRSS"] = true
this.Layout = "layout/admin.html"
this.TplNames = "rss/rss.html"
}
func (this *RSSController) Read() {
limit, offset := this.GetPaginationParam()
rfs := rss.SearchRssFeeders(this.GetString("search"), this.GetString("sort"), this.GetString("order"), limit, offset)
result := make(map[string]interface{})
result["total"] = len(rfs)
result["rows"] = rfs
this.Data["json"] = result
this.ServeJson()
}
func (this *RSSController) DoEdit() {
rf := RssFeeder{}
this.ParseForm(&rf)
result := make(map[string]interface{})
valid := validation.Validation{}
b, err := valid.Valid(&rf)
if err != nil {
result["status"] = "fail"
result["result"] = err.Error()
} else {
if b {
rf.UpdateTime = time.Now()
err = rss.Update(&rf, "rss_desc", "rss_url", "update_time")
if err != nil {
result["status"] = "fail"
result["result"] = err.Error()
} else {
beego.Debug("update RssFeeder[", rf.Id, "] success")
result["status"] = "success"
}
} else {
//验证不通过
result["status"] = "fail"
var errStr string
for _, err := range valid.Errors {
errStr += err.Key + err.Message
}
result["result"] = errStr
}
}
this.Data["json"] = result
this.ServeJson()
}
func (this *RSSController) DoDel() {
id, err := this.GetInt64("id")
result := make(map[string]interface{})
if err != nil {
result["status"] = "fail"
result["result"] = err.Error()
} else {
rf := RssFeeder{Id: id}
err = rss.Del(&rf)
if err != nil {
result["status"] = "fail"
result["result"] = err.Error()
} else {
beego.Debug("remove RssFeeder[", id, "] success")
result["status"] = "success"
}
}
this.Data["json"] = result
this.ServeJson()
}
func (this *RSSController) RSSData() {
id, _ := strconv.ParseInt(this.Ctx.Input.Param(":id"), 10, 64)
session, err := mongo.CopyMonotonicSession()
if err != nil {
beego.Error(err)
}
defer mongo.CloseSession(session)
c := session.DB("beego_blog").C("rssdata")
rd := handler.RSSData{}
err = c.Find(bson.M{"rssfeeder.id": id}).One(&rd)
if err != nil {
beego.Error(err)
}
this.Data["json"] = rd
this.ServeJson()
}