This commit is contained in:
@@ -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": "创建新内容",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user