|
|
|
@ -3,13 +3,15 @@ package wp
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/go-sonic/sonic/consts"
|
|
|
|
|
sonicconst "github.com/go-sonic/sonic/consts"
|
|
|
|
|
"github.com/go-sonic/sonic/log"
|
|
|
|
|
"github.com/go-sonic/sonic/model/dto/wp"
|
|
|
|
|
"github.com/go-sonic/sonic/model/entity"
|
|
|
|
|
"github.com/go-sonic/sonic/model/param"
|
|
|
|
|
"github.com/go-sonic/sonic/service"
|
|
|
|
|
"github.com/go-sonic/sonic/util"
|
|
|
|
|
"github.com/go-sonic/sonic/util/xerr"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
@ -24,50 +26,133 @@ func NewPostHandler(postService service.PostService) *PostHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (handler *PostHandler) List(ctx *gin.Context) (interface{}, error) {
|
|
|
|
|
var wpPostQuery param.WpPostQuery
|
|
|
|
|
if err := ctx.ShouldBind(&wpPostQuery); err != nil {
|
|
|
|
|
return nil, util.WrapJsonBindErr(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var postQuery param.PostQuery
|
|
|
|
|
postQuery.PageSize = wpPostQuery.Page
|
|
|
|
|
postQuery.PageNum = wpPostQuery.PerPage
|
|
|
|
|
entities, _, err := handler.PostService.Page(ctx, postQuery)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpPostList := make([]*wp.PostDTO, 0, len(entities))
|
|
|
|
|
for _, postEntity := range entities {
|
|
|
|
|
wpPostList = append(wpPostList, convertToWpPost(postEntity))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wpPostList, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (handler *PostHandler) Create(ctx *gin.Context) (interface{}, error) {
|
|
|
|
|
var wpPost param.WpPost
|
|
|
|
|
err := ctx.ShouldBindJSON(&wpPost)
|
|
|
|
|
postParam, err := parsePostParam(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, util.WrapJsonBindErr(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(wpPost)
|
|
|
|
|
create, err := handler.PostService.Create(ctx, postParam)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
log.CtxInfo(ctx, "wpPost: "+string(bytes))
|
|
|
|
|
return convertToWpPost(create), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postParam := convertToPostParam(&wpPost)
|
|
|
|
|
func (handler *PostHandler) Update(ctx *gin.Context) (interface{}, error) {
|
|
|
|
|
postParam, err := parsePostParam(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, util.WrapJsonBindErr(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create, err := handler.PostService.Create(ctx, postParam)
|
|
|
|
|
postIDStr := ctx.Param("postID")
|
|
|
|
|
postID, err := strconv.ParseInt(postIDStr, 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg("Parameter error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postDetail, err := handler.PostService.Update(ctx, int32(postID), postParam)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertToWpPost(create), nil
|
|
|
|
|
return convertToWpPost(postDetail), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (handler *PostHandler) Delete(ctx *gin.Context) (interface{}, error) {
|
|
|
|
|
postIDStr := ctx.Param("postID")
|
|
|
|
|
postID, err := strconv.ParseInt(postIDStr, 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg("Parameter error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postEntity, err := handler.PostService.GetByPostID(ctx, int32(postID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = handler.PostService.Delete(ctx, int32(postID)); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertToWpPost(postEntity), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsePostParam(ctx *gin.Context) (*param.Post, error) {
|
|
|
|
|
var wpPost param.WpPost
|
|
|
|
|
err := ctx.ShouldBindJSON(&wpPost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, util.WrapJsonBindErr(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(wpPost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
log.CtxInfo(ctx, "wpPost: "+string(bytes))
|
|
|
|
|
|
|
|
|
|
return convertToPostParam(&wpPost)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertToPostParam(wpPost *param.WpPost) *param.Post {
|
|
|
|
|
var paramPostStatus = consts.PostStatusPublished
|
|
|
|
|
if strings.ToLower(wpPost.Content) == "draft" {
|
|
|
|
|
paramPostStatus = consts.PostStatusDraft
|
|
|
|
|
func convertToPostParam(wpPost *param.WpPost) (*param.Post, error) {
|
|
|
|
|
var paramPostStatus = sonicconst.PostStatusPublished
|
|
|
|
|
if strings.ToLower(wpPost.Status) == "draft" {
|
|
|
|
|
paramPostStatus = sonicconst.PostStatusDraft
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createTime := time.Now().Unix()
|
|
|
|
|
editorType := sonicconst.EditorTypeRichText
|
|
|
|
|
disallowComment := false
|
|
|
|
|
if strings.ToLower(wpPost.CommentStatus) == "closed" {
|
|
|
|
|
disallowComment = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var createTime *int64
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(wpPost.Date) != "" {
|
|
|
|
|
datetime, err := time.Parse(sonicconst.LocalDateTimeFormat, wpPost.Date)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
dateTimeMills := datetime.UnixMilli()
|
|
|
|
|
createTime = &dateTimeMills
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ¶m.Post{
|
|
|
|
|
Title: wpPost.Title,
|
|
|
|
|
Status: paramPostStatus,
|
|
|
|
|
Slug: wpPost.Slug,
|
|
|
|
|
EditorType: nil,
|
|
|
|
|
EditorType: &editorType,
|
|
|
|
|
OriginalContent: wpPost.Content,
|
|
|
|
|
Summary: "",
|
|
|
|
|
Thumbnail: "",
|
|
|
|
|
DisallowComment: false,
|
|
|
|
|
DisallowComment: disallowComment,
|
|
|
|
|
Password: wpPost.Password,
|
|
|
|
|
Template: "",
|
|
|
|
|
TopPriority: 0,
|
|
|
|
|
CreateTime: &createTime,
|
|
|
|
|
CreateTime: createTime,
|
|
|
|
|
MetaKeywords: "",
|
|
|
|
|
MetaDescription: "",
|
|
|
|
|
TagIDs: make([]int32, 0),
|
|
|
|
@ -76,7 +161,7 @@ func convertToPostParam(wpPost *param.WpPost) *param.Post {
|
|
|
|
|
Content: wpPost.Content,
|
|
|
|
|
EditTime: nil,
|
|
|
|
|
UpdateTime: nil,
|
|
|
|
|
}
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertToWpPost(postEntity *entity.Post) *wp.PostDTO {
|
|
|
|
@ -85,7 +170,7 @@ func convertToWpPost(postEntity *entity.Post) *wp.PostDTO {
|
|
|
|
|
var wpCommentStatus = "open"
|
|
|
|
|
var wpContent = make(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
if postEntity.Status == consts.PostStatusDraft {
|
|
|
|
|
if postEntity.Status == sonicconst.PostStatusDraft {
|
|
|
|
|
wpStatus = "draft"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,15 +187,15 @@ func convertToWpPost(postEntity *entity.Post) *wp.PostDTO {
|
|
|
|
|
Guid: nil,
|
|
|
|
|
Id: postEntity.ID,
|
|
|
|
|
Link: "",
|
|
|
|
|
Modified: postEntity.UpdateTime.Format(timeFormat),
|
|
|
|
|
ModifiedGmt: postEntity.UpdateTime.UTC().Format(timeFormat),
|
|
|
|
|
Modified: "",
|
|
|
|
|
ModifiedGmt: "",
|
|
|
|
|
Slug: "",
|
|
|
|
|
Status: wpStatus,
|
|
|
|
|
Type: "post",
|
|
|
|
|
Password: "standard",
|
|
|
|
|
PermalinkTemplate: "",
|
|
|
|
|
GeneratedSlug: "",
|
|
|
|
|
Title: "",
|
|
|
|
|
Title: postEntity.Title,
|
|
|
|
|
Content: wpContent,
|
|
|
|
|
Author: 0,
|
|
|
|
|
Excerpt: nil,
|
|
|
|
@ -124,5 +209,10 @@ func convertToWpPost(postEntity *entity.Post) *wp.PostDTO {
|
|
|
|
|
Categories: make([]int32, 0),
|
|
|
|
|
Tags: make([]int32, 0),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if postEntity.UpdateTime != nil {
|
|
|
|
|
postDTO.Modified = postEntity.UpdateTime.Format(timeFormat)
|
|
|
|
|
postDTO.ModifiedGmt = postEntity.UpdateTime.UTC().Format(timeFormat)
|
|
|
|
|
}
|
|
|
|
|
return postDTO
|
|
|
|
|
}
|
|
|
|
|