Skip to content

Commit

Permalink
ContentType text
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbbbbbbbbbbba committed Apr 20, 2020
1 parent 158e4ec commit 153f6dd
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 695 deletions.
211 changes: 98 additions & 113 deletions server/controllers/admin/article_controller.go
Original file line number Diff line number Diff line change
@@ -1,113 +1,98 @@
package admin

import (
"strconv"
"strings"

"github.com/PuerkitoBio/goquery"

"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"

"bbs-go/controllers/render"
"bbs-go/model"
"bbs-go/services"
"bbs-go/services/cache"
)

type ArticleController struct {
Ctx iris.Context
}

func (c *ArticleController) GetBy(id int64) *simple.JsonResult {
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return simple.JsonData(t)
}

func (c *ArticleController) AnyList() *simple.JsonResult {
list, paging := services.ArticleService.FindPageByParams(simple.NewQueryParams(c.Ctx).
EqByReq("id").EqByReq("user_id").EqByReq("status").LikeByReq("title").PageByReq().Desc("id"))

var results []map[string]interface{}
for _, article := range list {
builder := simple.NewRspBuilderExcludes(article, "content")

// 用户
builder = builder.Put("user", render.BuildUserDefaultIfNull(article.UserId))

// 简介
if article.ContentType == model.ContentTypeMarkdown {
mr := simple.NewMd().Run(article.Content)
if len(article.Summary) == 0 {
builder.Put("summary", mr.SummaryText)
}
} else {
if len(article.Summary) == 0 {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(article.Content))
if err != nil {
builder.Put("summary", simple.GetSummary(doc.Text(), 256))
}
}
}

// 标签
tagIds := cache.ArticleTagCache.Get(article.Id)
tags := cache.TagCache.GetList(tagIds)
builder.Put("tags", render.BuildTags(tags))

results = append(results, builder.Build())
}

return simple.JsonData(&simple.PageResult{Results: results, Page: paging})
}

func (c *ArticleController) PostCreate() *simple.JsonResult {
return simple.JsonErrorMsg("为实现")
}

func (c *ArticleController) PostUpdate() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("entity not found")
}

simple.ReadForm(c.Ctx, t)

// 数据校验
if len(t.Title) == 0 {
return simple.JsonErrorMsg("标题不能为空")
}
if len(t.Content) == 0 {
return simple.JsonErrorMsg("内容不能为空")
}
if len(t.ContentType) == 0 {
return simple.JsonErrorMsg("请选择内容格式")
}

t.UpdateTime = simple.NowTimestamp()
err := services.ArticleService.Update(t)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}

return simple.JsonData(t)
}

func (c *ArticleController) PostDelete() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
err := services.ArticleService.Delete(id)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
package admin

import (
"strconv"

"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"

"bbs-go/common"
"bbs-go/controllers/render"
"bbs-go/services"
"bbs-go/services/cache"
)

type ArticleController struct {
Ctx iris.Context
}

func (c *ArticleController) GetBy(id int64) *simple.JsonResult {
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return simple.JsonData(t)
}

func (c *ArticleController) AnyList() *simple.JsonResult {
list, paging := services.ArticleService.FindPageByParams(simple.NewQueryParams(c.Ctx).
EqByReq("id").EqByReq("user_id").EqByReq("status").LikeByReq("title").PageByReq().Desc("id"))

var results []map[string]interface{}
for _, article := range list {
builder := simple.NewRspBuilderExcludes(article, "content")

// 用户
builder = builder.Put("user", render.BuildUserDefaultIfNull(article.UserId))

// 简介
builder.Put("summary", common.GetSummary(article.ContentType, article.Content))

// 标签
tagIds := cache.ArticleTagCache.Get(article.Id)
tags := cache.TagCache.GetList(tagIds)
builder.Put("tags", render.BuildTags(tags))

results = append(results, builder.Build())
}

return simple.JsonData(&simple.PageResult{Results: results, Page: paging})
}

func (c *ArticleController) PostCreate() *simple.JsonResult {
return simple.JsonErrorMsg("为实现")
}

func (c *ArticleController) PostUpdate() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("entity not found")
}

simple.ReadForm(c.Ctx, t)

// 数据校验
if len(t.Title) == 0 {
return simple.JsonErrorMsg("标题不能为空")
}
if len(t.Content) == 0 {
return simple.JsonErrorMsg("内容不能为空")
}
if len(t.ContentType) == 0 {
return simple.JsonErrorMsg("请选择内容格式")
}

t.UpdateTime = simple.NowTimestamp()
err := services.ArticleService.Update(t)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}

return simple.JsonData(t)
}

func (c *ArticleController) PostDelete() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
err := services.ArticleService.Delete(id)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
12 changes: 7 additions & 5 deletions server/controllers/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func BuildArticle(article *model.Article) *model.ArticleResponse {
if len(rsp.Summary) == 0 {
rsp.Summary = mr.SummaryText
}
} else {
} else if article.ContentType == model.ContentTypeHtml {
rsp.Content = template.HTML(BuildHtmlContent(article.Content))
if len(rsp.Summary) == 0 {
rsp.Summary = simple.GetSummary(article.Content, 256)
Expand Down Expand Up @@ -166,7 +166,7 @@ func BuildSimpleArticle(article *model.Article) *model.ArticleSimpleResponse {
mr := simple.NewMd(simple.MdWithTOC()).Run(article.Content)
rsp.Summary = mr.SummaryText
}
} else {
} else if article.ContentType == model.ContentTypeHtml {
if len(rsp.Summary) == 0 {
rsp.Summary = simple.GetSummary(simple.GetHtmlText(article.Content), 256)
}
Expand Down Expand Up @@ -408,9 +408,11 @@ func _buildComment(comment *model.Comment, buildQuote bool) *model.CommentRespon

if comment.ContentType == model.ContentTypeMarkdown {
markdownResult := simple.NewMd().Run(comment.Content)
ret.Content = template.HTML(BuildHtmlContent(markdownResult.ContentHtml))
ret.Content = BuildHtmlContent(markdownResult.ContentHtml)
} else if comment.ContentType == model.ContentTypeHtml {
ret.Content = BuildHtmlContent(comment.Content)
} else {
ret.Content = template.HTML(BuildHtmlContent(comment.Content))
ret.Content = comment.Content
}

if buildQuote && comment.QuoteId > 0 {
Expand Down Expand Up @@ -457,7 +459,7 @@ func BuildFavorite(favorite *model.Favorite) *model.FavoriteResponse {
rsp.Title = article.Title
if article.ContentType == model.ContentTypeMarkdown {
rsp.Content = common.GetMarkdownSummary(article.Content)
} else {
} else if article.ContentType == model.ContentTypeHtml {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(article.Content))
if err == nil {
text := doc.Text()
Expand Down
1 change: 1 addition & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ github.com/mlogclub/simple v1.0.57/go.mod h1:J5p18ObwMAb7pJ8nodcHuZrmU0G3bnoFX2V
github.com/mlogclub/simple v1.0.58/go.mod h1:J5p18ObwMAb7pJ8nodcHuZrmU0G3bnoFX2VEzeuj5Os=
github.com/mlogclub/simple v1.0.59 h1:XZ3FBJXErh8mbaAx6MMX1dZHAGZCBkOPoZ9u2KF8yP4=
github.com/mlogclub/simple v1.0.59/go.mod h1:J5p18ObwMAb7pJ8nodcHuZrmU0G3bnoFX2VEzeuj5Os=
github.com/mlogclub/simple v1.0.60 h1:2w6fBtNxbXVbCKuIgCWCMZY8KV9dbfQakYH/v8ZJrIs=
github.com/mlogclub/simple v1.0.60/go.mod h1:J5p18ObwMAb7pJ8nodcHuZrmU0G3bnoFX2VEzeuj5Os=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
1 change: 1 addition & 0 deletions server/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (

ContentTypeHtml = "html"
ContentTypeMarkdown = "markdown"
ContentTypeText = "text"

EntityTypeArticle = "article"
EntityTypeTopic = "topic"
Expand Down
4 changes: 2 additions & 2 deletions server/model/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ type CommentResponse struct {
User *UserInfo `json:"user"`
EntityType string `json:"entityType"`
EntityId int64 `json:"entityId"`
Content template.HTML `json:"content"`
Content string `json:"content"`
QuoteId int64 `json:"quoteId"`
Quote *CommentResponse `json:"quote"`
QuoteContent template.HTML `json:"quoteContent"`
QuoteContent string `json:"quoteContent"`
Status int `json:"status"`
CreateTime int64 `json:"createTime"`
}
Expand Down
Loading

0 comments on commit 153f6dd

Please sign in to comment.