使用qoder优化后更新
Some checks failed
Go build / build and run (push) Has been cancelled

This commit is contained in:
2026-02-12 13:48:35 +08:00
parent abd5962ae9
commit bc59f616fd
15 changed files with 130 additions and 278 deletions

View File

@@ -2,13 +2,15 @@ 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()})
@@ -22,3 +24,61 @@ func CreateContentHandler(ctx *gin.Context) {
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": "创建新内容",
})
}