feat(admin): 重构管理员文章路由并添加导航功能

- 将管理员文章路由从 Article 方法改为 Index 方法
- 在 ArticleController 中实现 Index 方法,支持预加载文章分类数据并返回 JSON
- 添加 ArticleCate 模型及对应数据表映射
- 扩展 Article 模型,关联 ArticleCate,实现文章与分类的关联查询
- 新增 Nav 模型及 NavController,提供导航数据接口
- 修改用户模型添加 JSON 标签,规范字段序列化
- 扩展数据库自动迁移,新增 Article、Nav 和 ArticleCate 表的自动创建功能
This commit is contained in:
2026-03-25 18:49:56 +08:00
parent 4a3c510a00
commit 4062d16fa8
8 changed files with 67 additions and 14 deletions

View File

@@ -1,10 +1,21 @@
package admin package admin
import "github.com/gin-gonic/gin" import (
"awesomeProject/model"
"github.com/gin-gonic/gin"
)
type ArticleController struct { type ArticleController struct {
} }
func (con ArticleController) Index(c *gin.Context) {
articleList := []model.Article{}
model.DB.Preload("ArticleCate").Find(&articleList)
c.JSON(200, articleList)
}
func (con ArticleController) Article(c *gin.Context) { func (con ArticleController) Article(c *gin.Context) {
c.String(200, "管理员文章列表") c.String(200, "管理员文章列表")
} }

View File

@@ -0,0 +1,17 @@
package admin
import (
"awesomeProject/model"
"github.com/gin-gonic/gin"
)
type NavController struct {
BaseController
}
func (con NavController) Index(c *gin.Context) {
navList := []model.Nav{}
model.DB.Find(&navList)
c.JSON(200, navList)
}

View File

@@ -1,10 +1,11 @@
package model package model
type Article struct { type Article struct {
Id int Id int
Title string Title string
CateId int CateId int
State int State int
ArticleCate ArticleCate `gorm:"foreignKey:CateId;references:State"`
} }
func (Article) TableName() string { func (Article) TableName() string {

11
model/article_cate.go Normal file
View File

@@ -0,0 +1,11 @@
package model
type ArticleCate struct {
Id int
Title string
State int
}
func (ArticleCate) TableName() string {
return "article_cate"
}

View File

@@ -20,8 +20,8 @@ func init() {
return return
} }
// 自动创建数据 // 自动创建所有不存在的
err = DB.AutoMigrate(&User{}) err = DB.AutoMigrate(&User{}, &Article{}, &Nav{}, &ArticleCate{})
if err != nil { if err != nil {
fmt.Println("数据表迁移失败:", err) fmt.Println("数据表迁移失败:", err)
} else { } else {

13
model/nav.go Normal file
View File

@@ -0,0 +1,13 @@
package model
type Nav struct {
Id int `json:"id"`
Title string `json:"title"`
Url string `json:"url"`
Status int `json:"status"`
Sort int `json:"sort"`
}
func (Nav) TableName() string {
return "nav"
}

View File

@@ -1,12 +1,12 @@
package model package model
type User struct { type User struct {
Id int Id int `json:"id"`
Name string Name string `json:"name"`
Age int Age int `json:"age"`
Email string Email string `json:"email"`
Password string Password string `json:"password"`
AddTime int AddTime int `json:"add_time"`
} }
func (User) TableName() string { func (User) TableName() string {

View File

@@ -22,7 +22,7 @@ func AdminRouterInit(r *gin.Engine) {
adminRouter.GET("/login", admin.IndexController{}.Login) adminRouter.GET("/login", admin.IndexController{}.Login)
adminRouter.GET("/article", admin.ArticleController{}.Article) adminRouter.GET("/article", admin.ArticleController{}.Index)
adminRouter.GET("/user", admin.UserController{}.Index) adminRouter.GET("/user", admin.UserController{}.Index)