在添加文章前,需要添加文章的分类。
这里是简单的对文章的增删改查
在templdate下创建admin/、footer.html、nav.html
header.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title>
<link rel="stylesheet" href="../../static/css/bootstrap.min.css">
<link rel="stylesheet" href="../../static/css/style.css">
<script src="../../static/js/jquery.min.js">script>
<script src="../../static/js/bootstrap.min.js">script>
head>
<body>
<nav class="navbar navbar-default" style="height: 60px">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" title="logo" href="/admin">XX博客后台a>
div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li role="presentation">
<a href="#">当前用户:<span class="badge">TestUserspan>a>
li>
<li>
<a href="../login/logout">
<span class="glyphicon glyphicon-lock">span>退出登录a>
li>
ul>
div>
div>
nav>
footer.html
<div class="footer">
<p class="text-center">
2021 xx
p>
div>
body>
html>
nav.html
<div class="pageContainer">
<div class="leftSideBar">
<ul class="nav nav-pills nav-stacked ">
<li role="presentation">
<a href="/article/list">文章列表a>
li>
<li role="presentation">
<a href="/cate/list">分类列表a>
li>
<li role="presentation">
<a href="/tag/list">标签列表a>
li>
ul>
div>
然后在template下创建category目录下创建CategoryList.html、CategoryAdd.html两个页面
CategoryList.html
{{template "header.html" .}}
{{template "nav.html" .}}
<div class="splitter">div>
<div class="pageContent">
<div class="container-fluid">
<h1 class="mt-4" style="font-size: 30px;text-align: center">分类列表h1>
<div class="card mb-4">
<div class="card-header" style="height: 30px">
<a class=" btn navbar-brand btn-success" href="/cate/add">添加分类a>
div>
<div class="card-body" style="padding-top: 20px;margin-top: 5px">
<table class="table table-bordered table-hover" id="datatablesSimple">
<thead>
<tr>
<th>IDth>
<th>分类名vth>
<th>操作th>
tr>
thead>
<tbody>
{{range .cateList}}
<tr>
<td>{{.ID}}td>
<td>{{.CateName}}td>
<td><a href="/cate/edit?id={{.ID}}">编辑a>|<a href="/cate/delete?id={{.ID}}">删除a>td>
tr>
{{end}}
tbody>
table>
div>
div>
div>
div>
{{template "footer.html" .}}
CategoryAdd.html
{{template "header.html" .}}
{{template "nav.html" .}}
<div class="splitter">div>
<div class="pageContent">
<div>
{{if .cateRow.ID}}
<form action="/cate/edit" method="post">
{{else}}
<form action="/cate/add" method="post">
{{end}}
<div class="col-md-4 col-lg-4 col-lg-offset-4 col-md-offset-4 login-block" style="background-color: white; opacity: 0.8; padding: 50px; border-radius: 15px">
<h3 style="text-align: center;padding-bottom:20px;font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size: 40px">分类名称添加h3>
<div class="form-group input-group input-group-lg" >
<span class="input-group-addon" id="CateName" >分类名称span>
<input type="hidden" name="ID" class="form-control" value="{{.cateRow.ID}}">
<input type="text" name="CateName" class="form-control" placeholder="分类名称" value="{{.cateRow.CateName}}">
div>
<div class="form-group center-block">
<button type="submit" class="btn btn-success center-block" style="width: 80px;height: 40px;font-size: 20px">保存button>
div>
div>
form>
div>
div>
div>
div>
{{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,"/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,"/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,"/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("/cate/list",controller.CategoryListPage)
r.GET("/cate/add",controller.CategoryAddPage)
r.GET("/cate/edit",controller.CategoryEditPage)
r.GET("/cate/delete",controller.DeleteCate)
r.POST("/cate/add",controller.AddCate)
r.POST("/cate/edit",controller.EditCate)
return r
}
运行即可完成增删改查。
比较简陋。