gin框架的使用(八)——文章分类的增删改查

9 篇文章 7 订阅

在添加文章前,需要添加文章的分类。
这里是简单的对文章的增删改查
在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">TestUser</span></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>ID</th>
            <th>分类名v</th>
            <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
}

运行即可完成增删改查。
比较简陋。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值