Commit a4447955 authored by szh's avatar szh

update model

parent ca7c6f7b
...@@ -8,13 +8,22 @@ import ( ...@@ -8,13 +8,22 @@ import (
"time" "time"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
ttype "github.com/33cn/plugin/plugin/dapp/ticket/types" ttype "github.com/33cn/plugin/plugin/dapp/ticket/types"
"github.com/jinzhu/gorm"
) )
func init() { func init() {
height,err := models.GetHeight() height,err := models.GetHeight()
if err != nil { if err != nil {
if err == gorm.ErrRecordNotFound {
err = models.InitHeight()
if err != nil {
panic(err)
}
} else {
panic(err) panic(err)
} }
}
prev.LocalHeight = height prev.LocalHeight = height
prev.SetMainHeight() prev.SetMainHeight()
} }
......
package models package models
import (
"github.com/jinzhu/gorm"
)
type RaspConfig struct { type RaspConfig struct {
Model Model
Height int64 `json:"height"` Height int64 `json:"height"`
...@@ -11,12 +7,15 @@ type RaspConfig struct { ...@@ -11,12 +7,15 @@ type RaspConfig struct {
// GetHeight get lastheight in chain33 // GetHeight get lastheight in chain33
func GetHeight() (int64, error) { func GetHeight() (int64, error) {
var raspConfig RaspConfig var list []*RaspConfig
err := db.Select("height").First(&raspConfig).Error err := db.Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound { if err != nil {
return 0, err //if err == gorm.ErrRecordNotFound {
// return 0, err
//}
return 0,err
} }
return raspConfig.Height, nil return list[0].Height, nil
} }
// EditArticle modify a single article // EditArticle modify a single article
...@@ -29,9 +28,9 @@ func UpdateHeight(height int64) error { ...@@ -29,9 +28,9 @@ func UpdateHeight(height int64) error {
} }
// AddArticle add a single article // AddArticle add a single article
func InitHeight(data map[string]interface{}) error { func InitHeight() error {
config := RaspConfig{ config := RaspConfig{
Height: data["height"].(int64), Height: 0,
} }
if err := db.Create(&config).Error; err != nil { if err := db.Create(&config).Error; err != nil {
return err 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