From 03fdaa908eba4cc7181bd9f066726f6cfbc2d1ec Mon Sep 17 00:00:00 2001 From: davenathanael Date: Fri, 28 Jun 2019 15:57:40 +0700 Subject: [PATCH] Initial commit --- config/db.go | 20 +++++++ controllers/gorm.go | 7 +++ controllers/person.go | 124 ++++++++++++++++++++++++++++++++++++++++++ main.go | 27 +++++++++ models/person.go | 11 ++++ 5 files changed, 189 insertions(+) create mode 100644 config/db.go create mode 100644 controllers/gorm.go create mode 100644 controllers/person.go create mode 100644 main.go create mode 100644 models/person.go diff --git a/config/db.go b/config/db.go new file mode 100644 index 0000000..9f8b1ff --- /dev/null +++ b/config/db.go @@ -0,0 +1,20 @@ +package config + +import ( + "web/models" + + "github.com/jinzhu/gorm" +) + +var dbURL = "user:password@tcp(localhost:9306)/gotest?parseTime=true" + +func DBInit() *gorm.DB { + db, err := gorm.Open("mysql", dbURL) + if err != nil { + panic("Failed to connect to database, " + err.Error()) + } + + db.AutoMigrate(models.Person{}) + + return db +} diff --git a/controllers/gorm.go b/controllers/gorm.go new file mode 100644 index 0000000..ddf8139 --- /dev/null +++ b/controllers/gorm.go @@ -0,0 +1,7 @@ +package controllers + +import "github.com/jinzhu/gorm" + +type InDB struct { + DB *gorm.DB +} diff --git a/controllers/person.go b/controllers/person.go new file mode 100644 index 0000000..49aef5c --- /dev/null +++ b/controllers/person.go @@ -0,0 +1,124 @@ +package controllers + +import ( + "net/http" + + "web/models" + + "github.com/gin-gonic/gin" +) + +func (idb *InDB) GetPerson(c *gin.Context) { + var ( + person models.Person + result gin.H + ) + + id := c.Param("id") + + err := idb.DB.Where("id = ?", id).First(&person).Error + + if err != nil { + result = gin.H{ + "result": err.Error(), + "count": 0, + } + } else { + result = gin.H{ + "result": person, + "count": 1, + } + } + + c.JSON(http.StatusOK, result) + +} + +func (idb *InDB) GetPersons(c *gin.Context) { + var ( + persons []models.Person + result gin.H + ) + + idb.DB.Find(&persons) + + if len(persons) <= 0 { + result = gin.H{ + "result": nil, + "count": 0, + } + } else { + result = gin.H{ + "result": persons, + "count": len(persons), + } + } + + c.JSON(http.StatusOK, result) +} + +func (idb *InDB) CreatePerson(c *gin.Context) { + var ( + person models.Person + result gin.H + ) + + if err := c.ShouldBindJSON(&person); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + idb.DB.Create(&person) + result = gin.H{ + "result": person, + } + c.JSON(http.StatusOK, result) +} + +func (idb *InDB) DeletePerson(c *gin.Context) { + var ( + person models.Person + result gin.H + ) + + id := c.Param("id") + + err := idb.DB.Where("id = ?", id).First(&person).Error + + if err != nil { + result = gin.H{"result": err.Error()} + } else { + idb.DB.Delete(&person) + result = gin.H{"result": "Successfully deleted person with id " + id} + } + + c.JSON(http.StatusOK, result) +} + +func (idb *InDB) UpdatePerson(c *gin.Context) { + var ( + person models.Person + newPerson models.Person + ) + + id := c.Param("id") + + err := idb.DB.First(&person, id).Error + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"result": err.Error()}) + return + } + + if err := c.ShouldBindJSON(&newPerson); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + err = idb.DB.Model(&person).Update(newPerson).Error + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"result": "update failed"}) + } else { + c.JSON(http.StatusBadRequest, gin.H{"result": "update success"}) + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..1ff4256 --- /dev/null +++ b/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "./config" + "./controllers" + "github.com/gin-gonic/gin" + _ "github.com/go-sql-driver/mysql" +) + +func main() { + // ==================================== INIT ==================================== + + db := config.DBInit() + controller := &controllers.InDB{DB: db} + + // ==================================== ROUTER ==================================== + + router := gin.Default() + + router.GET("/person", controller.GetPersons) + router.GET("/person/:id", controller.GetPerson) + router.POST("/person", controller.CreatePerson) + router.DELETE("/person/:id", controller.DeletePerson) + router.PUT("/person/:id", controller.UpdatePerson) + + router.Run(":8080") +} diff --git a/models/person.go b/models/person.go new file mode 100644 index 0000000..e76fdc9 --- /dev/null +++ b/models/person.go @@ -0,0 +1,11 @@ +package models + +import ( + "github.com/jinzhu/gorm" +) + +type Person struct { + gorm.Model + Name string + Age uint +}