标签:填写信息 bsp 业务逻辑 route delete 修改 lazy password load
目录列表
安装gin框架
go get github.com/gin-gonic/gin
路由包
package routers import ( "github.com/gin-gonic/gin" "lesson26/bubble/controller" ) func SetupRouter() *gin.Engine { r := gin.Default() r.Static("/static","./static") r.Static("/templates","./templates") r.LoadHTMLGlob("templates/*") r.GET("/", controller.IndexHandler) //v1 v1Group := r.Group("v1") { //代办事项 //添加 v1Group.POST("/todo", controller.CreateATodo) //查看所有的代办事项 v1Group.GET("/todo", controller.GetTodoList) //修改 某一个代办事项 v1Group.PUT("/todo/:id", controller.UpdateATodo) //删除 v1Group.DELETE("/todo/:id", controller.DeleteATodo) } return r }
models包
package models import "lesson26/bubble/dao" //Todo model type Todo struct { ID int `json:"id"` Title string `json:"title"` Status bool `json:"status"` } //todo增伤改查 //创建todo func CreateTodo(todo *Todo)(err error){ if err := dao.DB.Create(&todo).Error;err !=nil{ return err } return nil } func GetAllTodo() (todoList []*Todo,err error) { if err := dao.DB.Find(&todoList).Error;err !=nil{ return nil,err } return todoList,nil } func GetATodo(id string) (todo *Todo,err error) { todo = new(Todo) if err = dao.DB.Where("id=?",id).First(&todo).Error;err !=nil{ return nil,err } return todo,nil } func UpdateATodo(todo *Todo) (err error) { if err =dao.DB.Save(&todo).Error;err !=nil{ return err } return } func DeleteATodo(todo *Todo) (err error) { if err = dao.DB.Delete(&todo).Error;err !=nil{ return err } return }
dao包,数据库
package dao import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) var( DB *gorm.DB ) func InitMysql() (err error) { //链接数据库 DB ,err = gorm.Open("mysql","user:password@/bubble?charset=utf8&parseTime=True&loc=Local") if err !=nil{ return err //panic(err) } return DB.DB().Ping() } func Close() { DB.Close() }
controller
package controller import ( "github.com/gin-gonic/gin" "lesson26/bubble/models" "net/http" ) /* url -- >controller ---> logic的逻辑--->models 请求来了-> 控制器----> 业务逻辑 --> 模型层的增删改查 */ func IndexHandler(c *gin.Context){ c.HTML(http.StatusOK,"index.html",nil) } func CreateATodo(c *gin.Context) { var todo models.Todo //前端页面填写信息发送过来, //拿到数据 if err :=c.BindJSON(&todo);err !=nil{ c.JSON(http.StatusBadRequest,gin.H{"error":err.Error()}) }else{ err := models.CreateTodo(&todo) if err !=nil{ c.JSON(http.StatusOK,gin.H{"error":err.Error()}) }else{ //返回响应 c.JSON(http.StatusOK,todo) } } } func GetTodoList(c *gin.Context) { //var todoList []Todo todoList,err := models.GetAllTodo() if err !=nil{ c.JSON(http.StatusOK,gin.H{"error:":err.Error()}) }else{ c.JSON(http.StatusOK,todoList) } } func UpdateATodo(c *gin.Context) { id:= c.Param("id") //var todo Todo todo,err := models.GetATodo(id) if err !=nil{ c.JSON(http.StatusBadRequest,gin.H{"error":err.Error()}) return } c.BindJSON(&todo) if err = models.UpdateATodo(todo);err !=nil{ c.JSON(http.StatusOK,gin.H{"error":err.Error()}) }else{ c.JSON(http.StatusOK,todo) } } func DeleteATodo(c *gin.Context) { id := c.Param("id") todo,err :=models.GetATodo(id) if err !=nil{ c.JSON(http.StatusOK,gin.H{"error":err.Error()}) return } err = models.DeleteATodo(todo) if err !=nil{ c.JSON(http.StatusOK,gin.H{"error":err.Error()}) }else{ c.JSON(http.StatusOK,gin.H{id:"deleted"}) } }
main 入口
package main import ( "lesson26/bubble/dao" "lesson26/bubble/models" "lesson26/bubble/routers" ) func main() { //创建数据库 //sql CREATE DATABASE bubble; //初始化数据库 err := dao.InitMysql() if err !=nil{ panic(err) } defer dao.Close() //创建表 //模型绑定 dao.DB.AutoMigrate(&models.Todo{}) r := routers.SetupRouter() r.Run(":9090") }
标签:填写信息 bsp 业务逻辑 route delete 修改 lazy password load
原文地址:https://www.cnblogs.com/pebblecome/p/14314285.html