This commit is contained in:
@@ -1,9 +1,3 @@
|
||||
/*
|
||||
@Time : 2020/6/28 21:40
|
||||
@Author : xuyiqing
|
||||
@File : users.py
|
||||
*/
|
||||
|
||||
package admin
|
||||
|
||||
import (
|
||||
|
||||
@@ -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": "创建新内容",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,50 +6,55 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm" // <-- 确保导入 gorm
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Home(c *gin.Context) {
|
||||
// getThemeManager 从上下文中获取主题管理器
|
||||
func getThemeManager(c *gin.Context) (*utils.ThemeManager, bool) {
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.String(http.StatusInternalServerError, "Theme manager not found in context")
|
||||
return
|
||||
return nil, false
|
||||
}
|
||||
themeManager, ok := tm.(*utils.ThemeManager)
|
||||
return themeManager, ok
|
||||
}
|
||||
|
||||
// getDB 从上下文中获取数据库实例
|
||||
func getDB(c *gin.Context) (*gorm.DB, bool) {
|
||||
dbInterface, exists := c.Get("DB")
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
db, ok := dbInterface.(*gorm.DB)
|
||||
return db, ok
|
||||
}
|
||||
|
||||
// Home 首页
|
||||
func Home(c *gin.Context) {
|
||||
themeManager, ok := getThemeManager(c)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "Invalid theme manager type in context")
|
||||
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
|
||||
// 从 Gin 上下文中获取 DB 实例
|
||||
dbInterface, exists := c.Get("DB")
|
||||
if !exists {
|
||||
c.String(http.StatusInternalServerError, "DB not found in context")
|
||||
return
|
||||
}
|
||||
db, ok := dbInterface.(*gorm.DB)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "Invalid DB type in context")
|
||||
return
|
||||
}
|
||||
|
||||
db.Select("*").Limit(5).Find(&items, "type = ?", "post")
|
||||
|
||||
tpl := themeManager.GetTemplate("index") // "index" 是模板的基本名 (例如 index.tmpl -> index)
|
||||
tpl := themeManager.GetTemplate("index")
|
||||
if tpl == nil {
|
||||
c.String(http.StatusInternalServerError, "Template 'index' not found in current theme: "+themeManager.CurrentTheme())
|
||||
c.String(http.StatusInternalServerError, "Template 'index' not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
err := tpl.Execute(c.Writer, gin.H{
|
||||
tpl.Execute(c.Writer, gin.H{
|
||||
"Items": items,
|
||||
"Title": "首页", // 你可以根据需要传递更多数据
|
||||
"Title": "首页",
|
||||
})
|
||||
if err != nil {
|
||||
// 实际项目中应记录错误
|
||||
c.String(http.StatusInternalServerError, "Error rendering template: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,23 +2,18 @@ package controllers
|
||||
|
||||
import (
|
||||
"go_blog/models"
|
||||
"go_blog/utils"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm" // <-- 确保导入 gorm
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ShowPost 显示文章详情
|
||||
func ShowPost(c *gin.Context) {
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||
return
|
||||
}
|
||||
themeManager, ok := tm.(*utils.ThemeManager)
|
||||
themeManager, ok := getThemeManager(c)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -29,19 +24,13 @@ func ShowPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var content = models.Content{Cid: int32(id)}
|
||||
// 从 Gin 上下文中获取 DB 实例
|
||||
dbInterface, exists := c.Get("DB")
|
||||
if !exists {
|
||||
c.String(http.StatusInternalServerError, "DB not found in context")
|
||||
return
|
||||
}
|
||||
db, ok := dbInterface.(*gorm.DB)
|
||||
db, ok := getDB(c)
|
||||
if !ok {
|
||||
c.String(http.StatusInternalServerError, "Invalid DB type in context")
|
||||
c.String(http.StatusInternalServerError, "DB not found")
|
||||
return
|
||||
}
|
||||
|
||||
var content = models.Content{Cid: int32(id)}
|
||||
result := db.First(&content)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
@@ -52,19 +41,12 @@ func ShowPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 假设你的主题中有一个名为 "post" 的模板 (post.html 或 post.tmpl)
|
||||
// 注意:当前的 default 主题 (web/themes/default/templates/) 并没有 post.tmpl,你需要添加它。
|
||||
tpl := themeManager.GetTemplate("post")
|
||||
if tpl == nil {
|
||||
c.String(http.StatusInternalServerError, "Template 'post' not found in current theme: "+themeManager.CurrentTheme()+". Make sure 'post.html' or 'post.tmpl' exists in the theme's templates directory.")
|
||||
c.String(http.StatusInternalServerError, "Template 'post' not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
// 将整个 content 对象传递给模板。模板内部通过 {{ .Title }} {{ .Text }} 等访问。
|
||||
err = tpl.Execute(c.Writer, content)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error rendering template: "+err.Error())
|
||||
}
|
||||
tpl.Execute(c.Writer, content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user