Commit a4447955 authored by szh's avatar szh

update model

parent ca7c6f7b
......@@ -8,12 +8,21 @@ import (
"time"
"github.com/33cn/chain33/types"
ttype "github.com/33cn/plugin/plugin/dapp/ticket/types"
"github.com/jinzhu/gorm"
)
func init() {
height,err := models.GetHeight()
if err != nil {
panic(err)
if err == gorm.ErrRecordNotFound {
err = models.InitHeight()
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
prev.LocalHeight = height
prev.SetMainHeight()
......
package models
import (
"github.com/jinzhu/gorm"
)
type RaspConfig struct {
Model
Height int64 `json:"height"`
......@@ -11,12 +7,15 @@ type RaspConfig struct {
// GetHeight get lastheight in chain33
func GetHeight() (int64, error) {
var raspConfig RaspConfig
err := db.Select("height").First(&raspConfig).Error
if err != nil && err != gorm.ErrRecordNotFound {
return 0, err
var list []*RaspConfig
err := db.Find(&list).Error
if err != nil {
//if err == gorm.ErrRecordNotFound {
// return 0, err
//}
return 0,err
}
return raspConfig.Height, nil
return list[0].Height, nil
}
// EditArticle modify a single article
......@@ -29,9 +28,9 @@ func UpdateHeight(height int64) error {
}
// AddArticle add a single article
func InitHeight(data map[string]interface{}) error {
func InitHeight() error {
config := RaspConfig{
Height: data["height"].(int64),
Height: 0,
}
if err := db.Create(&config).Error; err != nil {
return err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment