优化代码
This commit is contained in:
parent
09e99ac379
commit
eee0b75d19
@ -8,6 +8,13 @@ func ToAny[T any](v T) any {
|
||||
return v
|
||||
}
|
||||
|
||||
func Or[T any](is bool, left, right T) T {
|
||||
if is {
|
||||
return left
|
||||
}
|
||||
return right
|
||||
}
|
||||
|
||||
func StructColumnToSlice[T any, M any](arr []M, field string) (r []T) {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
v := reflect.ValueOf(arr[i]).FieldByName(field).Interface()
|
||||
|
@ -21,7 +21,7 @@ func Detail(c *gin.Context) {
|
||||
var err error
|
||||
recent := cache.RecentPosts(c, 5)
|
||||
archive := cache.Archives(c)
|
||||
categoryItems := cache.Categories(c)
|
||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||
recentComments := cache.RecentComments(c, 5)
|
||||
var ginH = gin.H{
|
||||
"title": wpconfig.Options.Value("blogname"),
|
||||
|
@ -141,7 +141,7 @@ func (h *indexHandle) parseParams() (err error) {
|
||||
category = h.c.Param("tag")
|
||||
if category != "" {
|
||||
h.scene = plugins.Tag
|
||||
allNames := cache.AllTagsNames(h.c)
|
||||
allNames := cache.AllCategoryTagsNames(h.c, plugins.Tag)
|
||||
if _, ok := allNames[category]; !ok {
|
||||
return errors.New(str.Join("not exists tag ", category))
|
||||
}
|
||||
@ -150,7 +150,7 @@ func (h *indexHandle) parseParams() (err error) {
|
||||
}
|
||||
} else {
|
||||
h.scene = plugins.Category
|
||||
allNames := cache.AllCategoryNames(h.c)
|
||||
allNames := cache.AllCategoryTagsNames(h.c, plugins.Category)
|
||||
if _, ok := allNames[category]; !ok {
|
||||
return errors.New(str.Join("not exists category ", category))
|
||||
}
|
||||
@ -237,7 +237,7 @@ func Index(c *gin.Context) {
|
||||
var err error
|
||||
archive := cache.Archives(c)
|
||||
recent := cache.RecentPosts(c, 5)
|
||||
categoryItems := cache.Categories(c)
|
||||
categoryItems := cache.CategoriesTags(c, plugins.Category)
|
||||
recentComments := cache.RecentComments(c, 5)
|
||||
ginH := gin.H{
|
||||
"err": err,
|
||||
|
36
internal/pkg/cache/cache.go
vendored
36
internal/pkg/cache/cache.go
vendored
@ -3,11 +3,13 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/cache"
|
||||
"github.com/fthvgb1/wp-go/helper"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/dao"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -155,39 +157,21 @@ func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
|
||||
return c.data
|
||||
}
|
||||
|
||||
func Categories(ctx context.Context) []models.TermsMy {
|
||||
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return my.Taxonomy == "category"
|
||||
})
|
||||
if len(t) > 0 {
|
||||
return slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return helper.Or(t[0] == plugins.Tag, "post_tag", "category") == my.Taxonomy
|
||||
})
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func Tags(ctx context.Context) []models.TermsMy {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
r = slice.Filter(r, func(my models.TermsMy) bool {
|
||||
return my.Taxonomy == "post_tag"
|
||||
})
|
||||
return r
|
||||
}
|
||||
func AllTagsNames(ctx context.Context) map[string]struct{} {
|
||||
func AllCategoryTagsNames(ctx context.Context, c int) map[string]struct{} {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||
if t.Taxonomy == "post_tag" {
|
||||
return t.Name, struct{}{}, true
|
||||
}
|
||||
return "", struct{}{}, false
|
||||
})
|
||||
}
|
||||
|
||||
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
||||
logs.ErrPrintln(err, "get category err")
|
||||
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
||||
if t.Taxonomy == "category" {
|
||||
if helper.Or(c == plugins.Tag, "post_tag", "category") == t.Taxonomy {
|
||||
return t.Name, struct{}{}, true
|
||||
}
|
||||
return "", struct{}{}, false
|
||||
|
@ -139,23 +139,16 @@ func (h handle) bodyClass() string {
|
||||
s := ""
|
||||
switch h.scene {
|
||||
case plugins.Search:
|
||||
s = "search-no-results"
|
||||
if len(h.ginH["posts"].([]models.Posts)) > 0 {
|
||||
s = "search-results"
|
||||
} else {
|
||||
s = "search-no-results"
|
||||
}
|
||||
case plugins.Category:
|
||||
case plugins.Category, plugins.Tag:
|
||||
cat := h.c.Param("category")
|
||||
_, cate := slice.SearchFirst(cache.Categories(h.c), func(my models.TermsMy) bool {
|
||||
return my.Name == cat
|
||||
})
|
||||
if cate.Slug[0] != '%' {
|
||||
s = cate.Slug
|
||||
if cat == "" {
|
||||
cat = h.c.Param("tag")
|
||||
}
|
||||
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
|
||||
case plugins.Tag:
|
||||
cat := h.c.Param("tag")
|
||||
_, cate := slice.SearchFirst(cache.Tags(h.c), func(my models.TermsMy) bool {
|
||||
_, cate := slice.SearchFirst(cache.CategoriesTags(h.c, h.scene), func(my models.TermsMy) bool {
|
||||
return my.Name == cat
|
||||
})
|
||||
if cate.Slug[0] != '%' {
|
||||
|
Loading…
Reference in New Issue
Block a user