栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

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

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

分类列表

{{range .cateList}} {{end}}
ID 分类名v 操作
{{.ID}} {{.CateName}} 编辑|删除
{{template "footer.html" .}}

CategoryAdd.html

{{template "header.html" .}}
{{template "nav.html" .}}

{{if .cateRow.ID}}
{{else}} {{end}}
{{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
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991455.html

Go语言相关栏目本月热门文章

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号