Commit 118814fa authored by shajiaiming's avatar shajiaiming

fix

parent b16c21ed
package models
import (
"github.com/jinzhu/gorm"
"bwallet/pkg/setting"
"encoding/json"
"github.com/jinzhu/gorm"
)
type Coin struct {
Model
Id int `json:"primary_key,omitempty"`
Sid string `json:"sid,omitempty"` //全称
Name string `json:"name,omitempty"` //简称
Nickname string `json:"nickname,omitempty"` //全称
Icon string `json:"icon,omitempty"` //币种图标
Introduce string `json:"introduce,omitempty"` //币种介绍
Official string `json:"official,omitempty"` //官网
Paper string `json:"paper,omitempty"` //白皮书
Platform string `json:"platform,omitempty"` //平台
Chain string `json:"chain,omitempty"` //主链
Release string `json:"release,omitempty"` //发行时间
AreaSearch string `json:"area_search,omitempty"` //区块查询
PublishCount float64 `json:"publish_count,omitempty"` //发行总量
CirculateCount float64 `json:"circulate_count,omitempty"` //流通总量
Decimals int `json:"decimals,omitempty"` //币种精度
Address string `json:"address,omitempty"` //合约地址
Treaty int `json:"treaty,omitempty"` //币种类型
PlatformId int `json:"platform_id,omitempty"`
//OptionalName string `json:"optional_name,omitempty"`
//Exchange int `json:"exchange,omitempty"`
//Price float64 `json:"price,omitempty"`
Id int `json:"primary_key"`
Sid string `json:"sid"` //全称
Name string `json:"name"` //简称
Nickname json.RawMessage `json:"nickname"` //全称
Icon string `json:"icon"` //币种图标
Introduce json.RawMessage `json:"introduce"` //币种介绍
Official string `json:"official"` //官网
Paper string `json:"paper"` //白皮书
Platform string `json:"platform"` //平台
Chain string `json:"chain"` //主链
Release string `json:"release"` //发行时间
AreaSearch string `json:"area_search"` //区块查询
PublishCount float64 `json:"publish_count"` //发行总量
CirculateCount float64 `json:"circulate_count"` //流通总量
Decimals int `json:"decimals"` //币种精度
Address string `json:"address"` //合约地址
Treaty int `json:"treaty"` //币种类型
PlatformId int `json:"platform_id"`
//OptionalName string `json:"optional_name"`
//Exchange int `json:"exchange"`
//Price float64 `json:"price"`
}
func (c Coin) TableName() string {
......@@ -114,9 +115,9 @@ func AddCoin(data map[string]interface{}) (error) {
coin := Coin{
Sid: data["sid"].(string),
Name: data["name"].(string),
Nickname: "{\"ja\": \"\", \"en-US\": \"\", \"zh-CN\": \"ICON\"}",
Nickname: data["nickname"].(json.RawMessage),
Icon: data["icon"].(string),
Introduce: "{\"ja\": \"\", \"en-US\": \"\", \"zh-CN\": \"ICON\"}",
Introduce: data["introduce"].(json.RawMessage),
Official: data["official"].(string),
Paper: data["paper"].(string),
Platform: data["platform"].(string),
......
......@@ -10,62 +10,9 @@ import (
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"gopkg.in/go-playground/validator.v9"
"reflect"
"strings"
)
func validateChainInputs(dataSet interface{}) (bool, map[string][]string) {
validate := validator.New()
err := validate.Struct(dataSet)
if err != nil {
//Validation syntax is invalid
if err, ok := err.(*validator.InvalidValidationError); ok {
panic(err)
}
//Validation errors occurred
errors := make(map[string][]string)
//Use reflector to reverse engineer struct
reflected := reflect.ValueOf(dataSet)
for _, err := range err.(validator.ValidationErrors) {
// Attempt to find field by name and get json tag name
field, _ := reflected.Type().FieldByName(err.StructField())
var name string
//If json tag doesn't exist, use lower case of name
if name = field.Tag.Get("json"); name == "" {
name = strings.ToLower(err.StructField())
}
switch err.Tag() {
case "required":
errors[name] = append(errors[name], "The "+name+" is required")
break
case "email":
errors[name] = append(errors[name], "The "+name+" should be a valid email")
break
case "url":
errors[name] = append(errors[name], "The "+name+" should be a valid url")
break
case "eqfield":
errors[name] = append(errors[name], "The "+name+" should be equal to the "+err.Param())
break
default:
errors[name] = append(errors[name], "The "+name+" is invalid")
break
}
}
return false, errors
}
return true, nil
}
func GetChain(c *gin.Context) {
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
......@@ -167,7 +114,7 @@ func AddChain(c *gin.Context) {
c.ShouldBindJSON(&chain)
//方法一
if ok, errors := validateChainInputs(chain); !ok {
if ok, errors := validate_service.ValidateInputs(chain); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......@@ -206,7 +153,7 @@ func EditChain(c *gin.Context) {
c.ShouldBindJSON(&chain)
//方法一
if ok, errors := validateChainInputs(chain); !ok {
if ok, errors := validate_service.ValidateInputs(chain); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......
......@@ -7,65 +7,13 @@ import (
"bwallet/service/auth_service"
"bwallet/service/coin_service"
"bwallet/validate_service"
"fmt"
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"gopkg.in/go-playground/validator.v9"
"reflect"
"strings"
)
func validateCoinInputs(dataSet interface{}) (bool, map[string][]string) {
validate := validator.New()
err := validate.Struct(dataSet)
if err != nil {
//Validation syntax is invalid
if err, ok := err.(*validator.InvalidValidationError); ok {
panic(err)
}
//Validation errors occurred
errors := make(map[string][]string)
//Use reflector to reverse engineer struct
reflected := reflect.ValueOf(dataSet)
for _, err := range err.(validator.ValidationErrors) {
// Attempt to find field by name and get json tag name
field, _ := reflected.Type().FieldByName(err.StructField())
var name string
//If json tag doesn't exist, use lower case of name
if name = field.Tag.Get("json"); name == "" {
name = strings.ToLower(err.StructField())
}
switch err.Tag() {
case "required":
errors[name] = append(errors[name], "The "+name+" is required")
break
case "email":
errors[name] = append(errors[name], "The "+name+" should be a valid email")
break
case "url":
errors[name] = append(errors[name], "The "+name+" should be a valid url")
break
case "eqfield":
errors[name] = append(errors[name], "The "+name+" should be equal to the "+err.Param())
break
default:
errors[name] = append(errors[name], "The "+name+" is invalid")
break
}
}
return false, errors
}
return true, nil
}
func GetCoin(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{}
......@@ -170,8 +118,9 @@ func GetCoins(c *gin.Context) {
func AddCoin(c *gin.Context) {
coin := validate_service.Coin{}
c.ShouldBindJSON(&coin)
fmt.Println(coin)
//方法一
if ok, errors := validateCoinInputs(coin); !ok {
if ok, errors := validate_service.ValidateInputs(coin); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......@@ -221,7 +170,7 @@ func EditCoin(c *gin.Context) {
c.ShouldBindJSON(&coin)
//方法一
if ok, errors := validateCoinInputs(coin); !ok {
if ok, errors := validate_service.ValidateInputs(coin); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......
......@@ -10,62 +10,9 @@ import (
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"gopkg.in/go-playground/validator.v9"
"reflect"
"strings"
)
func validateRecommendCoinInputs(dataSet interface{}) (bool, map[string][]string) {
validate := validator.New()
err := validate.Struct(dataSet)
if err != nil {
//Validation syntax is invalid
if err, ok := err.(*validator.InvalidValidationError); ok {
panic(err)
}
//Validation errors occurred
errors := make(map[string][]string)
//Use reflector to reverse engineer struct
reflected := reflect.ValueOf(dataSet)
for _, err := range err.(validator.ValidationErrors) {
// Attempt to find field by name and get json tag name
field, _ := reflected.Type().FieldByName(err.StructField())
var name string
//If json tag doesn't exist, use lower case of name
if name = field.Tag.Get("json"); name == "" {
name = strings.ToLower(err.StructField())
}
switch err.Tag() {
case "required":
errors[name] = append(errors[name], "The "+name+" is required")
break
case "email":
errors[name] = append(errors[name], "The "+name+" should be a valid email")
break
case "url":
errors[name] = append(errors[name], "The "+name+" should be a valid url")
break
case "eqfield":
errors[name] = append(errors[name], "The "+name+" should be equal to the "+err.Param())
break
default:
errors[name] = append(errors[name], "The "+name+" is invalid")
break
}
}
return false, errors
}
return true, nil
}
func GetRecommendCoins(c *gin.Context) {
token := c.Request.Header.Get("Token")
......@@ -111,7 +58,7 @@ func AddRecommendCoin(c *gin.Context) {
c.ShouldBindJSON(&recommend_coin)
//方法一
if ok, errors := validateRecommendCoinInputs(recommend_coin); !ok {
if ok, errors := validate_service.ValidateInputs(recommend_coin); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......@@ -150,7 +97,7 @@ func EditRecommendCoin(c *gin.Context) {
c.ShouldBindJSON(&recommend_coin)
//方法一
if ok, errors := validateWalletInputs(recommend_coin); !ok {
if ok, errors := validate_service.ValidateInputs(recommend_coin); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......
......@@ -10,62 +10,9 @@ import (
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"gopkg.in/go-playground/validator.v9"
"reflect"
"strings"
)
func validateWalletInputs(dataSet interface{}) (bool, map[string][]string) {
validate := validator.New()
err := validate.Struct(dataSet)
if err != nil {
//Validation syntax is invalid
if err, ok := err.(*validator.InvalidValidationError); ok {
panic(err)
}
//Validation errors occurred
errors := make(map[string][]string)
//Use reflector to reverse engineer struct
reflected := reflect.ValueOf(dataSet)
for _, err := range err.(validator.ValidationErrors) {
// Attempt to find field by name and get json tag name
field, _ := reflected.Type().FieldByName(err.StructField())
var name string
//If json tag doesn't exist, use lower case of name
if name = field.Tag.Get("json"); name == "" {
name = strings.ToLower(err.StructField())
}
switch err.Tag() {
case "required":
errors[name] = append(errors[name], "The "+name+" is required")
break
case "email":
errors[name] = append(errors[name], "The "+name+" should be a valid email")
break
case "url":
errors[name] = append(errors[name], "The "+name+" should be a valid url")
break
case "eqfield":
errors[name] = append(errors[name], "The "+name+" should be equal to the "+err.Param())
break
default:
errors[name] = append(errors[name], "The "+name+" is invalid")
break
}
}
return false, errors
}
return true, nil
}
func GetWallet(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{}
......@@ -178,7 +125,7 @@ func AddWallet(c *gin.Context) {
c.ShouldBindJSON(&wallet)
//方法一
if ok, errors := validateWalletInputs(wallet); !ok {
if ok, errors := validate_service.ValidateInputs(wallet); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......@@ -257,7 +204,7 @@ func EditWallet(c *gin.Context) {
c.ShouldBindJSON(&wallet)
//方法一
if ok, errors := validateWalletInputs(wallet); !ok {
if ok, errors := validate_service.ValidateInputs(wallet); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
......
......@@ -3,15 +3,17 @@ package coin_service
import (
"bwallet/models"
"encoding/json"
//"encoding/json"
)
type Coin struct {
Id int
Sid string
Name string
Nickname string
Nickname json.RawMessage
Icon string
Introduce string
Introduce json.RawMessage
Official string
Paper string
Platform string
......@@ -40,10 +42,10 @@ func (c *Coin) Get() (*models.Coin, error) {
return nil, err
}
var m map[string]interface{}
if err := json.Unmarshal([]byte(coin.Nickname), &m); err == nil {
coin.Nickname = m["zh-CN"].(string)
}
//var m map[string]interface{}
//if err := json.Unmarshal([]byte(coin.Nickname), &m); err == nil {
// coin.Nickname = m["zh-CN"].(string)
//}
return coin, nil
}
......@@ -60,12 +62,12 @@ func (c *Coin) GetAll() ([]*models.Coin, error) {
return nil, err
}
var m map[string]interface{}
for _, value := range coins {
if err := json.Unmarshal([]byte(value.Nickname), &m); err == nil {
value.Nickname = m["zh-CN"].(string)
}
}
//var m map[string]interface{}
//for _, value := range coins {
// if err := json.Unmarshal([]byte(value.Nickname), &m); err == nil {
// value.Nickname = m["zh-CN"].(string)
// }
//}
return coins, nil
}
......
package validate_service
import "encoding/json"
type Coin struct {
Sid string `json:"sid" validate:"required"`
Name string `json:"name" validate:"required"`
Nickname string `json:"nickname" validate:"required"`
Icon string `json:"icon" validate:"required"`
Introduce string `json:"introduce" validate:"required"`
Sid string `json:"sid" validate:"required"`
Name string `json:"name" validate:"required"`
Nickname json.RawMessage `json:"nickname" validate:"required"`
Icon string `json:"icon" validate:"required"`
Introduce json.RawMessage `json:"introduce" validate:"required"`
Official string `json:"official" validate:"required"`
Paper string `json:"paper" validate:"required"`
......
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