-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.go
180 lines (169 loc) · 5.23 KB
/
manage.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package user
import (
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/webx/application/dbschema"
hanlderArticle "github.com/admpub/webx/application/handler/backend/official/article"
"github.com/admpub/webx/application/middleware/sessdata"
modelArticle "github.com/admpub/webx/application/model/official/article"
)
func ListByCustomer(ctx echo.Context, customer *dbschema.OfficialCustomer) error {
m := modelArticle.NewArticle(ctx)
cond := &db.Compounds{
db.Cond{`owner_id`: customer.Id},
db.Cond{`owner_type`: `customer`},
}
listFilterer(ctx, cond, m)
cond.AddKV(`display`, `Y`)
sorts := common.Sorts(ctx, `official_common_article`, `-id`)
list, err := m.ListPage(cond, sorts...)
ctx.Set(`list`, list)
return err
}
func listFilterer(ctx echo.Context, cond *db.Compounds, m *modelArticle.Article) {
contype := ctx.Queryx(`contype`).String()
categoryID := ctx.Formx(`categoryId`).Uint()
title := ctx.Formx(`q`).String()
if len(title) > 0 {
cond.AddKV(`title`, title)
}
if categoryID > 0 {
cond.Add(db.Or(
db.Cond{`category1`: categoryID},
db.Cond{`category2`: categoryID},
db.Cond{`category3`: categoryID},
db.Cond{`category_id`: categoryID},
))
}
if len(contype) > 0 {
cond.AddKV(`contype`, contype)
}
tag := ctx.Formx(`tag`).String()
if len(tag) > 0 {
cond.Add(m.TagCond(tag))
}
size := ctx.Formx(`size`).Int()
if size < 1 {
ctx.Request().Form().Set(`size`, `20`)
}
}
// List 用户文章列表
func List(ctx echo.Context) error {
customer := sessdata.Customer(ctx)
var err error
m := modelArticle.NewArticle(ctx)
cond := &db.Compounds{
db.Cond{`owner_id`: customer.Id},
db.Cond{`owner_type`: `customer`},
}
listFilterer(ctx, cond, m)
sorts := common.Sorts(ctx, `official_common_article`, `-id`)
list, err := m.ListPage(cond, sorts...)
ctx.Set(`list`, list)
ctx.SetFunc(`getContypeName`, modelArticle.Contype.Get)
return ctx.Render(`article/user/list`, handler.Err(ctx, err))
}
func applyFormData(ctx echo.Context, m *dbschema.OfficialCommonArticle) {
m.CategoryId = ctx.Formx(`categoryId`).Uint()
m.Title = ctx.Formx(`title`).String()
m.Image = ctx.Formx(`image`).String()
m.ImageOriginal = ctx.Formx(`imageOriginal`).String()
m.Summary = ctx.Formx(`summary`).String()
m.Summary = ctx.Formx(`summary`).String()
m.Content = ctx.Formx(`content`).String()
m.Contype = ctx.Formx(`contype`).String()
m.Display = `N`
m.Tags = ctx.Formx(`tags`).String()
}
// Create 创建文章
func Create(ctx echo.Context) error {
sourceID := ``
sourceTable := ``
customer := sessdata.Customer(ctx)
var err error
m := modelArticle.NewArticle(ctx)
m.DisallowCreateTags = true
if ctx.IsPost() {
m.OwnerId = customer.Id
m.OwnerType = `customer`
applyFormData(ctx, m.OfficialCommonArticle)
_, err = m.Add()
if err != nil {
goto END
}
return ctx.Redirect(sessdata.URLFor(`/user/article/list`))
}
END:
hanlderArticle.SetArticleFormData(ctx, sourceID, sourceTable)
ctx.Set(`activeURL`, `/user/article/list`)
ctx.Set(`sourceId`, sourceID)
ctx.Set(`sourceTable`, sourceTable)
ctx.Set(`contypes`, modelArticle.Contype.Slice())
ctx.Set(`title`, ctx.T(`投稿`))
ctx.Set(`isEdit`, false)
return ctx.Render(`article/user/edit`, handler.Err(ctx, err))
}
// Edit 修改文章
func Edit(ctx echo.Context) error {
sourceID := ``
sourceTable := ``
id := ctx.Paramx(`id`).Uint64()
if id < 1 {
return ctx.NewError(code.InvalidParameter, `参数“%s”值无效`, `id`)
}
customer := sessdata.Customer(ctx)
m := modelArticle.NewArticle(ctx)
m.DisallowCreateTags = true
err := m.Get(nil, `id`, id)
if err != nil {
if err == db.ErrNoMoreRows {
err = ctx.NewError(code.DataNotFound, `文章不存在`)
}
return err
}
if m.OwnerType != `customer` || m.OwnerId != customer.Id {
return ctx.NewError(code.NonPrivileged, `越权操作!您没有权限修改此数据`)
}
if ctx.IsPost() {
applyFormData(ctx, m.OfficialCommonArticle)
err = m.Edit(nil, `id`, m.Id)
if err != nil {
goto END
}
common.SendOk(ctx, ctx.T(`修改成功`))
return ctx.Redirect(sessdata.URLFor(`/user/article/list`))
}
echo.StructToForm(ctx, m.OfficialCommonArticle, ``, echo.LowerCaseFirstLetter)
END:
hanlderArticle.SetArticleFormData(ctx, sourceID, sourceTable)
ctx.Set(`activeURL`, `/user/article/list`)
ctx.Set(`sourceId`, sourceID)
ctx.Set(`sourceTable`, sourceTable)
ctx.Set(`contypes`, modelArticle.Contype.Slice())
ctx.Set(`title`, ctx.T(`修改文章`))
ctx.Set(`isEdit`, true)
return ctx.Render(`article/user/edit`, common.Err(ctx, err))
}
// Delete 用户删除文章
func Delete(ctx echo.Context) error {
id := ctx.Paramx(`id`).Uint64()
if id < 1 {
return ctx.NewError(code.InvalidParameter, `参数“%s”值无效`, `id`)
}
customer := sessdata.Customer(ctx)
m := modelArticle.NewArticle(ctx)
err := m.Get(nil, `id`, id)
if err != nil {
if err == db.ErrNoMoreRows {
err = ctx.NewError(code.DataNotFound, `文章不存在`)
}
return err
}
if m.OwnerType != `customer` || m.OwnerId != customer.Id {
return ctx.NewError(code.NonPrivileged, `越权操作!您没有权限删除此数据`)
}
return ctx.Redirect(sessdata.URLFor(`/user/article/list`))
}