拆分服务

This commit is contained in:
朱毅骏 2021-07-22 11:16:13 +08:00
parent 7145c4f651
commit 3f034f86c7
7 changed files with 134 additions and 123 deletions

View File

@ -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, "删除成功")
}

View File

@ -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",
})
}

View File

@ -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")
}

View File

@ -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
}

21
bubble/routers/routers.go Normal file
View File

@ -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
}

24
bubble/routers/v1.go Normal file
View File

@ -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)
}
}

15
bubble/routers/v2.go Normal file
View File

@ -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)
}
}