在添加文章前,需要添加文章的分类。
这里是简单的对文章的增删改查
在templdate下创建admin/、footer.html、nav.html
header.html
footer.html
nav.html
然后在template下创建category目录下创建CategoryList.html、CategoryAdd.html两个页面
CategoryList.html
{{template "header.html" .}}
{{template "nav.html" .}}
分类列表
| ID |
分类名v |
操作 |
{{range .cateList}}
| {{.ID}} |
{{.CateName}} |
编辑|删除 |
{{end}}
{{template "footer.html" .}}
CategoryAdd.html
{{template "header.html" .}}
{{template "nav.html" .}}
{{template "footer.html" .}}
gin-demo/static/css/style.css
body {
width: 100%;
height: 100%;
margin: 0px;
overflow: hidden;
background-color: #FFFFFF;
font-family: "Microsoft YaHei", sans-serif;
}
.leftSideBar{
width: 182px;
height:100%;
padding-top: 31px;
padding-bottom: 31px;
overflow: auto;
background-color: #FFFFFF;
}
.pageContent{
height: 100%;
min-width: 770px;
left: 246px;
top: 0;
right: 0;
z-index: 3;
padding-bottom: 30px;
position: absolute;
}
.pageContainer{
bottom: 0;
left:0;
right: 0;
top: 63px;
overflow: auto;
position: absolute;
width: 100%;
}
.footer {
width: 100%;
height: 30px;
line-height: 30px;
margin-top: 0;
left: 0;
right: 0;
bottom: 0;
position: absolute;
z-index: 10;
}
这里创建完后,在gin-demo/model/、gin-demo/controller下创建category.go
model/category.go
package model
type Category struct {
gorm.Model
Id int `gorm:"primaryKey;"`
CateName string `gorm:"not null;unique"`
}
controller/category.go
package controller
import (
"fmt"
"gin-demo/common"
"gin-demo/model"
"gin-demo/response"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"net/http"
)
func AddCate(c *gin.Context){
db := common.GetDB()
var reqCate model.Category
c.Bind(&reqCate)
cateName := reqCate.CateName
if len(cateName) == 0 {
response.Response(c,http.StatusUnprocessableEntity,422,"分类名称不能为空",gin.H{})
return
}
if isCateExist(db,cateName){
response.Response(c,http.StatusUnprocessableEntity,422,"当前分类已经存在",gin.H{})
return
}
db.Create(&reqCate)
//创建完后查询数据
var cateList []model.Category
db.Find(&cateList)
c.Redirect(http.StatusMovedPermanently,"https://blog.csdn.net/cate/list")
}
func EditCate(c *gin.Context){
db := common.GetDB()
var reqCate model.Category
c.Bind(&reqCate)
fmt.Println(reqCate.CateName)
cateName := reqCate.CateName
if len(cateName) == 0 {
response.Response(c,http.StatusUnprocessableEntity,422,"分类名称不能为空",gin.H{})
return
}
db.Model(&reqCate).Update("cate_name", cateName)
c.Redirect(http.StatusMovedPermanently,"https://blog.csdn.net/cate/list")
}
func DeleteCate(c *gin.Context){
db := common.GetDB()
var id = c.Query("id")
var cateRow model.Category
db.Where("id = ?", id).Delete(&cateRow)
c.Redirect(http.StatusMovedPermanently,"https://blog.csdn.net/cate/list")
}
func CategoryEditPage(c *gin.Context){
db := common.GetDB()
var id = c.Query("id")
var cateRow model.Category
db.Where("id = ?", id).First(&cateRow)
c.HTML(http.StatusOK,"CategoryAdd.html",gin.H{"cateRow":cateRow})
}
func CategoryListPage(c *gin.Context) {
db := common.GetDB()
var cateList []model.Category
db.Find(&cateList)
c.HTML(http.StatusOK,"CategoryList.html",gin.H{"cateList":cateList})
}
func CategoryAddPage(c *gin.Context) {
c.HTML(http.StatusOK,"CategoryAdd.html",gin.H{})
}
func isCateExist(db *gorm.DB, cateName string) bool {
var cate model.Category
db.Where("cate_name = ?", cateName).First(&cate)
if cate.Id != 0 {
return true
}
return false
}
然后在routes.go下添加
package router
import (
"gin-demo/controller"
"github.com/gin-gonic/gin"
)
func InitRouter(r *gin.Engine) *gin.Engine{
r.LoadHTMLGlob("template*")
//加载静态资源
r.Static("/static", "./static")
//首页页面
r.GET("/index",controller.IndexPage)
//注册
r.POST("/user/register",controller.Register)
r.GET("/user/register",controller.RegisterPage)
//登录
r.POST("/user/login",controller.Login)
r.GET("/user/login",controller.LoginPage)
//分类页面
r.GET("https://blog.csdn.net/cate/list",controller.CategoryListPage)
r.GET("https://blog.csdn.net/cate/add",controller.CategoryAddPage)
r.GET("/cate/edit",controller.CategoryEditPage)
r.GET("/cate/delete",controller.DeleteCate)
r.POST("https://blog.csdn.net/cate/add",controller.AddCate)
r.POST("/cate/edit",controller.EditCate)
return r
}
运行即可完成增删改查。
比较简陋。