Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了收藏页面的管理功能 #376

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a95d9dd
feat: make admin path configurable
Oct 25, 2023
106e6ff
merge: Merge branch 'master' of github.com:textworld/sonic
textworld Nov 14, 2023
56157ba
feat: add manage apis for ApplicationPassword
textworld Nov 15, 2023
b77bcce
feat: add manage apis for ApplicationPassword
textworld Nov 17, 2023
8abb450
merge: merge master
textworld Nov 18, 2023
a314f69
Merge branch 'master' into rest-api
textworld Nov 20, 2023
013b939
feat: add some api for wordpress
textworld Nov 22, 2023
8cdafbc
feat: add manage apis for ApplicationPassword
textworld Nov 23, 2023
f0c92e1
refactorï: remove unused code
textworld Nov 23, 2023
6d3f54e
style: golint
textworld Nov 30, 2023
8e6de2e
style: use goimport
textworld Nov 30, 2023
9ba3842
style: golintci
textworld Dec 1, 2023
ad38517
style: style all project
textworld Dec 4, 2023
3fcf41f
style: fix imports
textworld Dec 5, 2023
949ab17
Merge branch 'master' into main
textworld Dec 5, 2023
97d9540
feat: for save
textworld Dec 6, 2023
927be7b
feat: no application_password
textworld Dec 7, 2023
6012ea7
feat: add scrap
textworld Dec 26, 2023
790ef8c
feat: update theme remotely
textworld Dec 26, 2023
7bd6749
feat: update release-docker to textworld/sonic
textworld Dec 26, 2023
e360c70
feat: support http minio
textworld Jan 4, 2024
85b3890
Merge pull request #1 from textworld/minio_http
textworld Jan 4, 2024
d8fe47d
feat: scrap api
Jan 8, 2024
919f5bf
feat: 切换console子模块到main分支
Jan 9, 2024
66a1143
feat: yarn build
Jan 9, 2024
3902faf
Update remote URL for resources/console submodule
Jan 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add some api for wordpress
textworld committed Nov 22, 2023
commit 013b939ac7fcd99007d79059c371bb8c374325db
4 changes: 4 additions & 0 deletions consts/consts.go
Original file line number Diff line number Diff line change
@@ -46,3 +46,7 @@ var (
BuildTime string
BuildCommit string
)

const (
LocalDateTimeFormat = "2006-01-02T15:04:05"
)
2 changes: 1 addition & 1 deletion handler/content/wp/category.go
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ func (c *CategoryHandler) List(ctx *gin.Context) (interface{}, error) {
return nil, err
}

categoryDTOList := make([]*wp.CategoryDTO, len(categoryEntities))
categoryDTOList := make([]*wp.CategoryDTO, 0, len(categoryEntities))
for _, categoryEntity := range categoryEntities {
categoryDTOList = append(categoryDTOList, convertToCategoryDTO(categoryEntity))
}
1 change: 1 addition & 0 deletions handler/content/wp/init.go
Original file line number Diff line number Diff line change
@@ -7,5 +7,6 @@ func init() {
NewPostHandler,
NewUserHandler,
NewCategoryHandler,
NewTagHandler,
)
}
132 changes: 111 additions & 21 deletions handler/content/wp/post.go
Original file line number Diff line number Diff line change
@@ -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 &param.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
}
53 changes: 53 additions & 0 deletions handler/content/wp/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package wp

import (
"github.com/gin-gonic/gin"
"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"
)

type TagHandler struct {
TagService service.TagService
}

func NewTagHandler(tagService service.TagService) *TagHandler {
return &TagHandler{
TagService: tagService,
}
}

func (handler *TagHandler) List(ctx *gin.Context) (interface{}, error) {
var err error
var listParam param.TagListParam
if err = ctx.ShouldBindJSON(&listParam); err != nil {
return nil, err
}

entities, err := handler.TagService.ListByOption(ctx, &listParam)
if err != nil {
return nil, err
}

tagDTOList := make([]*wp.TagDTO, 0, len(entities))
for _, tagEntity := range entities {
tagDTOList = append(tagDTOList, convertToWpTag(tagEntity))
}

return tagDTOList, nil
}

func convertToWpTag(tagEntity *entity.Tag) *wp.TagDTO {
tagDTO := &wp.TagDTO{
ID: tagEntity.ID,
Count: 0,
Description: "",
Link: "",
Name: tagEntity.Name,
Slug: tagEntity.Slug,
Taxonomy: "",
Meta: nil,
}
return tagDTO
}
2 changes: 1 addition & 1 deletion handler/content/wp/user.go
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ func (u *UserHandler) List(ctx *gin.Context) (interface{}, error) {
return nil, err
}

userDTOList := make([]*dto.User, len(allUser))
userDTOList := make([]*dto.User, 0, len(allUser))
for _, user := range allUser {
userDTO := u.UserService.ConvertToDTO(ctx, user)
userDTOList = append(userDTOList, userDTO)
18 changes: 15 additions & 3 deletions handler/router.go
Original file line number Diff line number Diff line change
@@ -47,9 +47,21 @@ func (s *Server) RegisterRouters() {
{
wpCompatibleRouter := router.Group("/wp-json/wp/v2")
wpCompatibleRouter.Use(s.ApplicationPasswordMiddleware.GetWrapHandler())
wpCompatibleRouter.POST("/posts", s.wrapHandler(s.WpPostHandler.Create))
wpCompatibleRouter.GET("/users", s.wrapHandler(s.WpUserHandler.List))
wpCompatibleRouter.GET("/categories", s.wrapHandler(s.WpCategoryHandler.List))
{
wpCompatibleRouter.GET("/posts", s.wrapWpHandler(s.WpPostHandler.List))
wpCompatibleRouter.POST("/posts", s.wrapWpHandler(s.WpPostHandler.Create))
wpCompatibleRouter.POST("/posts/:postID", s.wrapWpHandler(s.WpPostHandler.Update))
wpCompatibleRouter.DELETE("/posts/:postID", s.wrapWpHandler(s.WpPostHandler.Delete))
}
{
wpCompatibleRouter.GET("/tags", s.wrapWpHandler(s.WpTagHandler.List))
}
{
wpCompatibleRouter.GET("/users", s.wrapWpHandler(s.WpUserHandler.List))
}
{
wpCompatibleRouter.GET("/categories", s.wrapWpHandler(s.WpCategoryHandler.List))
}
}
{
adminAPIRouter := router.Group("/api/admin")
18 changes: 18 additions & 0 deletions handler/server.go
Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@ type Server struct {
WpPostHandler *wp.PostHandler
WpUserHandler *wp.UserHandler
WpCategoryHandler *wp.CategoryHandler
WpTagHandler *wp.TagHandler
}

type ServerParams struct {
@@ -143,6 +144,7 @@ type ServerParams struct {
WpPostHandler *wp.PostHandler
WpUserHandler *wp.UserHandler
WpCategoryHandler *wp.CategoryHandler
WpTagHandler *wp.TagHandler
}

func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
@@ -213,6 +215,7 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
WpPostHandler: param.WpPostHandler,
WpUserHandler: param.WpUserHandler,
WpCategoryHandler: param.WpCategoryHandler,
WpTagHandler: param.WpTagHandler,
}
lifecycle.Append(fx.Hook{
OnStop: httpServer.Shutdown,
@@ -256,6 +259,21 @@ func (s *Server) wrapHandler(handler wrapperHandler) gin.HandlerFunc {
}
}

func (s *Server) wrapWpHandler(handler wrapperHandler) gin.HandlerFunc {
return func(ctx *gin.Context) {
data, err := handler(ctx)
if err != nil {
s.logger.Error("handler error", zap.Error(err))
status := xerr.GetHTTPStatus(err)
ctx.JSON(status, &dto.BaseWpDTO{Code: status, Message: xerr.GetMessage(err), Data: map[string]interface{}{"status": status}})
return
}

ctx.JSON(http.StatusOK, data)
return
}
}

type wrapperHTMLHandler func(ctx *gin.Context, model template.Model) (templateName string, err error)

var (
6 changes: 6 additions & 0 deletions model/dto/base.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,12 @@ type BaseDTO struct {
Data interface{} `json:"data"`
}

type BaseWpDTO struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

type Page struct {
Content interface{} `json:"content"`
Pages int `json:"pages"`
12 changes: 12 additions & 0 deletions model/dto/wp/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wp

type TagDTO struct {
ID int32 `json:"id"`
Count int32 `json:"count"`
Description string `json:"description"`
Link string `json:"link"`
Name string `json:"name"`
Slug string `json:"slug"`
Taxonomy string `json:"taxonomy"`
Meta map[string]interface{} `json:"meta"`
}
Loading