2024-08-01 02:44:06 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"go_blog/models"
|
2026-02-12 13:48:35 +08:00
|
|
|
"go_blog/utils"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2024-08-01 02:44:06 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-12 13:48:35 +08:00
|
|
|
// CreateContentHandler 创建内容
|
2024-08-01 02:44:06 +08:00
|
|
|
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 {
|
2024-10-17 20:08:31 +08:00
|
|
|
ctx.JSON(500, gin.H{"error": "增加内容失败"})
|
2024-08-01 02:44:06 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(201, content)
|
|
|
|
|
}
|
2026-02-12 13:48:35 +08:00
|
|
|
|
|
|
|
|
// 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": "创建新内容",
|
|
|
|
|
})
|
|
|
|
|
}
|