Compare commits
2 Commits
11e0c32c6f
...
b4cc570e8a
Author | SHA1 | Date | |
---|---|---|---|
|
b4cc570e8a | ||
|
9c89f44841 |
|
@ -1,7 +1,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/fthvgb1/wp-go/helper/number"
|
||||||
"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,6 +17,7 @@ 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 ",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,38 +26,40 @@ 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.GinH["posts"].([]models.Posts)) > 0 {
|
if len(h.Index.Posts) > 0 {
|
||||||
s = "search-results"
|
s = "search-results"
|
||||||
}
|
}
|
||||||
|
class = append(class, s)
|
||||||
case constraints.Category, constraints.Tag:
|
case constraints.Category, constraints.Tag:
|
||||||
cat := h.C.Param("category")
|
cat := h.Index.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] != '%' {
|
||||||
s = cate.Slug
|
class = append(class, str.Join("category-", cate.Slug))
|
||||||
}
|
}
|
||||||
s = fmt.Sprintf("category-%v category-%v", s, cate.Terms.TermId)
|
class = append(class, str.Join("category-", number.ToString(cate.Terms.TermId)))
|
||||||
case constraints.Detail:
|
|
||||||
s = fmt.Sprintf("postid-%d", h.GinH["post"].(models.Posts).Id)
|
case constraints.Author:
|
||||||
if len(h.ThemeMods.ThemeSupport.PostFormats) > 0 {
|
author := h.Index.Param.Author
|
||||||
s = str.Join(s, " single-format-standard")
|
user, _ := cache.GetUserByName(h.C, author)
|
||||||
}
|
class = append(class, str.Join("author-", number.ToString(user.Id)))
|
||||||
}
|
if user.UserLogin[0] != '%' {
|
||||||
if s != "" {
|
class = append(class, str.Join("author-", user.UserLogin))
|
||||||
class = append(class, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case constraints.Detail:
|
||||||
|
class = append(class, str.Join("postid-", number.ToString(h.Detail.Post.Id)))
|
||||||
|
if len(h.ThemeMods.ThemeSupport.PostFormats) > 0 {
|
||||||
|
class = append(class, "single-format-standard")
|
||||||
|
}
|
||||||
|
}
|
||||||
if wpconfig.IsCustomBackground(h.Theme) {
|
if wpconfig.IsCustomBackground(h.Theme) {
|
||||||
class = append(class, "custom-background")
|
class = append(class, "custom-background")
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,6 +210,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,9 @@ 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,
|
||||||
|
@ -46,14 +49,42 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r [
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q.Offset = offset
|
q.Offset = offset
|
||||||
sq, args, err := BuildQuerySql[T](q)
|
m := ctx.Value("handle=>")
|
||||||
if err != nil {
|
if m != nil {
|
||||||
|
mm, ok := m.(string)
|
||||||
|
if ok && mm == "toMap" {
|
||||||
|
v := ctx.Value("map")
|
||||||
|
mx, er := findToStringMap[T](db, ctx, q)
|
||||||
|
if er != nil {
|
||||||
|
err = er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = db.Select(ctx, &r, sq, args...)
|
vv := v.(*[]map[string]string)
|
||||||
if err != nil {
|
*vv = mx
|
||||||
return
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -516,3 +516,64 @@ 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user