wp-go/app/pkg/dao/common.go

81 lines
2.0 KiB
Go
Raw Permalink Normal View History

package dao
2023-01-12 12:42:16 +00:00
import (
"context"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-03-19 14:48:23 +00:00
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/model"
2023-01-12 12:42:16 +00:00
)
var TotalRaw int64
type PostIds struct {
Ids []uint64
Length int
}
type PostContext struct {
Prev models.Posts
Next models.Posts
2023-01-12 12:42:16 +00:00
}
2023-01-21 14:56:41 +00:00
func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
2023-01-12 12:42:16 +00:00
ctx := a[0].(context.Context)
2023-04-24 13:51:43 +00:00
t, ok := a[1].(string)
2023-01-21 14:56:41 +00:00
var in = []any{"category", "post_tag"}
2023-03-19 12:40:08 +00:00
if ok {
switch t {
case constraints.Category:
in = []any{"category"}
case constraints.Tag:
in = []any{"post_tag"}
}
}
2023-03-19 14:48:23 +00:00
w := model.SqlBuilder{
{"tt.taxonomy", "in", ""},
}
2023-03-27 03:37:24 +00:00
if helper.GetContextVal(ctx, "showOnlyTopLevel", false) {
2023-03-19 14:48:23 +00:00
w = append(w, []string{"tt.parent", "=", "0", "int"})
}
2023-03-27 03:37:24 +00:00
if !helper.GetContextVal(ctx, "showEmpty", false) {
2023-03-19 14:48:23 +00:00
w = append(w, []string{"tt.count", ">", "0", "int"})
}
2023-03-20 15:41:57 +00:00
order := []string{"name", "asc"}
ord := helper.GetContextVal[[]string](ctx, "order", nil)
if ord != nil {
order = ord
}
2023-02-06 12:50:25 +00:00
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
2023-03-19 14:48:23 +00:00
model.Where(w),
2023-02-06 12:50:25 +00:00
model.Fields("t.term_id"),
2023-03-20 15:41:57 +00:00
model.Order(model.SqlBuilder{order}),
2023-02-06 12:50:25 +00:00
model.Join(model.SqlBuilder{
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
}),
model.In(in),
))
2023-01-12 12:42:16 +00:00
for i := 0; i < len(terms); i++ {
2023-04-15 16:57:50 +00:00
if v, ok := wpconfig.GetTerm(terms[i].Terms.TermId); ok {
2023-01-12 12:42:16 +00:00
terms[i].Terms = v
}
2023-04-15 16:57:50 +00:00
if v, ok := wpconfig.GetTermTaxonomy(terms[i].Terms.TermId); ok {
2023-01-12 12:42:16 +00:00
terms[i].TermTaxonomy = v
}
}
return
}
func Archives(ctx context.Context) ([]models.PostArchive, error) {
2023-02-06 12:50:25 +00:00
return model.Finds[models.PostArchive](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"post_type", "post"},
{"post_status", "publish"},
}),
model.Fields("YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts"),
model.Group("year,month"),
model.Order(model.SqlBuilder{{"year", "desc"}, {"month", "desc"}}),
))
2023-01-12 12:42:16 +00:00
}