wp-go/app/theme/wp/index.go

192 lines
4.6 KiB
Go
Raw Permalink Normal View History

2023-03-01 05:17:12 +00:00
package wp
import (
"database/sql"
2023-10-27 15:57:15 +00:00
"errors"
"fmt"
2023-05-04 12:37:06 +00:00
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins"
2023-11-12 13:39:04 +00:00
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/plugin/pagination"
2023-03-06 12:53:51 +00:00
"strings"
)
2023-02-23 09:26:20 +00:00
type IndexHandle struct {
*Handle
Param *IndexParams
Posts []models.Posts
pageEle pagination.Render
TotalRows int
postsPlugin PostsPlugin
2023-02-28 15:59:17 +00:00
}
func (h *Handle) GetIndexHandle() *IndexHandle {
v, ok := h.C.Get("indexHandle")
if !ok {
vv := NewIndexHandle(h)
h.C.Set("indexHandle", vv)
return vv
}
return v.(*IndexHandle)
}
func (i *IndexHandle) ListPlugin() func(*Handle, *models.Posts) {
return i.postsPlugin
2023-02-28 15:59:17 +00:00
}
func (i *IndexHandle) SetListPlugin(listPlugin func(*Handle, *models.Posts)) {
i.postsPlugin = listPlugin
2023-02-28 15:59:17 +00:00
}
func (i *IndexHandle) PageEle() pagination.Render {
return i.pageEle
2023-02-28 15:59:17 +00:00
}
func (i *IndexHandle) SetPageEle(pageEle pagination.Render) {
i.pageEle = pageEle
2023-02-23 09:26:20 +00:00
}
func NewIndexHandle(handle *Handle) *IndexHandle {
return &IndexHandle{Handle: handle}
}
2023-05-05 12:45:20 +00:00
func PushIndexHandler(pipeScene string, h *Handle, call HandleCall) {
h.PushHandlers(pipeScene, call, constraints.Home,
constraints.Category, constraints.Search, constraints.Tag,
constraints.Archive, constraints.Author,
)
}
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
i.Param = parm
2023-02-28 15:38:23 +00:00
switch i.scene {
2023-03-17 10:26:08 +00:00
case constraints.Search:
i.Param.ParseSearch()
case constraints.Category:
err = i.Param.ParseCategory()
case constraints.Tag:
err = i.Param.ParseTag()
case constraints.Archive:
err = i.Param.ParseArchive()
case constraints.Author:
err = i.Param.ParseAuthor()
}
if err != nil {
i.Stats = constraints.ParamError
return
}
i.Param.ParseParams()
2023-02-14 15:44:48 +00:00
i.Param.CacheKey = i.Param.getSearchKey()
2023-02-28 15:38:23 +00:00
i.ginH["title"] = i.Param.getTitle()
i.ginH["search"] = i.Param.Search
i.ginH["header"] = i.Param.Header
return
}
func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err error) {
2023-05-18 14:27:28 +00:00
q := &model.QueryCondition{
2023-02-25 15:10:42 +00:00
Where: i.Param.Where,
Order: model.SqlBuilder{{i.Param.OrderBy, i.Param.Order}},
Join: i.Param.Join,
In: [][]any{i.Param.PostType, i.Param.PostStatus},
}
2023-02-28 15:38:23 +00:00
switch i.scene {
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
2023-11-25 09:49:20 +00:00
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, q, i.Param.Page, i.Param.PageSize)
2023-03-29 13:58:39 +00:00
if i.scene == constraints.Home && i.Param.Page == 1 {
i.MarkSticky(&posts)
}
case constraints.Search:
2023-11-25 09:49:20 +00:00
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, q, i.Param.Page, i.Param.PageSize)
case constraints.Archive:
2023-03-06 12:53:51 +00:00
i.ginH["archiveYear"] = i.Param.Year
i.ginH["archiveMonth"] = strings.TrimLeft(i.Param.Month, "0")
posts, totalRaw, err = cache.GetMonthPostIds(i.C, i.Param.Year, i.Param.Month, i.Param.Page, i.Param.PageSize, i.Param.Order)
}
return
}
func (i *IndexHandle) Pagination() {
2023-02-28 15:59:17 +00:00
if i.pageEle == nil {
i.pageEle = plugins.TwentyFifteenPagination()
2023-02-23 09:26:20 +00:00
}
q := i.C.Request.URL.Query().Encode()
if q != "" {
q = fmt.Sprintf("?%s", q)
}
i.ginH["pagination"] = pagination.Paginate(i.pageEle, i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, *i.C.Request.URL, i.IsHttps())
}
func (i *IndexHandle) BuildIndexData() (err error) {
if i.Param == nil {
i.Param = NewIndexParams(i.C)
}
err = i.ParseIndex(i.Param)
if err != nil {
2023-02-27 14:58:35 +00:00
i.Stats = constraints.ParamError
return
}
posts, totalRows, err := i.GetIndexData()
2023-10-27 15:57:15 +00:00
if err != nil && !errors.Is(err, sql.ErrNoRows) {
2023-02-27 14:58:35 +00:00
i.Stats = constraints.Error404
return
}
i.Posts = posts
i.TotalRows = totalRows
2023-12-03 14:42:44 +00:00
i.ginH["totalPage"] = number.DivideCeil(totalRows, i.Param.PageSize)
return
}
var GetPostsPlugin = reload.BuildValFnWithAnyParams("postPlugins", UsePostsPlugins)
func (i *IndexHandle) ExecPostsPlugin() {
2023-05-09 05:44:20 +00:00
fn := i.postsPlugin
if fn == nil {
fn = GetPostsPlugin()
2023-05-09 05:44:20 +00:00
}
for j := range i.Posts {
fn(i.Handle, &i.Posts[j])
}
2023-02-14 15:28:45 +00:00
}
func IndexRender(h *Handle) {
i := h.GetIndexHandle()
i.ExecPostsPlugin()
i.Pagination()
i.ginH["posts"] = i.Posts
}
2023-05-05 12:45:20 +00:00
func Index(h *Handle) {
i := h.GetIndexHandle()
err := i.BuildIndexData()
2023-05-05 12:45:20 +00:00
if err != nil {
i.SetErr(err)
}
h.SetData("scene", h.Scene())
2023-02-14 15:28:45 +00:00
}
2023-03-29 13:58:39 +00:00
func (i *IndexHandle) MarkSticky(posts *[]models.Posts) {
a := GetStickPosts(i.Handle)
2023-03-29 13:58:39 +00:00
if len(a) < 1 {
return
}
m := GetStickMapPosts(i.Handle)
2023-03-29 13:58:39 +00:00
*posts = append(a, slice.Filter(*posts, func(post models.Posts, _ int) bool {
_, ok := m[post.Id]
return !ok
2023-03-29 13:58:39 +00:00
})...)
}