-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
479 lines (444 loc) · 11.9 KB
/
article.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package models
import (
"time"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
"strings"
"fmt"
)
//文章字段过滤器
const (
Filter_Create = "created"
Filter_Update = "updated"
Filter_ViewCount = "view_count"
Filter_LastReplyTime = "last_reply_time"
Filter_UserName = "author"
ArticlePerPageLimit = 10
)
var totalArticleCount int64
type Article struct {
Id int64
Title string `form:"title"`
Subtitle string `form:"subtitle"`
Content string `orm:"size(10000)" form:"content"`
Author *User `orm:"rel(fk)"`
Category string `form:"category"`
Created time.Time `orm:"index"`
Updated time.Time `orm:"index"`
ViewCount int64 `orm:"index"`
ReplyCount int64
LastReplyTime time.Time `orm:"index"`
LastReplyUserId int64
}
func initArticle() {
err := SetupTotalArticleCount()
if err != nil {
beego.Error(err)
}
}
func GetArticles(order_key string, category, tag string, uid int64, inverted bool, page int) (articles []*Article, article_count int, err error) {
o := orm.NewOrm()
query := o.QueryTable("article")
article_count = int(GetTotalArticleCount())
switch order_key {
case Filter_Create:
if len(category) > 0 {
query = query.Filter("category", category)
}
if len(tag) > 0 {
query = query.Filter("tag", tag)
}
if uid > 0 {
query = query.Filter("author", uid)
}
if inverted {
_, err = query.OrderBy("-" + Filter_Create).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
} else {
_, err = query.OrderBy(Filter_Create).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
}
if uid > 0 {
qs_user := o.QueryTable("article").Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if uid > 0 && len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category).Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
return articles, article_count, err
case Filter_LastReplyTime:
if len(category) > 0 {
query = query.Filter("category", category)
}
if len(tag) > 0 {
query = query.Filter("tag", tag)
}
if uid > 0 {
query = query.Filter("author", uid)
}
if inverted {
_, err = query.OrderBy("-" + Filter_LastReplyTime).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
} else {
_, err = query.OrderBy(Filter_LastReplyTime).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
}
if uid > 0 {
qs_user := o.QueryTable("article").Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if uid > 0 && len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category).Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
return articles, article_count, err
case Filter_ViewCount:
if len(category) > 0 {
query = query.Filter("category", category)
}
if len(tag) > 0 {
query = query.Filter("tag", tag)
}
if uid > 0 {
query = query.Filter("author", uid)
}
if inverted {
_, err = query.OrderBy("-" + Filter_ViewCount).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
} else {
_, err = query.OrderBy(Filter_ViewCount).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
}
if uid > 0 {
qs_user := o.QueryTable("article").Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if uid > 0 && len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category).Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
return articles, article_count, err
case Filter_Update:
if len(category) > 0 {
query = query.Filter("category", category)
}
if len(tag) > 0 {
query = query.Filter("tag", tag)
}
if uid > 0 {
query = query.Filter("author", uid)
}
if inverted {
_, err = query.OrderBy("-" + Filter_Update).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
} else {
_, err = query.OrderBy(Filter_Update).Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
}
if uid > 0 {
qs_user := o.QueryTable("article").Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
if uid > 0 && len(category) > 0 {
qs_user := o.QueryTable("article").Filter("Category", category).Filter("Author", uid)
x, err := qs_user.Count()
if err != nil {
return articles, article_count, err
}
article_count = int(x)
beego.Info("The user article count = ", article_count)
}
return articles, article_count, err
}
_, err = query.Limit(ArticlePerPageLimit).Offset((page - 1) * ArticlePerPageLimit).All(&articles)
article_count = len(articles)
return articles, article_count, err
}
func GetArticle(id int64) (*Article, error) {
o := orm.NewOrm()
query := o.QueryTable("article")
article := Article{}
err := query.Filter("id", id).One(&article)
if err != nil {
return nil, err
}
article.ViewCount++
o.Update(&article)
return &article, err
}
func AddArticle(article Article, tagsrt []string) error {
beego.Info(tagsrt)
o := orm.NewOrm()
var err error
//插入文章
_, err = o.Insert(&article)
if err != nil {
return err
}
//添加分类
category := Category{Name:article.Category}
if created, _, err := o.ReadOrCreate(&category, "Name"); err == nil {
if created {
category.ArticleCount = 1
} else {
category.ArticleCount++
_, err = o.Update(&category)
if err != nil {
return err
}
}
}
//添加Tag
var inexistTags []int64
for _, x := range tagsrt {
tag := Tag{Name:x}
_, _, err := o.ReadOrCreate(&tag, "Name")
if err != nil {
return err
} else {
inexistTags = append(inexistTags, tag.Id)
}
}
article_tmp := Article{}
qs_art := o.QueryTable("article").Filter("title", article.Title)
err = qs_art.One(&article_tmp)
if err != nil {
return err
}
for _, x := range inexistTags {
err = SaveArticleTag(article_tmp.Id, x)
if err != nil {
return err
}
}
return err
}
func ModifyArticle(article *Article, raw_former_tag string, raw_tags string) error {
o := orm.NewOrm()
modifyArticle := Article{
Id:article.Id,
}
err := o.Read(&modifyArticle)
formerCategory := modifyArticle.Category
if err == nil {
modifyArticle.Category = article.Category
modifyArticle.Title = article.Title
modifyArticle.Subtitle = article.Subtitle
modifyArticle.Content = article.Content
modifyArticle.Author = article.Author
modifyArticle.Updated = time.Now()
}
_, err = o.Update(&modifyArticle)
if err != nil {
return err
}
//处理分类
qs := o.QueryTable("category")
category := Category{}
if len(formerCategory) > 0 {
err = qs.Filter("name", formerCategory).One(&category)
if err == nil {
category.ArticleCount--
_, err = o.Update(&category)
}
}
err = qs.Filter("name", article.Category).One(&category)
if err == nil {
category.ArticleCount++
o.Update(&category)
} else {
if len(article.Category) > 0 {
category.Name = article.Category
category.Created = time.Now()
category.ArticleCount++
o.Insert(&category)
}
}
//FIXME 标签处理仍存在问题
//处理标签
former_tag := strings.Split(raw_former_tag, " ")
tags := strings.Split(raw_tags, " ")
beego.Info(former_tag)
beego.Info(tags)
var deleted_tag, created_tag []string
var str_tmp string
var flag bool = false
for _, x := range tags {
for _, y := range former_tag {
if y == x {
flag = true
str_tmp = y
break
}
}
if !flag {
deleted_tag = append(deleted_tag,str_tmp)
flag = false
}
}
beego.Info("deleta tag list = ",deleted_tag)
for _, x := range tags {
for a, y := range former_tag {
if y == x {
break
}
if a == len(tags) && y != x {
deleted_tag = append(created_tag, x)
}
}
}
beego.Info("the create list of ", created_tag)
beego.Info("the delete list of ", deleted_tag)
beego.Info("the raw create list of ", former_tag)
beego.Info("the raw delete list of ", tags)
return err
}
func DeleteArticle(id int64) error {
o := orm.NewOrm()
cate := Article{Id : id}
_, err := o.Delete(&cate)
if err != nil {
return err
}
var delete_list []ArticleTag
qs := o.QueryTable("article_tag").Filter("article_id", id)
_, err = qs.All(&delete_list)
if err != nil {
return err
}
for _, x := range delete_list {
_, err = o.Delete(&x)
if err != nil {
return err
}
}
return err
}
func FindArticles(key string, byTitle bool, bySubtitle bool, byCategory bool, byTag bool) (articles []*Article, err error) {
var conds []string
baseCond := fmt.Sprintf("article.%%s like '%%%%%s%%%%'", key)
// 按照标题检索
if byTitle {
conds = append(conds, fmt.Sprintf(baseCond, "title"))
}
// 按照副标题检索
if bySubtitle {
conds = append(conds, fmt.Sprintf(baseCond, "subtitle"))
}
// 按照分类检索
if byCategory {
conds = append(conds, fmt.Sprintf(baseCond, "category"))
}
if byTag {
conds = append(conds, fmt.Sprintf("tag.name like '%%%s%%'", key))
}
qb, err := orm.NewQueryBuilder("mysql")
if err != nil {
return
}
// 构造SQL语句
qb.Select("article.*").From("tag").
InnerJoin("(article INNER JOIN article_tag ON article.id = article_tag.article_id)").
On("tag.id = article_tag.tag_id")
if len(conds) > 0 {
qb.Where(conds[0])
}
for i := 1; i < len(conds); i++ {
qb.Or(conds[i])
}
sql := qb.String()
// 执行SQL语句
fmt.Println(sql)
o := orm.NewOrm()
o.Raw(sql).QueryRows(&articles)
RemoveDuplicates(&articles)
return
}
func RemoveDuplicates(articles *[]*Article) {
foundId := make(map[int64]bool)
j := 0
for i, x := range *articles {
if !foundId[x.Id] {
foundId[x.Id] = true
(*articles)[j] = (*articles)[i]
j++
}
}
*articles = (*articles)[:j]
}
func SetupTotalArticleCount() (err error) {
o := orm.NewOrm()
totalArticleCount, err = o.QueryTable("article").Count()
if err != nil {
return
}
beego.Info("Total article count = ", totalArticleCount)
return
}
func GetTotalArticleCount() (int64) {
return totalArticleCount
}