-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03fdaa9
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package controllers | ||
|
||
import "github.com/jinzhu/gorm" | ||
|
||
type InDB struct { | ||
DB *gorm.DB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/jinzhu/gorm" | ||
) | ||
|
||
type Person struct { | ||
gorm.Model | ||
Name string | ||
Age uint | ||
} |