优化
This commit is contained in:
parent
d88dd6b853
commit
1100b89312
|
@ -2,6 +2,7 @@ package actions
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
|
@ -18,7 +19,7 @@ import (
|
|||
func Detail(c *gin.Context) {
|
||||
var err error
|
||||
var post models.Posts
|
||||
recent := cache.RecentPosts(c, 5, true)
|
||||
recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle)
|
||||
archive := cache.Archives(c)
|
||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||
recentComments := cache.RecentComments(c, 5)
|
||||
|
@ -56,14 +57,14 @@ func Detail(c *gin.Context) {
|
|||
Stats: status,
|
||||
})
|
||||
}()
|
||||
ID := str.ToInteger[uint64](c.Param("id"), 0)
|
||||
id := str.ToInteger[uint64](c.Param("id"), 0)
|
||||
|
||||
maxId, err := cache.GetMaxPostId(c)
|
||||
logs.ErrPrintln(err, "get max post id")
|
||||
if ID > maxId || ID <= 0 || err != nil {
|
||||
if id > maxId || id <= 0 || err != nil {
|
||||
return
|
||||
}
|
||||
post, err = cache.GetPostById(c, ID)
|
||||
post, err = cache.GetPostById(c, id)
|
||||
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -221,7 +221,7 @@ func Index(c *gin.Context) {
|
|||
var totalRaw int
|
||||
var err error
|
||||
archive := cache.Archives(c)
|
||||
recent := cache.RecentPosts(c, 5, true)
|
||||
recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle)
|
||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||
recentComments := cache.RecentComments(c, 5)
|
||||
ginH := gin.H{
|
||||
|
|
6
internal/pkg/cache/comments.go
vendored
6
internal/pkg/cache/comments.go
vendored
|
@ -9,7 +9,11 @@ import (
|
|||
)
|
||||
|
||||
func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
||||
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx)
|
||||
nn := n
|
||||
if nn <= 5 {
|
||||
nn = 10
|
||||
}
|
||||
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx, nn)
|
||||
if len(r) > n {
|
||||
r = r[0:n]
|
||||
}
|
||||
|
|
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, true)
|
||||
r := RecentPosts(c, 10)
|
||||
ids := slice.Map(r, func(t models.Posts) uint64 {
|
||||
return t.Id
|
||||
})
|
||||
|
|
11
internal/pkg/cache/posts.go
vendored
11
internal/pkg/cache/posts.go
vendored
|
@ -6,7 +6,6 @@ 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"
|
||||
)
|
||||
|
@ -43,7 +42,7 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) {
|
|||
return maxPostIdCache.GetCache(ctx, time.Second, ctx)
|
||||
}
|
||||
|
||||
func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) {
|
||||
func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
|
||||
nn := n
|
||||
if nn <= 5 {
|
||||
nn = 10
|
||||
|
@ -52,14 +51,6 @@ func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) {
|
|||
if n < len(r) {
|
||||
r = r[:n]
|
||||
}
|
||||
if project {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
// param context.Context
|
||||
func RecentComments(a ...any) (r []models.Comments, err error) {
|
||||
ctx := a[0].(context.Context)
|
||||
n := a[1].(int)
|
||||
return model.Finds[models.Comments](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
{"comment_approved", "1"},
|
||||
|
@ -20,7 +21,7 @@ func RecentComments(a ...any) (r []models.Comments, err error) {
|
|||
model.Fields("comment_ID,comment_author,comment_post_ID,post_title"),
|
||||
model.Order(model.SqlBuilder{{"comment_date_gmt", "desc"}}),
|
||||
model.Join(model.SqlBuilder{{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}}),
|
||||
model.Limit(10),
|
||||
model.Limit(n),
|
||||
))
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,13 @@ func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r mode
|
|||
return
|
||||
}
|
||||
|
||||
func ProjectTitle(t models.Posts) models.Posts {
|
||||
if t.PostPassword != "" {
|
||||
plugins.PasswordProjectTitle(&t)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
|
||||
if post.PostExcerpt != "" {
|
||||
plugins.PostExcerpt(&post)
|
||||
|
|
Loading…
Reference in New Issue
Block a user