优化代码
This commit is contained in:
parent
b453910f82
commit
562520c080
|
@ -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)
|
||||
|
|
|
@ -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{
|
||||
|
|
2
internal/pkg/cache/feed.go
vendored
2
internal/pkg/cache/feed.go
vendored
|
@ -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
|
||||
})
|
||||
|
|
17
internal/pkg/cache/posts.go
vendored
17
internal/pkg/cache/posts.go
vendored
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
{"tt.count", ">", "0", "int"},
|
||||
{"tt.taxonomy", "in", ""},
|
||||
}, "t.term_id", "", model.SqlBuilder{
|
||||
{"t.name", "asc"},
|
||||
}, model.SqlBuilder{
|
||||
}),
|
||||
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"},
|
||||
}, nil, 0, in)
|
||||
}),
|
||||
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"}}),
|
||||
))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user