-
Notifications
You must be signed in to change notification settings - Fork 0
/
article_ctrl.go
120 lines (109 loc) · 3.99 KB
/
article_ctrl.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
package admin
import (
"github.com/GoodHot/TinyCMS/common/ctrl"
"github.com/GoodHot/TinyCMS/model"
"github.com/GoodHot/TinyCMS/service"
"strings"
)
type AdminArticleCtrl struct {
ArticleService *service.ArticleService `ioc:"auto"`
TagService *service.TagService `ioc:"auto"`
}
func (s *AdminArticleCtrl) Page(ctx *ctrl.HTTPContext) error {
page := ctx.ParamInt("page")
query := &service.ArticlePageQuery{
Type: ctx.QueryParamInt("type"),
CategoryID: ctx.QueryParamInt("category"),
Keyword: ctx.QueryParam("keyword"),
TagID: ctx.QueryParamInt("tag"),
UserID: ctx.QueryParamInt("user"),
}
ctx.Put("page", s.ArticleService.Page(page, query))
return ctx.ResultOK()
}
type PublishForm struct {
Article *model.Article `json:"article"`
Content *model.ArticleContent `json:"content"`
Tags []*model.Tag `json:"tags"`
GetCover bool `json:"get_cover"`
GetNetImage bool `json:"get_net_image"`
GetDescription bool `json:"get_description"`
//ID int `json:"id"`
//Title string `json:"title"` // 标题
//SEOTitle string `json:"seo_title"` // SEO标题
//SEODescription string `json:"seo_description"` // SEO描述
//SEOKeyword string `json:"seo_keyword"` // SEO关键字
//Markdown string `json:"markdown"` // markdown代码
//Html string `json:"html"` // HTML代码
//Cover string `json:"cover"` // 封面图
//CategoryID int `json:"category_id"` // 分类
//Description string `json:"description"` // 内容描述
//Tags []string `json:"tags"` // 标签
//Type string `json:"type"` // 操作类型 [publish, draft]
//Visibility string `json:"visibility"` // 可见性 [public, private]
//Template string `json:"template"` // 页面模板
//Author uint `json:"author"` // 作者ID
}
func (s *AdminArticleCtrl) Publish(ctx *ctrl.HTTPContext) error {
publish := new(model.ArticlePublish)
if err := ctx.Bind(publish); err != nil {
return ctx.ResultErr(err.Error())
}
publish.Article.Title = strings.TrimSpace(publish.Article.Title)
if publish.Article.Title == "" {
publish.Article.Title = "未命名"
}
if err := s.ArticleService.Publish(publish, ctx.Admin.ID); err != nil {
return ctx.ResultErr(err.Error())
}
ctx.Put("article", publish)
return ctx.ResultOK()
}
func (s *AdminArticleCtrl) Count(ctx *ctrl.HTTPContext) error {
ctx.Put("category", s.ArticleService.NoCategoryCount())
ctx.Put("draft", s.ArticleService.DraftCount())
ctx.Put("total", s.ArticleService.TotalCount())
return ctx.ResultOK()
}
func (s *AdminArticleCtrl) Get(ctx *ctrl.HTTPContext) error {
id := ctx.ParamInt("id")
article := s.ArticleService.GetById(id)
if article == nil || article.Title == "" {
return ctx.ResultErr("article not exists")
}
content := s.ArticleService.GetContent(article.ContentTable, int(article.ContentID))
//tags := s.TagService.GetByArticleID(id)
//tagsName := []string{}
//if tags != nil && len(tags) > 0 {
// for _, v := range tags {
// tagsName = append(tagsName, v.Name)
// }
//}
//publish := &PublishForm{
// ID: int(article.ID),
// Title: article.Title,
// SEOTitle: article.SEOTitle,
// Markdown: content.Markdown,
// Html: content.Html,
// Cover: article.Cover,
// CategoryID: int(article.CategoryID),
// Description: article.Description,
// Tags: tagsName,
//}
ctx.Put("article", article)
ctx.Put("content", content)
return ctx.ResultOK()
}
type DeleteArticle struct {
IDS []int `json:"ids"`
}
func (s *AdminArticleCtrl) Delete(ctx *ctrl.HTTPContext) error {
ids := new(DeleteArticle)
if err := ctx.Bind(ids); err != nil {
return ctx.ResultErr(err.Error())
}
if err := s.ArticleService.Delete(ids.IDS); err != nil {
return ctx.ResultErr(err.Error())
}
return ctx.ResultOK()
}