/
c-admin-friend-links.go
128 lines (114 loc) · 3.14 KB
/
c-admin-friend-links.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
package nut
import (
"net/http"
"time"
"github.com/astaxie/beego/orm"
)
// IndexAdminFriendLinks list all friend-links
// @router /admin/friend-links [get]
func (p *Plugin) IndexAdminFriendLinks() {
p.LayoutDashboard()
p.MustAdmin()
var items []FriendLink
if _, err := orm.NewOrm().QueryTable(new(FriendLink)).
OrderBy("sort_order").
All(&items, "id", "title", "home", "logo", "sort_order"); err != nil {
p.Abort(http.StatusInternalServerError, err)
}
p.Data["items"] = items
p.Data[TITLE] = Tr(p.Locale(), "nut.admin.friend-links.index.title")
p.TplName = "nut/admin/friend-links/index.html"
}
// NewAdminFriendLink new friend-link
// @router /admin/friend-links/new [get]
func (p *Plugin) NewAdminFriendLink() {
p.LayoutDashboard()
p.MustAdmin()
var item FriendLink
p.Data["item"] = item
p.Data[TITLE] = Tr(p.Locale(), "buttons.new")
p.SetSortOrders()
p.Data["action"] = p.URLFor("nut.Plugin.CreateAdminFriendLink")
p.TplName = "nut/admin/friend-links/form.html"
}
type fmFriendLink struct {
Home string `form:"home" valid:"Required"`
Logo string `form:"logo" valid:"Required"`
Title string `form:"title" valid:"Required"`
SortOrder int `form:"sortOrder"`
}
// CreateAdminFriendLink create
// @router /admin/friend-links [post]
func (p *Plugin) CreateAdminFriendLink() {
p.MustAdmin()
var fm fmFriendLink
err := p.ParseForm(&fm)
if err == nil {
_, err = orm.NewOrm().Insert(&FriendLink{
Home: fm.Home,
Logo: fm.Logo,
Title: fm.Title,
SortOrder: fm.SortOrder,
})
}
if p.Flash(nil, err) {
p.Redirect("nut.Plugin.IndexAdminFriendLinks")
} else {
p.Redirect("nut.Plugin.NewAdminFriendLink")
}
}
// EditAdminFriendLink edit
// @router /admin/friend-links/edit/:id [get]
func (p *Plugin) EditAdminFriendLink() {
p.LayoutDashboard()
p.MustAdmin()
id := p.Ctx.Input.Param(":id")
var item FriendLink
if err := orm.NewOrm().QueryTable(&item).
Filter("id", id).
One(&item); err != nil {
p.Abort(http.StatusInternalServerError, err)
}
p.Data[TITLE] = Tr(p.Locale(), "buttons.edit")
p.Data["action"] = p.URLFor("nut.Plugin.UpdateAdminFriendLink", ":id", id)
p.Data["item"] = item
p.SetSortOrders()
p.TplName = "nut/admin/friend-links/form.html"
}
// UpdateAdminFriendLink update
// @router /admin/friend-links/:id [post]
func (p *Plugin) UpdateAdminFriendLink() {
p.MustAdmin()
var fm fmFriendLink
id := p.Ctx.Input.Param(":id")
err := p.ParseForm(&fm)
if err == nil {
_, err = orm.NewOrm().QueryTable(new(FriendLink)).
Filter("id", id).
Update(orm.Params{
"home": fm.Home,
"title": fm.Title,
"logo": fm.Logo,
"sort_order": fm.SortOrder,
"updated_at": time.Now(),
})
}
if p.Flash(nil, err) {
p.Redirect("nut.Plugin.IndexAdminFriendLink")
} else {
p.Redirect("nut.Plugin.EditAdminFriendLink", ":id", id)
}
}
// DestroyAdminFriendLink remove
// @router /admin/friend-links/:id [delete]
func (p *Plugin) DestroyAdminFriendLink() {
p.MustAdmin()
_, err := orm.NewOrm().QueryTable(new(FriendLink)).
Filter("id", p.Ctx.Input.Param(":id")).
Delete()
if err != nil {
p.Abort(http.StatusOK, err)
}
p.Data["json"] = H{"ok": true}
p.ServeJSON()
}