Compare commits

..

No commits in common. "b4cc570e8a23b87d03c2124298edb60550aed1c9" and "11e0c32c6f558d80d142b2fc7866cbbd97d3f0ac" have entirely different histories.

4 changed files with 23 additions and 119 deletions

View File

@ -1,7 +1,7 @@
package common package common
import ( import (
"github.com/fthvgb1/wp-go/helper/number" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
@ -17,7 +17,6 @@ var commonClass = map[int]string{
constraints.Category: "archive category ", constraints.Category: "archive category ",
constraints.Tag: "archive category ", constraints.Tag: "archive category ",
constraints.Search: "search ", constraints.Search: "search ",
constraints.Author: "archive author ",
constraints.Detail: "post-template-default single single-post ", constraints.Detail: "post-template-default single single-post ",
} }
@ -26,40 +25,38 @@ func (h *Handle) CalBodyClass() {
} }
func (h *Handle) BodyClass(class ...string) string { func (h *Handle) BodyClass(class ...string) string {
s := ""
if constraints.Ok != h.Stats { if constraints.Ok != h.Stats {
return "error404" return "error404"
} }
switch h.Scene { switch h.Scene {
case constraints.Search: case constraints.Search:
s := "search-no-results" s = "search-no-results"
if len(h.Index.Posts) > 0 { if len(h.GinH["posts"].([]models.Posts)) > 0 {
s = "search-results" s = "search-results"
} }
class = append(class, s)
case constraints.Category, constraints.Tag: case constraints.Category, constraints.Tag:
cat := h.Index.Param.Category cat := h.C.Param("category")
if cat == "" {
cat = h.C.Param("tag")
}
_, cate := slice.SearchFirst(cache.CategoriesTags(h.C, h.Scene), func(my models.TermsMy) bool { _, cate := slice.SearchFirst(cache.CategoriesTags(h.C, h.Scene), func(my models.TermsMy) bool {
return my.Name == cat return my.Name == cat
}) })
if cate.Slug[0] != '%' { if cate.Slug[0] != '%' {
class = append(class, str.Join("category-", cate.Slug)) s = cate.Slug
} }
class = append(class, str.Join("category-", number.ToString(cate.Terms.TermId))) s = fmt.Sprintf("category-%v category-%v", s, cate.Terms.TermId)
case constraints.Author:
author := h.Index.Param.Author
user, _ := cache.GetUserByName(h.C, author)
class = append(class, str.Join("author-", number.ToString(user.Id)))
if user.UserLogin[0] != '%' {
class = append(class, str.Join("author-", user.UserLogin))
}
case constraints.Detail: case constraints.Detail:
class = append(class, str.Join("postid-", number.ToString(h.Detail.Post.Id))) s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id)
if len(h.ThemeMods.ThemeSupport.PostFormats) > 0 { if len(h.ThemeMods.ThemeSupport.PostFormats) > 0 {
class = append(class, "single-format-standard") s = str.Join(s, " single-format-standard")
} }
} }
if s != "" {
class = append(class, s)
}
if wpconfig.IsCustomBackground(h.Theme) { if wpconfig.IsCustomBackground(h.Theme) {
class = append(class, "custom-background") class = append(class, "custom-background")
} }

View File

@ -210,7 +210,6 @@ func (i *IndexParams) parseAuthor() (err error) {
i.Where = append(i.Where, []string{ i.Where = append(i.Where, []string{
"post_author", "=", strconv.FormatUint(user.Id, 10), "int", "post_author", "=", strconv.FormatUint(user.Id, 10), "int",
}) })
i.Header = str.Join("作者:", username)
} }
return return
} }

View File

@ -10,9 +10,6 @@ import (
) )
func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []T, total int, err error) { func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []T, total int, err error) {
if q.Page < 1 {
return
}
qx := QueryCondition{ qx := QueryCondition{
Where: q.Where, Where: q.Where,
Having: q.Having, Having: q.Having,
@ -49,42 +46,14 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r [
return return
} }
q.Offset = offset q.Offset = offset
m := ctx.Value("handle=>") sq, args, err := BuildQuerySql[T](q)
if m != nil { if err != nil {
mm, ok := m.(string) return
if ok && mm == "toMap" { }
v := ctx.Value("map") err = db.Select(ctx, &r, sq, args...)
mx, er := findToStringMap[T](db, ctx, q) if err != nil {
if er != nil { return
err = er
return
}
vv := v.(*[]map[string]string)
*vv = mx
return
}
} }
r, err = finds[T](db, ctx, q)
return
}
func paginationToMap[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
ctx = context.WithValue(ctx, "handle=>", "toMap")
ctx = context.WithValue(ctx, "map", &r)
_, total, err = pagination[T](db, ctx, q)
return
}
func PaginationToMap[T Model](ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
ctx = context.WithValue(ctx, "handle=>", "toMap")
ctx = context.WithValue(ctx, "map", &r)
_, total, err = pagination[T](globalBb, ctx, q)
return
}
func PaginationToMapFromDB[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
ctx = context.WithValue(ctx, "handle=>", "toMap")
ctx = context.WithValue(ctx, "map", &r)
_, total, err = pagination[T](db, ctx, q)
return return
} }

View File

@ -516,64 +516,3 @@ func Test_pagination(t *testing.T) {
}) })
} }
} }
func Test_paginationToMap(t *testing.T) {
type args struct {
db dbQuery
ctx context.Context
q QueryCondition
}
tests := []struct {
name string
args args
wantR []map[string]string
wantTotal int
wantErr bool
}{
{
name: "t1",
args: args{
db: glob,
ctx: ctx,
q: QueryCondition{
Fields: "ID",
Limit: 2,
Page: 1,
Where: SqlBuilder{{"ID < 200"}},
},
},
wantR: []map[string]string{{"ID": "63"}, {"ID": "64"}},
wantTotal: 4,
},
{
name: "t2",
args: args{
db: glob,
ctx: ctx,
q: QueryCondition{
Fields: "ID",
Limit: 2,
Page: 2,
Where: SqlBuilder{{"ID < 200"}},
},
},
wantR: []map[string]string{{"ID": "190"}, {"ID": "193"}},
wantTotal: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotR, gotTotal, err := paginationToMap[post](tt.args.db, tt.args.ctx, tt.args.q)
if (err != nil) != tt.wantErr {
t.Errorf("paginationToMap() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("paginationToMap() gotR = %v, want %v", gotR, tt.wantR)
}
if gotTotal != tt.wantTotal {
t.Errorf("paginationToMap() gotTotal = %v, want %v", gotTotal, tt.wantTotal)
}
})
}
}