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