Created
July 10, 2019 08:45
-
-
Save farkroft/8bec7be10755652332e15de572b8de6f to your computer and use it in GitHub Desktop.
Gin Rest Tutorial
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
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/gin-gonic/gin" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/postgres" | |
) | |
var db *gorm.DB | |
var err error | |
// Person struct | |
type Person struct { | |
// gorm.Model | |
PersonID uint `json:"PersonID" gorm:"primary_key"` | |
FirstName string `json:"FirstName" gorm:"type:varchar(100)"` | |
LastName string `json:"LastName" gorm:"type:varchar(100)"` | |
CreatedAt time.Time `json:"CreatedAt"` | |
UpdatedAt time.Time `json:"UpdatedAt"` | |
DeletedAt time.Time `json:"DeletedAt"` | |
} | |
// Cars struct | |
type Cars struct { | |
CarsID uint `json:"CarsID" gorm:"Primary_key"` | |
Brand string `json:"Brand" gorm:"type:varchar(100)"` | |
Name string `json:"Name" gorm:"type:varchar(100)"` | |
CreatedAt time.Time `json:"CreatedAt"` | |
UpdatedAt time.Time `json:"UpdatedAt"` | |
DeletedAt time.Time `json:"DeletedAt"` | |
} | |
func main() { | |
db, err = gorm.Open("postgres", "host=localhost port=5432 user=postgres dbname=rest_gin_tutorial password=postgres sslmode=disable") | |
if err != nil { | |
fmt.Println(err) | |
log.Panic(err) | |
} | |
log.Println("Connection established ...") | |
// db.Debug().DropTableIfExists(&Person{}, &Cars{}) | |
db.Debug().AutoMigrate(&Person{}, &Cars{}) | |
defer db.Close() | |
// db.AutoMigrate(&Person{}) | |
r := gin.Default() | |
// Person API | |
r.GET("/", GetAllPerson) | |
r.GET("/people/:id", GetPerson) | |
r.POST("/people", CreatePerson) | |
r.PUT("/people/:id", UpdatePerson) | |
r.DELETE("/people/:id", DeletePerson) | |
// Cars API | |
r.GET("/cars", GetAllCars) | |
r.GET("/cars/:id", GetAllCars) | |
r.POST("/cars", CreateCars) | |
r.PUT("/cars/:id", UpdateCar) | |
r.DELETE("/cars/:id", DeleteCar) | |
r.Run(":8080") | |
} | |
// CreatePerson to create new person | |
func CreatePerson(c *gin.Context) { | |
var person Person | |
c.BindJSON(&person) | |
db.Create(&person) | |
c.JSON(200, person) | |
} | |
// GetAllPerson func to run when root page accessed | |
func GetAllPerson(c *gin.Context) { | |
people := []Person{} | |
// fmt.Println(people) | |
if err := db.Find(&people).Error; err != nil { | |
// if err := db.Select("person_id").Find(&people).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} else { | |
c.JSON(200, people) | |
} | |
} | |
// GetPerson func to get a person details | |
func GetPerson(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var person Person | |
if err := db.Where("person_id = ?", id).First(&person).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} else { | |
c.JSON(200, person) | |
} | |
} | |
// UpdatePerson func to update a person detail | |
func UpdatePerson(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var person Person | |
if err := db.Where("id = ?", id).First(&person).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} | |
c.BindJSON(&person) | |
db.Save(&person) | |
c.JSON(200, person) | |
} | |
// DeletePerson func to delete a person | |
func DeletePerson(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var person Person | |
if err := db.Where("id = ?", id).First(&person).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} | |
d := db.Where("id = ?", id).Delete(&person) | |
fmt.Println(d) | |
c.JSON(200, gin.H{"id #" + id: "deleted"}) | |
} | |
// GetAllCars func to run when root page accessed | |
func GetAllCars(c *gin.Context) { | |
var cars []Cars | |
if err := db.Find(&cars).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} else { | |
c.JSON(200, cars) | |
} | |
} | |
// CreateCars to create new person | |
func CreateCars(c *gin.Context) { | |
var car Cars | |
c.BindJSON(&car) | |
db.Create(&car) | |
c.JSON(200, car) | |
} | |
// GetCar func to get a person details | |
func GetCar(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var car Cars | |
if err := db.Where("cars_id = ?", id).First(&car).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} else { | |
c.JSON(200, car) | |
} | |
} | |
// UpdateCar func to update a person detail | |
func UpdateCar(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var car Cars | |
if err := db.Where("id = ?", id).First(&car).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} | |
c.BindJSON(&car) | |
db.Save(&car) | |
c.JSON(200, car) | |
} | |
// DeleteCar func to delete a person | |
func DeleteCar(c *gin.Context) { | |
id := c.Params.ByName("id") | |
var car Cars | |
if err := db.Where("id = ?", id).First(&car).Error; err != nil { | |
c.AbortWithStatus(404) | |
fmt.Println(err) | |
} | |
d := db.Where("id = ?", id).Delete(&car) | |
fmt.Println(d) | |
c.JSON(200, gin.H{"id #" + id: "deleted"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment