完善状态码

This commit is contained in:
xing 2023-02-11 23:58:40 +08:00
parent 23f32c8fed
commit bfd4e4ac4d
4 changed files with 12 additions and 13 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
@ -26,7 +25,6 @@ func (d *DetailHandle) BuildDetailData() (err error) {
d.GinH["title"] = wpconfig.Options.Value("blogname")
err = d.CheckAndGetPost()
if err != nil {
d.Scene = constraints.Error404
return
}
d.WidgetAreaData()
@ -41,12 +39,10 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
maxId, err := cache.GetMaxPostId(d.C)
logs.ErrPrintln(err, "get max post id")
if id > maxId || id <= 0 || err != nil {
d.Stats = constraints.Error404
return
}
post, err := cache.GetPostById(d.C, id)
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
d.Stats = constraints.Error404
return
}

View File

@ -11,7 +11,6 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/plugin/pagination"
"net/http"
)
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
@ -76,14 +75,10 @@ func (i *IndexHandle) Pagination() {
func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
err = i.ParseIndex(parm)
if err != nil {
i.Stats = constraints.ParamError
i.Code = http.StatusNotFound
return
}
posts, totalRows, err := i.GetIndexData()
if err != nil && err != sql.ErrNoRows {
i.Scene = constraints.InternalErr
i.Code = http.StatusInternalServerError
return
}
i.GinH["posts"] = posts

View File

@ -4,6 +4,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/common"
"net/http"
)
const ThemeName = "twentyfifteen"
@ -39,6 +40,8 @@ func (i *indexHandle) Index() {
err := i.BuildIndexData(common.NewIndexParams(i.C))
if err != nil {
i.Stats = constraints.Error404
i.Code = http.StatusNotFound
i.C.HTML(i.Code, i.Templ, i.GinH)
return
}
@ -54,6 +57,7 @@ func (d *detailHandle) Detail() {
err := d.BuildDetailData()
if err != nil {
d.Stats = constraints.Error404
d.Code = http.StatusNotFound
d.C.HTML(d.Code, d.Templ, d.GinH)
return
}

View File

@ -73,27 +73,31 @@ var pluginFns = func() map[string]common.Plugin[models.Posts] {
func (i *indexHandle) Index() {
i.Templ = "twentyseventeen/posts/index.gohtml"
p := common.NewIndexParams(i.C)
err := i.BuildIndexData(p)
i.GinH["bodyClass"] = i.h.bodyClass()
err := i.BuildIndexData(common.NewIndexParams(i.C))
if err != nil {
i.Stats = constraints.Error404
i.Code = http.StatusNotFound
i.GinH["bodyClass"] = i.h.bodyClass()
i.C.HTML(i.Code, i.Templ, i.GinH)
return
}
i.PageEle = paginate
i.ExecListPagePlugin(pluginFns)
i.Pagination()
i.GinH["bodyClass"] = i.h.bodyClass()
i.C.HTML(i.Code, i.Templ, i.GinH)
}
func (d *detailHandle) Detail() {
err := d.BuildDetailData()
d.GinH["bodyClass"] = d.h.bodyClass()
if err != nil {
d.Code = http.StatusNotFound
d.Stats = constraints.Error404
d.GinH["bodyClass"] = d.h.bodyClass()
d.C.HTML(d.Code, d.Templ, d.GinH)
return
}
d.GinH["bodyClass"] = d.h.bodyClass()
img := plugins.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail")
img.Width = img.OriginAttachmentData.Width
img.Height = img.OriginAttachmentData.Height