增加基础设置页面

This commit is contained in:
2026-07-29 16:45:22 +08:00
parent 0b22d4f126
commit a9a890a849
15 changed files with 472 additions and 84 deletions
+17 -2
View File
@@ -7,6 +7,7 @@ import (
"goblog/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// 创建评论请求
@@ -24,7 +25,10 @@ func GetComments(c *gin.Context) {
postID := c.Query("post_id")
status := c.Query("status")
db := database.DB.Model(&models.Comment{}).Preload("User")
db := database.DB.Model(&models.Comment{}).Preload("User").
Preload("Post", func(db *gorm.DB) *gorm.DB {
return db.Select("id", "title", "slug")
})
if postID != "" {
db = db.Where("post_id = ?", postID)
@@ -36,7 +40,18 @@ func GetComments(c *gin.Context) {
var comments []models.Comment
db.Order("created_at DESC").Find(&comments)
c.JSON(http.StatusOK, gin.H{"data": comments})
// Comment.Post 在 JSON 中被隐藏,额外附加文章标题和别名供前端展示
type commentWithPost struct {
models.Comment
PostTitle string `json:"post_title"`
PostSlug string `json:"post_slug"`
}
list := make([]commentWithPost, 0, len(comments))
for _, cm := range comments {
list = append(list, commentWithPost{cm, cm.Post.Title, cm.Post.Slug})
}
c.JSON(http.StatusOK, gin.H{"data": list})
}
// 创建评论