This commit is contained in:
xing 2022-09-15 22:44:45 +08:00
parent 912ec05876
commit 4a31759f45

View File

@ -3,6 +3,7 @@ package route
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/helper"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"math" "math"
"net/http" "net/http"
@ -16,6 +17,10 @@ var PostsCache sync.Map
func index(c *gin.Context) { func index(c *gin.Context) {
page := 1 page := 1
pageSize := 10 pageSize := 10
order := c.Query("order")
if !helper.IsContainInArr(order, []string{"asc", "desc"}) {
order = "desc"
}
p := c.Query("paged") p := c.Query("paged")
if p == "" { if p == "" {
p = c.Param("page") p = c.Param("page")
@ -29,7 +34,7 @@ func index(c *gin.Context) {
status := []interface{}{"publish", "private"} status := []interface{}{"publish", "private"}
posts, totalRaw, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{ posts, totalRaw, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{
"post_type", "post", "post_type", "post",
}, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", "desc"}}, nil, status) }, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status)
defer func() { defer func() {
if err != nil { if err != nil {
c.Error(err) c.Error(err)