Commit daf62281 authored by shajiaiming's avatar shajiaiming

fix

parent 7adb0e5c
......@@ -15,12 +15,15 @@ require (
github.com/go-siris/siris v7.4.0+incompatible // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/gomodule/redigo v2.0.1-0.20190322064113-39e2c31b7ca3+incompatible
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/jinzhu/gorm v1.9.10
github.com/kr/pretty v0.2.0 // indirect
github.com/labstack/echo v3.3.10+incompatible // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/oxequa/interact v0.0.0-20171114182912-f8fb5795b5d7 // indirect
github.com/oxequa/realize v2.0.2+incompatible // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/valyala/fasttemplate v1.1.0 // indirect
gopkg.in/go-playground/validator.v9 v9.29.1
......
......@@ -91,6 +91,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORR
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/gorm v1.9.10 h1:HvrsqdhCW78xpJF67g1hMxS6eCToo9PZH4LDB8WKPac=
github.com/jinzhu/gorm v1.9.10/go.mod h1:Kh6hTsSGffh4ui079FHrR5Gg+5D0hgihqDcsDN2BBJY=
......@@ -148,6 +150,10 @@ github.com/oxequa/realize v2.0.2+incompatible h1:R+Rg8R+gyuWP8oqvFpaJMzdcFF0vy15
github.com/oxequa/realize v2.0.2+incompatible/go.mod h1:Bqw5jC78Eh70s7/rryEaVPwps/kYdPxBWTZDE+6x0/8=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a h1:Tg4E4cXPZSZyd3H1tJlYo6ZreXV0ZJvE/lorNqyw1AU=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a/go.mod h1:9Or9aIl95Kp43zONcHd5tLZGKXb9iLx0pZjau0uJ5zg=
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 h1:XXDLZIIt9NqdeIEva0DM+z1npM0Tsx6h5TYqwNvXfP0=
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017/go.mod h1:2LLTtftTZSdAPR/iVyennXZDLZOYzyDn+T0qEKJ8eSw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
......
......@@ -9,7 +9,7 @@ import (
type Coin struct {
Model
Id int `json:"primary_key"`
Id int `json:"primary_key, id,omitempty"`
Sid string `json:"sid"` //全称
Name string `json:"name"` //简称
Nickname json.RawMessage `json:"nickname"` //全称
......@@ -27,10 +27,6 @@ type Coin struct {
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 {
......@@ -130,10 +126,6 @@ func AddCoin(data map[string]interface{}) (error) {
Address: data["address"].(string),
PlatformId: data["platform_id"].(int),
Treaty: data["treaty"].(int),
//OptionalName: data["optional_name"].(string),
//Exchange: data["exchange"].(int),
//Price: data["price"].(float64),
}
if err := db.Create(&coin).Error; err != nil {
return err
......
......@@ -7,7 +7,7 @@ import (
type WalletCoinRelation struct {
Model
Id int `json:"id,omitempty"`
PlatformId int `json:"platform_id"` //钱包ID
CoinId int `json:"coin_id"` //币种ID
......@@ -49,6 +49,23 @@ func GetWalletCoinRelations(pageNum, pageSize int, maps interface{}) ([]*WalletC
}
/**
* 添加币种钱包对应关系
* @param
* @return
*/
func AddWalletCoinRelation(data map[string]interface{}) (error) {
wallet_coin_relateion := WalletCoinRelation{
PlatformId: data["platform_id"].(int),
CoinId: data["coin_id"].(int),
}
if err := db.Create(&wallet_coin_relateion).Error; err != nil {
return err
}
return nil
}
/**
* 删除钱包币关系
* @param
* @return
......
......@@ -72,6 +72,8 @@ var (
ErrAddCoin = &Errno{Code: 20101, Message: "The coin add error."}
ErrUpdateCoin = &Errno{Code: 20102, Message: "The coin update error."}
ErrDeleteCoin = &Errno{Code: 20102, Message: "The coin delete error."}
ErrExistCoin = &Errno{Code: 20103, Message: "The coin already exists."}
ErrNotExistCoin = &Errno{Code: 20104, Message: "The coin not exists."}
// recommend coin errors
ErrRecommendCoinNotFound = &Errno{Code: 20101, Message: "The recommend coin was not found."}
......@@ -110,7 +112,6 @@ var (
ErrDeleteSupportedChain = &Errno{Code: 20102, Message: "The wallet supported chain delete error."}
ErrExistSupportedChain = &Errno{Code: 20103, Message: "The wallet supported chain already exists."}
// user errors
ErrUserTokenIncorrect = &Errno{Code: 40001, Message: "The user token was incorrect."}
ErrUserAuthIncorrect = &Errno{Code: 40001, Message: "The user auth was incorrect."}
......
......@@ -5,8 +5,12 @@ import (
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/auth_service"
"bwallet/service/coin_service"
"bwallet/service/wallet_coin_relation_service"
"bwallet/validate_service"
"github.com/Unknwon/com"
"github.com/gin-gonic/gin"
"strings"
)
func GetWalletCoinRelationCoinRelations(c *gin.Context) {
......@@ -14,8 +18,13 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) {
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
var platform_id int
if ("administrator" == group) {
platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
} else {
platform_id = auth.PlatformId
}
walletCoinRelateionService := wallet_coin_relation_service.WalletCoinRelation{}
......@@ -43,3 +52,121 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) {
handler.SendResponse(c, nil, data)
}
/**
* 添加币种钱包对应关系
* @param
* @return
*/
func AddWalletCoinRelationCoinRelation(c *gin.Context) {
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
if ("administrator" != group) {
handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
coin_relation := validate_service.WalletCoinRelateion{}
c.ShouldBindJSON(&coin_relation)
//方法一
if ok, errors := validate_service.ValidateInputs(coin_relation); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
}
}
coinService := coin_service.Coin{
Id: coin_relation.CoinId,
}
exists, err := coinService.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
relationService := wallet_coin_relation_service.WalletCoinRelation{
CoinId: coin_relation.CoinId,
PlatformId: coin_relation.PlatformId,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
coins, err := relationService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
if 0 != len(coins) {
handler.SendResponse(c, errno.ErrExistCoin, nil)
return
}
if err := relationService.Add(); err != nil {
handler.SendResponse(c, errno.ErrAddCoin, nil)
return
}
handler.SendResponse(c, nil, nil)
}
/**
* 删除币种钱包对应关系
* @param
* @return
*/
func DeleteWalletCoinRelationCoinRelation(c *gin.Context) {
var platform_id, coin_id int
if arg := c.Query("platform_id"); arg != "" {
platform_id = com.StrTo(c.Query("platform_id")).MustInt()
}
if arg := c.Query("coin_id"); arg != "" {
coin_id = com.StrTo(c.Query("coin_id")).MustInt()
}
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
if ("administrator" != group) {
if platform_id != auth.PlatformId {
handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
}
if (0 == platform_id || 0 == coin_id) {
handler.SendResponse(c, errno.ErrValidation, nil)
return
}
relationService := wallet_coin_relation_service.WalletCoinRelation{
PlatformId: platform_id,
CoinId: coin_id,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
coins, err := relationService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
if 0 == len(coins) {
handler.SendResponse(c, errno.ErrNotExistCoin, nil)
return
}
err = relationService.Delete()
if err != nil {
handler.SendResponse(c, errno.ErrDeleteCoin, nil)
return
}
handler.SendResponse(c, nil, nil)
}
......@@ -30,10 +30,10 @@ func InitRouter() *gin.Engine {
//api.POST("/upload",v1.Upload)
api.GET("/coins", v1.GetWalletCoinRelationCoinRelations)
api.POST("/coin", v1.AddCoin)
api.POST("/coin", v1.AddWalletCoinRelationCoinRelation)
api.GET("/coin/:id", v1.GetCoin)
api.PUT("/coin", v1.EditCoin)
api.DELETE("/coin/:id", v1.DeleteCoin)
api.DELETE("/coin", v1.DeleteWalletCoinRelationCoinRelation)
api.GET("/primary-chains", v1.GetPrimaryChains)
api.GET("/types", v1.GetTypes)
//api.GET("/get-platform", v1.GetCoinsPlatform)
......
......@@ -12,13 +12,18 @@ type WalletCoinRelation struct {
PageSize int
}
func (w *WalletCoinRelation) GetAll() ([]*models.WalletCoinRelation, error) {
var coins []*models.WalletCoinRelation
func (w *WalletCoinRelation) GetAll() ([]*models.Coin, error) {
var wallet_coin_relations []*models.WalletCoinRelation
coins, err := models.GetWalletCoinRelations(w.PageNum, w.PageSize, w.getMaps())
wallet_coin_relations, err := models.GetWalletCoinRelations(w.PageNum, w.PageSize, w.getMaps())
if err != nil {
return nil, err
}
var coins []*models.Coin
for _, value := range wallet_coin_relations {
coins = append(coins, value.Coin)
}
return coins, nil
}
......@@ -27,12 +32,39 @@ func (w *WalletCoinRelation) Count() (int, error) {
return models.GetWalletCoinRelationTotal(w.getMaps())
}
/**
* 添加币种钱包对应关系
* @param
* @return
*/
func (c *WalletCoinRelation) Add() error {
relation := map[string]interface{}{
"coin_id": c.CoinId,
"platform_id": c.PlatformId,
}
if err := models.AddWalletCoinRelation(relation); err != nil {
return err
}
return nil
}
/**
* 删除币种钱包对应关系
* @param
* @return
*/
func (c *WalletCoinRelation) Delete() error {
return models.DeleteWalletCoinRelation(c.PlatformId, c.CoinId)
}
func (c *WalletCoinRelation) getMaps() (map[string]interface{}) {
maps := make(map[string]interface{})
//if c.PlatformId != 0 {
// maps["platform_id"] = c.PlatformId
//}
if c.PlatformId != 0 {
maps["platform_id"] = c.PlatformId
}
if c.CoinId != 0 {
maps["coin_id"] = c.CoinId
......
package validate_service
type WalletCoinRelateion struct {
CoinId int `json:"coin_id" validate:"required"`
PlatformId int `json:"platform_id" validate:"required"`
}
type DeleteWalletCoinRelateion struct {
CoinId int `json:"coin_id" validate:"required"`
PlatformId int `json:"platform_id" 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