package controllers import ( "go_blog/models" "go_blog/utils" "net/http" "strconv" "github.com/gin-gonic/gin" ) // CreateContentHandler 创建内容 func CreateContentHandler(ctx *gin.Context) { var content models.Content if err := ctx.ShouldBindJSON(&content); err != nil { ctx.JSON(400, gin.H{"error": err.Error()}) return } if err := models.DB.Create(&content).Error; err != nil { ctx.JSON(500, gin.H{"error": "增加内容失败"}) return } ctx.JSON(201, content) } // PageList 分页文章列表 func PageList(c *gin.Context) { themeManager, ok := getThemeManager(c) if !ok { c.String(http.StatusInternalServerError, "Theme manager not found") return } db, ok := getDB(c) if !ok { c.String(http.StatusInternalServerError, "DB not found") return } var items []models.Content var pager utils.Pager pager.InitPager(c) offset := (pager.Page - 1) * pager.PageSize db.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post") var totalCount int64 db.Model(&models.Content{}).Where("type = ?", "post").Count(&totalCount) pager.Total = int(totalCount) tpl := themeManager.GetTemplate("index") if tpl == nil { c.String(http.StatusInternalServerError, "Template 'index' not found") return } c.Header("Content-Type", "text/html; charset=utf-8") tpl.Execute(c.Writer, gin.H{ "Items": items, "Pager": pager, "Title": "文章列表 - 第 " + strconv.Itoa(pager.Page) + " 页", }) } // CreateContentPage 创建内容页面 func CreateContentPage(c *gin.Context) { themeManager, ok := getThemeManager(c) if !ok { c.String(http.StatusInternalServerError, "Theme manager not found") return } tpl := themeManager.GetTemplate("content") if tpl == nil { c.String(http.StatusInternalServerError, "Template 'content' not found") return } c.Header("Content-Type", "text/html; charset=utf-8") tpl.Execute(c.Writer, gin.H{ "Title": "创建新内容", }) }