From 3f034f86c7849bc0bff0239dc0bd01e84d353240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E9=AA=8F?= Date: Thu, 22 Jul 2021 11:16:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bubble/controller/controller.go | 47 +++++++++++ bubble/controller/v2Controller.go | 12 +++ bubble/main.go | 128 ++---------------------------- bubble/models/todo.go | 10 ++- bubble/routers/routers.go | 21 +++++ bubble/routers/v1.go | 24 ++++++ bubble/routers/v2.go | 15 ++++ 7 files changed, 134 insertions(+), 123 deletions(-) create mode 100644 bubble/controller/v2Controller.go create mode 100644 bubble/routers/routers.go create mode 100644 bubble/routers/v1.go create mode 100644 bubble/routers/v2.go diff --git a/bubble/controller/controller.go b/bubble/controller/controller.go index 5847452..bd2fdd6 100644 --- a/bubble/controller/controller.go +++ b/bubble/controller/controller.go @@ -4,6 +4,8 @@ import ( "github.com/gin-gonic/gin" "log" "net/http" + "strconv" + "studyGin/bubble/dao" "studyGin/bubble/models" ) @@ -44,3 +46,48 @@ func FindById(c *gin.Context) { } c.JSON(http.StatusOK, todo) } +func FindAll(c *gin.Context) { + todoList := &[]models.Todo{} + if err := dao.DB.Find(todoList).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error, + }) + return + } + log.Println(*todoList) + c.JSON(http.StatusOK, *todoList) +} + +func Update(c *gin.Context) { + id, ok := c.Params.Get("id") + if !ok { + c.JSON(http.StatusInternalServerError, "无效id") + return + } + todo := &models.Todo{} + if err := dao.DB.Where("id=?", id).Find(todo).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + c.BindJSON(todo) + if err := dao.DB.Save(todo).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + c.JSON(http.StatusOK, todo) +} + +func Delete(c *gin.Context) { + id, ok := c.Params.Get("id") + ID, _ := strconv.Atoi(id) + if !ok { + c.JSON(http.StatusInternalServerError, "无效id") + return + } + dao.DB.Delete(&models.Todo{Id: ID}) + c.JSON(http.StatusOK, "删除成功") +} diff --git a/bubble/controller/v2Controller.go b/bubble/controller/v2Controller.go new file mode 100644 index 0000000..3e911bb --- /dev/null +++ b/bubble/controller/v2Controller.go @@ -0,0 +1,12 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func Get(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "msg": "ok", + }) +} diff --git a/bubble/main.go b/bubble/main.go index c636c05..f50d637 100644 --- a/bubble/main.go +++ b/bubble/main.go @@ -1,139 +1,23 @@ package main import ( - "github.com/gin-gonic/gin" _ "github.com/jinzhu/gorm/dialects/mysql" - "log" - "net/http" - "strconv" "studyGin/bubble/dao" "studyGin/bubble/models" + "studyGin/bubble/routers" ) -func CreateTodo(todo *models.Todo) error { - tx := dao.DB.Begin() - defer func() { - if r := recover(); r != nil { - tx.Rollback() - } - }() - if err := tx.Error; err != nil { - return err - } - if err := tx.Create(todo).Error; err != nil { - log.Fatalln("创建清单失败") - tx.Rollback() - return err - } - return tx.Commit().Error -} -func main() { +func init() { //链接数据库 err := dao.InitMySQL() if err != nil { panic(err) } +} +func main() { defer dao.Close() dao.DB.AutoMigrate(&models.Todo{}) - - r := gin.Default() - //告诉滚、框架模板文件引用的静态文件去哪里找 - r.Static("/static", "static") - //告诉gin框架 去哪里找模板文件 - r.LoadHTMLGlob("templates/**") - - r.GET("/", func(c *gin.Context) { - c.HTML(http.StatusOK, "index.html", nil) - }) - - v1Group := r.Group("/v1") - { - //待办事项 - //添加 - v1Group.POST("/todo", func(c *gin.Context) { - todo := &models.Todo{} - err := c.BindJSON(todo) - if err != nil { - log.Fatalln("获取todo数据失败") - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - err = CreateTodo(todo) - if err != nil { - log.Fatalln("创建代办失败") - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - c.JSON(http.StatusOK, todo) - }) - //查看所有 - v1Group.GET("/todo", func(c *gin.Context) { - todoList := &[]models.Todo{} - if err := dao.DB.Find(todoList).Error; err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error, - }) - return - } - log.Println(*todoList) - c.JSON(http.StatusOK, *todoList) - }) - //通过id查看 - v1Group.GET("/todo/:id", func(c *gin.Context) { - id, ok := c.Params.Get("id") - if !ok { - c.JSON(http.StatusInternalServerError, "无效id") - return - } - todo := &models.Todo{} - todo, err := models.FindById(id) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - c.JSON(http.StatusOK, todo) - }) - //修改 - v1Group.PUT("/todo/:id", func(c *gin.Context) { - id, ok := c.Params.Get("id") - if !ok { - c.JSON(http.StatusInternalServerError, "无效id") - return - } - todo := &models.Todo{} - if err := dao.DB.Where("id=?", id).Find(todo).Error; err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - c.BindJSON(todo) - if err := dao.DB.Save(todo).Error; err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - c.JSON(http.StatusOK, todo) - }) - //删除 - v1Group.DELETE("/todo/:id", func(c *gin.Context) { - id, ok := c.Params.Get("id") - ID, _ := strconv.Atoi(id) - if !ok { - c.JSON(http.StatusInternalServerError, "无效id") - return - } - dao.DB.Delete(&models.Todo{Id: ID}) - c.JSON(http.StatusOK, "删除成功") - }) - } - + //路由 + r := routers.Router() r.Run(":9029") } diff --git a/bubble/models/todo.go b/bubble/models/todo.go index 41b469b..e51a6b6 100644 --- a/bubble/models/todo.go +++ b/bubble/models/todo.go @@ -44,7 +44,15 @@ func FindAll() (*[]Todo, error) { func FindById(id string) (*Todo, error) { todo := &Todo{} if err := dao.DB.Where("id=?", id).Find(todo).Error; err != nil { - return nil, err + return &Todo{}, err } return todo, nil } + +func DeleteById(id int) error { + return dao.DB.Delete(&Todo{Id: id}).Error +} + +func UpdateTodo(todo *Todo) error { + return dao.DB.Save(todo).Error +} diff --git a/bubble/routers/routers.go b/bubble/routers/routers.go new file mode 100644 index 0000000..a6dda4b --- /dev/null +++ b/bubble/routers/routers.go @@ -0,0 +1,21 @@ +package routers + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func Router() *gin.Engine { + r := gin.Default() + //告诉滚、框架模板文件引用的静态文件去哪里找 + r.Static("/static", "static") + //告诉gin框架 去哪里找模板文件 + r.LoadHTMLGlob("templates/**") + + r.GET("/", func(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", nil) + }) + V1(r) + V2(r) + return r +} diff --git a/bubble/routers/v1.go b/bubble/routers/v1.go new file mode 100644 index 0000000..0f73460 --- /dev/null +++ b/bubble/routers/v1.go @@ -0,0 +1,24 @@ +package routers + +import ( + "github.com/gin-gonic/gin" + "studyGin/bubble/controller" +) + +func V1(e *gin.Engine) { + //v1 + v1Group := e.Group("/v1") + { + //待办事项 + //添加 + v1Group.POST("/todo", controller.AddTodo) + //查看所有 + v1Group.GET("/todo", controller.FindAll) + //通过id查看 + v1Group.GET("/todo/:id", controller.FindById) + //修改 + v1Group.PUT("/todo/:id", controller.Update) + //删除 + v1Group.DELETE("/todo/:id", controller.Delete) + } +} diff --git a/bubble/routers/v2.go b/bubble/routers/v2.go new file mode 100644 index 0000000..7ac2d12 --- /dev/null +++ b/bubble/routers/v2.go @@ -0,0 +1,15 @@ +package routers + +import ( + "github.com/gin-gonic/gin" + "studyGin/bubble/controller" +) + +func V2(e *gin.Engine) { + //v1 + v1Group := e.Group("/v2") + { + //查看所有 + v1Group.GET("/ok", controller.Get) + } +}