2018 Golang Update(3)REST API with ORM

2018 Golang Update(3)REST API with ORM

Database Layer
https://github.com/go-xorm/xorm
http://xorm.io/

MySQL Driver
https://github.com/go-sql-driver/mysql

Library
>go get github.com/go-xorm/xorm
>go get github.com/go-sql-driver/mysql

I find most of the CURD methods in here
https://github.com/go-xorm/xorm

Here is how to init the DB connection in restful_go_api/src/sillycat.com/waterrecord/db.go
package waterrecord

import (
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/go-xorm/core"
    "github.com/go-xorm/xorm"
)

func initDatabase() *xorm.Engine {
    var engine *xorm.Engine
    var err error
    engine, err = xorm.NewEngine("mysql", "watermonitor:kaishi117A@tcp(sillycat.ddns.net:3306)/watermonitor?charset=utf8")
    if err != nil {
        fmt.Println(err)
    }
    engine.Ping()        //ping
    engine.ShowSQL(true) //show SQL
    engine.Logger().SetLevel(core.LOG_DEBUG)
    return engine
}

Method and mapping in restful_go_api/sillycat.com/restful_go_api/waterrecord/waterrecord.go

package waterrecord

import (
    "github.com/gin-gonic/gin"
    "log"
    "strconv"
    "time"
)

type WaterRecord struct {
    Id          int64
    Location    string    `xorm:"location varchar(256) not null"`
    StartTime   string    `xorm:"start_time varchar(256) not null"`
    EndTime     string    `xorm:"end_time varchar(256) not null"`
    Duration    int       `xorm:"duration int(11)"`
    ReleaseDate string    `xorm:"release_date varchar(256)"`
    CreateDate  time.Time `xorm:"create_date DATETIME "`
    UpdateDate  time.Time `xorm:"update_date DATETIME"`
}

var engine = initDatabase()

func (waterRecord WaterRecord) TableName() string {
    return "water_monitor"
}

func GetWaterRecords(c *gin.Context) {
    var items []WaterRecord
    query := "SELECT id, location, start_time, end_time, duration, release_date, create_date, update_date FROM water_monitor"
    err := engine.Sql(query).Find(&items)
    if err != nil {
        log.Fatal("Get err when exec query: ", err)
    }
    log.Println("query result: ", items)
    c.JSON(200, items)
}

func GetWaterRecord(c *gin.Context) {
    var item WaterRecord
    id := c.Param("id")

    has, err := engine.Where("id = ?", id).Get(&item)
    if err != nil {
        log.Fatal("Get one item err: ", err)
    }
    if has {
        c.JSON(200, item)
    } else {
        c.JSON(404, gin.H{"error": "WaterRecord not found"})
    }
}

func PostWaterRecord(c *gin.Context) {
    var item WaterRecord
    c.Bind(&item)

    if item.Location != "" && item.ReleaseDate != "" {
        _, err := engine.Insert(&item)
        if err != nil {
            log.Fatal("Insert fail:", err)
        } else {
            c.JSON(201, item)
        }
    } else {
        c.JSON(422, gin.H{"error": "fields are empty!"})
    }

}

func UpdateWaterRecord(c *gin.Context) {
    var item WaterRecord
    id := c.Param("id")
    c.Bind(&item)

    has, err := engine.SQL("select * from water_monitor where id = ?", id).Exist()
    if err != nil {
        log.Fatal("Fail to find item:", err)
    } else {
        if has {
            item.Id, _ = strconv.ParseInt(id, 0, 64)
            _, err := engine.Id(id).Update(&item)
            if err != nil {
                log.Fatal("Fail to update item:", err)
            } else {
                c.JSON(200, item)
            }
        } else {
            c.JSON(404, gin.H{"error": "Water Record not found"})
        }
    }
}

func DeleteWaterRecord(c *gin.Context) {
    id := c.Param("id")

    item := new(WaterRecord)

    has, err := engine.SQL("select * from water_monitor where id = ?", id).Exist()
    if err != nil {
        log.Fatal("Fail to find item:", err)
    } else {
        if has {
            //itemID, _ := strconv.ParseInt(id, 0, 64)
            _, err := engine.Where("id = ?", id).Delete(item)
            if err != nil {
                log.Fatal("Fail to delete:", err)
            } else {
                c.JSON(202, gin.H{"id": id})
            }
        } else {
            c.JSON(404, gin.H{"error": "Water Record not found"})
        }
    }
}

It works great on local MAC and remote CentOS and RaspberryPi

#restful_go_api

Prepare the library

>go get github.com/gin-gonic/gin

>go get github.com/go-xorm/xorm

>go get github.com/go-sql-driver/mysql

How to Build for local

>go install sillycat.com/restful_go_api

How to Run Test

>go test sillycat.com/restful_go_api

How to Build for Linux

>env GOOS=linux GOARCH=amd64 go build -o bin/restful_go_api_linux -v sillycat.com/restful_go_api

How to Build for Raspberrypi

>env GOOS=linux GOARCH=arm GOARM=7 go build -o bin/restful_go_api_arm -v sillycat.com/restful_go_api

How to Run

>bin/restful_go_api

Maybe Later, I can change the layer to Model, Dao, Controller like Java, haha.

References:
http://sillycat.iteye.com/admin/blogs/2411149
http://solee.me/2016/10/26/golang-ginyu-xormjian-dan-shi-jian/

build for RaspberryPi
https://www.thepolyglotdeveloper.com/2017/04/cross-compiling-golang-applications-raspberry-pi/



猜你喜欢

转载自sillycat.iteye.com/blog/2411150