Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davenathanael committed Jun 28, 2019
0 parents commit 03fdaa9
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/db.go
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
}
7 changes: 7 additions & 0 deletions controllers/gorm.go
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
}
124 changes: 124 additions & 0 deletions controllers/person.go
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"})
}

}
27 changes: 27 additions & 0 deletions main.go
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")
}
11 changes: 11 additions & 0 deletions models/person.go
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
}

0 comments on commit 03fdaa9

Please sign in to comment.