From 562520c0805c8b257691400e71baed3250318232 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 6 Feb 2023 20:50:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/actions/detail.go | 2 +- internal/actions/index.go | 2 +- internal/pkg/cache/feed.go | 2 +- internal/pkg/cache/posts.go | 17 ++++++++++++-- internal/pkg/dao/common.go | 39 ++++++++++++++++++--------------- internal/pkg/dao/postmeta.go | 12 +++++----- internal/pkg/dao/posts.go | 29 +++++++++++++++--------- internal/pkg/models/wp_posts.go | 17 ++++++-------- 8 files changed, 70 insertions(+), 50 deletions(-) diff --git a/internal/actions/detail.go b/internal/actions/detail.go index f7652f4..981c681 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -21,7 +21,7 @@ type detailHandler struct { func Detail(c *gin.Context) { var err error var post models.Posts - recent := cache.RecentPosts(c, 5) + recent := cache.RecentPosts(c, 5, true) archive := cache.Archives(c) categoryItems := cache.CategoriesTags(c, plugins.Category) recentComments := cache.RecentComments(c, 5) diff --git a/internal/actions/index.go b/internal/actions/index.go index c4c34c7..a0034b5 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -220,7 +220,7 @@ func Index(c *gin.Context) { var totalRaw int var err error archive := cache.Archives(c) - recent := cache.RecentPosts(c, 5) + recent := cache.RecentPosts(c, 5, true) categoryItems := cache.CategoriesTags(c, plugins.Category) recentComments := cache.RecentComments(c, 5) ginH := gin.H{ diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index 0231ba5..bc10635 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -46,7 +46,7 @@ func PostFeedCache() *cache.MapCache[string, string] { func feed(arg ...any) (xml []string, err error) { c := arg[0].(*gin.Context) - r := RecentPosts(c, 10) + r := RecentPosts(c, 10, true) ids := slice.Map(r, func(t models.Posts) uint64 { return t.Id }) diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index b12581f..928ce5b 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -6,6 +6,7 @@ import ( "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/plugins" "github.com/gin-gonic/gin" "time" ) @@ -42,11 +43,23 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) { return maxPostIdCache.GetCache(ctx, time.Second, ctx) } -func RecentPosts(ctx context.Context, n int) (r []models.Posts) { - r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx) +func RecentPosts(ctx context.Context, n int, password bool) (r []models.Posts) { + nn := n + if nn <= 5 { + nn = 10 + } + r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx, nn) if n < len(r) { r = r[:n] } + if password { + r = slice.Map(r, func(t models.Posts) models.Posts { + if t.PostPassword != "" { + plugins.PasswordProjectTitle(&t) + } + return t + }) + } logs.ErrPrintln(err, "get recent post") return } diff --git a/internal/pkg/dao/common.go b/internal/pkg/dao/common.go index 710e8ee..6907c61 100644 --- a/internal/pkg/dao/common.go +++ b/internal/pkg/dao/common.go @@ -2,7 +2,6 @@ package dao import ( "context" - "fmt" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/model" @@ -20,23 +19,21 @@ type PostContext struct { Next models.Posts } -func PasswordProjectTitle(post *models.Posts) { - if post.PostPassword != "" { - post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) - } -} - func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) { ctx := a[0].(context.Context) var in = []any{"category", "post_tag"} - terms, err = model.Find[models.TermsMy](ctx, model.SqlBuilder{ - {"tt.count", ">", "0", "int"}, - {"tt.taxonomy", "in", ""}, - }, "t.term_id", "", model.SqlBuilder{ - {"t.name", "asc"}, - }, model.SqlBuilder{ - {"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"}, - }, nil, 0, in) + terms, err = model.Finds[models.TermsMy](ctx, model.Conditions( + model.Where(model.SqlBuilder{ + {"tt.count", ">", "0", "int"}, + {"tt.taxonomy", "in", ""}, + }), + model.Fields("t.term_id"), + model.Order(model.SqlBuilder{{"t.name", "asc"}}), + model.Join(model.SqlBuilder{ + {"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"}, + }), + model.In(in), + )) for i := 0; i < len(terms); i++ { if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok { terms[i].Terms = v @@ -49,7 +46,13 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) { } func Archives(ctx context.Context) ([]models.PostArchive, error) { - return model.Find[models.PostArchive](ctx, model.SqlBuilder{ - {"post_type", "post"}, {"post_status", "publish"}, - }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", model.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, nil, 0) + return model.Finds[models.PostArchive](ctx, model.Conditions( + model.Where(model.SqlBuilder{ + {"post_type", "post"}, + {"post_status", "publish"}, + }), + model.Fields("YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts"), + model.Group("year,month"), + model.Order(model.SqlBuilder{{"year", "desc"}, {"month", "desc"}}), + )) } diff --git a/internal/pkg/dao/postmeta.go b/internal/pkg/dao/postmeta.go index ceb567c..dba8891 100644 --- a/internal/pkg/dao/postmeta.go +++ b/internal/pkg/dao/postmeta.go @@ -14,9 +14,10 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) r = make(map[uint64]map[string]any) ctx := args[0].(context.Context) ids := args[1].([]uint64) - rr, err := model.Find[models.PostMeta](ctx, model.SqlBuilder{ - {"post_id", "in", ""}, - }, "*", "", nil, nil, nil, 0, slice.ToAnySlice(ids)) + rr, err := model.Finds[models.PostMeta](ctx, model.Conditions( + model.Where(model.SqlBuilder{{"post_id", "in", ""}}), + model.In(slice.ToAnySlice(ids)), + )) if err != nil { return } @@ -24,6 +25,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) if _, ok := r[postmeta.PostId]; !ok { r[postmeta.PostId] = make(map[string]any) } + r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue if postmeta.MetaKey == "_wp_attachment_metadata" { metadata, err := plugins.UnPHPSerialize[models.WpAttachmentMetadata](postmeta.MetaValue) if err != nil { @@ -31,11 +33,7 @@ func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) continue } r[postmeta.PostId][postmeta.MetaKey] = metadata - - } else { - r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue } - } return } diff --git a/internal/pkg/dao/posts.go b/internal/pkg/dao/posts.go index 4f89f24..a767ed1 100644 --- a/internal/pkg/dao/posts.go +++ b/internal/pkg/dao/posts.go @@ -98,7 +98,11 @@ func SearchPostIds(args ...any) (ids PostIds, err error) { join := args[5].(model.SqlBuilder) postType := args[6].([]any) postStatus := args[7].([]any) - res, total, err := model.SimplePagination[models.Posts](ctx, where, "ID", "", page, limit, order, join, nil, postType, postStatus) + res, total, err := model.SimplePagination[models.Posts]( + ctx, where, "ID", + "", page, limit, order, + join, nil, postType, postStatus, + ) for _, posts := range res { ids.Ids = append(ids.Ids, posts.Id) } @@ -113,7 +117,10 @@ func SearchPostIds(args ...any) (ids PostIds, err error) { func GetMaxPostId(a ...any) (uint64, error) { ctx := a[0].(context.Context) - r, err := model.SimpleFind[models.Posts](ctx, model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID") + r, err := model.SimpleFind[models.Posts](ctx, + model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, + "max(ID) ID", + ) var id uint64 if len(r) > 0 { id = r[0].Id @@ -123,14 +130,16 @@ func GetMaxPostId(a ...any) (uint64, error) { func RecentPosts(a ...any) (r []models.Posts, err error) { ctx := a[0].(context.Context) - r, err = model.Find[models.Posts](ctx, model.SqlBuilder{{ - "post_type", "post", - }, {"post_status", "publish"}}, "ID,post_title,post_password", "", model.SqlBuilder{{"post_date", "desc"}}, nil, nil, 10) - for i, post := range r { - if post.PostPassword != "" { - PasswordProjectTitle(&r[i]) - } - } + num := a[1].(int) + r, err = model.Finds[models.Posts](ctx, model.Conditions( + model.Where(model.SqlBuilder{ + {"post_type", "post"}, + {"post_status", "publish"}, + }), + model.Fields("ID,post_title,post_password"), + model.Order(model.SqlBuilder{{"post_date", "desc"}}), + model.Limit(num), + )) return } diff --git a/internal/pkg/models/wp_posts.go b/internal/pkg/models/wp_posts.go index df3df27..10cc88a 100644 --- a/internal/pkg/models/wp_posts.go +++ b/internal/pkg/models/wp_posts.go @@ -3,6 +3,7 @@ package models import "time" type Posts struct { + post Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"` PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"` PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"` @@ -47,23 +48,19 @@ type PostThumbnail struct { OriginAttachmentData WpAttachmentMetadata } -func (w Posts) PrimaryKey() string { +type post struct { +} + +func (w post) PrimaryKey() string { return "ID" } -func (w Posts) Table() string { - return "wp_posts" -} - -func (w PostArchive) PrimaryKey() string { - return "ID" -} - -func (w PostArchive) Table() string { +func (w post) Table() string { return "wp_posts" } type PostArchive struct { + post Year string `db:"year"` Month string `db:"month"` Posts int `db:"posts"`