首页右栏接入单页入口与公告置顶/全部页;Markdown 编辑器支持可视化时间线与 Git 导入;隐藏块改为 [hide]/[timeline] 命名闭合,并修复单页/公告 GORM 更新只改 updated_at 的问题。 Co-authored-by: Cursor <cursoragent@cursor.com>
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
package handler
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"strconv"
|
||
|
||
"github.com/freefire/jiang13-bbs/service"
|
||
"github.com/gin-gonic/gin"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// SitePageDetail 公开单页详情(按 slug)
|
||
func (h *Handlers) SitePageDetail(c *gin.Context) {
|
||
slug := c.Param("slug")
|
||
p, err := h.SitePage.GetPublishedBySlug(slug)
|
||
if err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
c.JSON(http.StatusNotFound, gin.H{"error": "页面不存在"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取页面失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"page": p})
|
||
}
|
||
|
||
// AdminListSitePages 后台单页列表
|
||
func (h *Handlers) AdminListSitePages(c *gin.Context) {
|
||
list, err := h.SitePage.ListAll()
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取页面失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"pages": nonNilSlice(list)})
|
||
}
|
||
|
||
// AdminCreateSitePage 新建单页
|
||
func (h *Handlers) AdminCreateSitePage(c *gin.Context) {
|
||
var in service.SitePageInput
|
||
if err := c.ShouldBindJSON(&in); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
|
||
return
|
||
}
|
||
p, err := h.SitePage.Create(&in)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusCreated, gin.H{"page": p})
|
||
}
|
||
|
||
// AdminUpdateSitePage 编辑单页
|
||
func (h *Handlers) AdminUpdateSitePage(c *gin.Context) {
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的页面 ID"})
|
||
return
|
||
}
|
||
var in service.SitePageInput
|
||
if err := c.ShouldBindJSON(&in); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
|
||
return
|
||
}
|
||
p, err := h.SitePage.Update(uint(id), &in)
|
||
if err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
c.JSON(http.StatusNotFound, gin.H{"error": "页面不存在"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"page": p})
|
||
}
|
||
|
||
// AdminDeleteSitePage 删除单页
|
||
func (h *Handlers) AdminDeleteSitePage(c *gin.Context) {
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的页面 ID"})
|
||
return
|
||
}
|
||
if err := h.SitePage.Delete(uint(id)); err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
c.JSON(http.StatusNotFound, gin.H{"error": "页面不存在"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除失败"})
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||
}
|