Commit 34e8c7c5 authored by shajiaiming's avatar shajiaiming

钱包-链对应关系

parent 4c4e7b60
package models package models
import ( import (
"github.com/jinzhu/gorm"
"bwallet/pkg/setting" "bwallet/pkg/setting"
"github.com/jinzhu/gorm"
) )
type Chain struct { type Chain struct {
ID int `json:"primary_key"` Model
Id int `json:"primary_key"`
Platform string `json:"platform"` //平行链名称 Platform string `json:"platform"` //平行链名称
Address string `json:"address"` //代扣地址 Address string `json:"address"` //代扣地址
PrivateKey string `json:"private_key"` //代扣私钥 PrivateKey string `json:"private_key"` //代扣私钥
......
package models
import (
"bwallet/pkg/setting"
"github.com/jinzhu/gorm"
)
type PlatformChain struct {
Model
Id int `json:"primary_key,omitempty"`
ChainId int `json:"chain_id"`
PlatformId int `json:"platform_id"`
Chain Chain `json:"Chain"`
Platform Wallet `json:"Platform"`
}
func (c PlatformChain) TableName() string {
return setting.DatabaseSetting.Name_Sources + ".wallet_platform_chain"
}
func GetPlatformChain(id int) (*PlatformChain, error) {
var platformChain PlatformChain
err := db.Where("id = ?", id).First(&platformChain).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
err = db.Model(&platformChain).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return &platformChain, nil
}
func ExistPlatformChainById(id int) (bool, error) {
var platformChain PlatformChain
err := db.Select("id").Where("id = ?", id).First(&platformChain).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
if platformChain.ID > 0 {
return true, nil
}
return false, nil
}
func GetPlatformChainTotal(maps interface{}) (int, error) {
var count int
if err := db.Debug().Model(&PlatformChain{}).Where(maps).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func GetPlatformChains(pageNum, pageSize int, maps interface{}) ([]*PlatformChain, error) {
var platformChain []*PlatformChain
err := db.Preload("Chain").Preload("Platform").Where(maps).Offset(pageNum).Limit(pageSize).Find(&platformChain).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return platformChain, nil
}
func AddPlatformChain(data map[string]interface{}) (error) {
platformChain := PlatformChain{
PlatformId: data["platform_id"].(int),
ChainId: data["chain_id"].(int),
}
if err := db.Create(&platformChain).Error; err != nil {
return err
}
return nil
}
func DeletePlatformChain(id int) error {
if err := db.Where("id = ?", id).Delete(PlatformChain{}).Error; err != nil {
return err
}
return nil
}
...@@ -11,9 +11,9 @@ type Wallet struct { ...@@ -11,9 +11,9 @@ type Wallet struct {
Name string `gorm:"size:255;not null;" json:"name"` Name string `gorm:"size:255;not null;" json:"name"`
DownloadUrl string `gorm:"not null;" json:"download_url"` DownloadUrl string `gorm:"not null;" json:"download_url"`
Introduce string `gorm:"not null;" json:"introduce"` Introduce string `gorm:"not null;" json:"introduce"`
ChainId int `gorm:"not null;default:0" json:"chain_id"` //ChainId int `gorm:"not null;default:0" json:"chain_id"`
IssueCharge float32 `gorm:"not null;default:0.00" json:"issue_charge"` //IssueCharge float32 `gorm:"not null;default:0.00" json:"issue_charge"`
ChargeUnit string `gorm:"not null;default:BTY" json:"charge_unit"` //ChargeUnit string `gorm:"not null;default:BTY" json:"charge_unit"`
} }
func (w Wallet) TableName() string { func (w Wallet) TableName() string {
...@@ -73,9 +73,9 @@ func AddWallet(data map[string]interface{}) (error) { ...@@ -73,9 +73,9 @@ func AddWallet(data map[string]interface{}) (error) {
Name: data["name"].(string), Name: data["name"].(string),
DownloadUrl: data["download_url"].(string), DownloadUrl: data["download_url"].(string),
Introduce: data["introduce"].(string), Introduce: data["introduce"].(string),
ChargeUnit: data["charge_unit"].(string), //ChargeUnit: data["charge_unit"].(string),
IssueCharge: data["issue_charge"].(float32), //IssueCharge: data["issue_charge"].(float32),
ChainId: data["chain_id"].(int), //ChainId: data["chain_id"].(int),
} }
if err := db.Create(&wallet).Error; err != nil { if err := db.Create(&wallet).Error; err != nil {
return err return err
......
...@@ -103,6 +103,14 @@ var ( ...@@ -103,6 +103,14 @@ var (
ErrDeleteSupportedCurrency = &Errno{Code: 20102, Message: "The supported currency delete error."} ErrDeleteSupportedCurrency = &Errno{Code: 20102, Message: "The supported currency delete error."}
ErrExistSupportedCurrency = &Errno{Code: 20103, Message: "The supported currency already exists."} ErrExistSupportedCurrency = &Errno{Code: 20103, Message: "The supported currency already exists."}
// wallet supported chain errors
ErrWalletSupportedChainNotFound = &Errno{Code: 20101, Message: "The wallet supported chain was not found."}
ErrCountWalletSupportedChain = &Errno{Code: 20102, Message: "The wallet supported chain statistic error."}
ErrAddWalletSupportedChain = &Errno{Code: 20101, Message: "The wallet supported chain add error."}
ErrUpdateWalletSupportedChain = &Errno{Code: 20102, Message: "The wallet supported chain update error."}
ErrDeleteWalletSupportedChain = &Errno{Code: 20102, Message: "The wallet supported chain delete error."}
ErrExistWalletSupportedChain = &Errno{Code: 20103, Message: "The wallet supported chain already exists."}
// user errors // user errors
ErrUserTokenIncorrect = &Errno{Code: 40001, Message: "The user token was incorrect."} ErrUserTokenIncorrect = &Errno{Code: 40001, Message: "The user token was incorrect."}
ErrUserAuthIncorrect = &Errno{Code: 40001, Message: "The user auth was incorrect."} ErrUserAuthIncorrect = &Errno{Code: 40001, Message: "The user auth was incorrect."}
......
package v1
import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/auth_service"
"bwallet/service/platform_chain_service"
"bwallet/validate_service"
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"strings"
)
func GetPlatformChains(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
platform_id = auth.PlatformId
if ("administrator" == group) {
if arg := c.Query("platform_id"); arg != "" {
platform_id = com.StrTo(c.Query("platform_id")).MustInt()
}
}
platformChainService := platform_chain_service.PlatformChain{
PlatformId: platform_id,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
total, err := platformChainService.Count()
if err != nil {
handler.SendResponse(c, errno.ErrCountWalletSupportedChain, nil)
return
}
coinRecommends, err := platformChainService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
data := make(map[string]interface{})
data["items"] = coinRecommends
data["total"] = total
handler.SendResponse(c, nil, data)
}
func AddPlatformChain(c *gin.Context) {
platform_chain := validate_service.PlatformChain{}
c.ShouldBindJSON(&platform_chain)
//方法一
if ok, errors := validate_service.ValidateInputs(platform_chain); !ok {
for _, err := range errors {
handler.SendResponse(c, errno.ErrBind, strings.Join(err, " "))
return
}
}
//自定义验证
platformChainValidate := platform_chain_service.PlatformChain{
PlatformId: platform_chain.PlatformId,
ChainId: platform_chain.ChainId,
}
total, err := platformChainValidate.Count()
if err != nil {
handler.SendResponse(c, errno.ErrCountWalletSupportedChain, nil)
return
}
if (total > 0) {
handler.SendResponse(c, errno.ErrExistWalletSupportedChain, nil)
return
}
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
var platform_id int
platform_id = auth.PlatformId
if ("administrator" == group) {
if platform_chain.PlatformId != 0 {
platform_id = platform_chain.PlatformId
}
}
platformChainService := platform_chain_service.PlatformChain{
PlatformId: platform_id,
ChainId: platform_chain.ChainId,
}
if err := platformChainService.Add(); err != nil {
handler.SendResponse(c, errno.ErrAddWalletSupportedChain, nil)
return
}
handler.SendResponse(c, nil, nil)
}
func DeletePlatformChain(c *gin.Context) {
valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt()
valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() {
handler.SendResponse(c, errno.ErrValidation, nil)
return
}
platformChainService := platform_chain_service.PlatformChain{Id: id}
exists, err := platformChainService.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrWalletSupportedChainNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrWalletSupportedChainNotFound, nil)
return
}
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
if ("administrator" != group) {
platform_chain, _ := platformChainService.Get()
if platform_chain.PlatformId != auth.PlatformId {
handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
}
err = platformChainService.Delete()
if err != nil {
handler.SendResponse(c, errno.ErrDeleteWalletSupportedChain, nil)
return
}
handler.SendResponse(c, nil, nil)
}
...@@ -186,9 +186,9 @@ func AddWallet(c *gin.Context) { ...@@ -186,9 +186,9 @@ func AddWallet(c *gin.Context) {
Name: wallet.Name, Name: wallet.Name,
DownloadUrl: wallet.DownloadUrl, DownloadUrl: wallet.DownloadUrl,
Introduce: wallet.Introduce, Introduce: wallet.Introduce,
ChainId: wallet.ChainId, //ChainId: wallet.ChainId,
IssueCharge: wallet.IssueCharge, //IssueCharge: wallet.IssueCharge,
ChargeUnit: wallet.ChargeUnit, //ChargeUnit: wallet.ChargeUnit,
} }
if err := walletService.Add(); err != nil { if err := walletService.Add(); err != nil {
...@@ -216,9 +216,9 @@ func EditWallet(c *gin.Context) { ...@@ -216,9 +216,9 @@ func EditWallet(c *gin.Context) {
Name: wallet.Name, Name: wallet.Name,
DownloadUrl: wallet.DownloadUrl, DownloadUrl: wallet.DownloadUrl,
Introduce: wallet.Introduce, Introduce: wallet.Introduce,
ChainId: wallet.ChainId, //ChainId: wallet.ChainId,
IssueCharge: wallet.IssueCharge, //IssueCharge: wallet.IssueCharge,
ChargeUnit: wallet.ChargeUnit, //ChargeUnit: wallet.ChargeUnit,
} }
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
......
...@@ -61,6 +61,10 @@ func InitRouter() *gin.Engine { ...@@ -61,6 +61,10 @@ func InitRouter() *gin.Engine {
api.PUT("/supported-currency", v1.EditSupportedCurrency) api.PUT("/supported-currency", v1.EditSupportedCurrency)
api.DELETE("/supported-currency/:id", v1.DeleteSupportedCurrency) api.DELETE("/supported-currency/:id", v1.DeleteSupportedCurrency)
api.GET("/currencies", v1.GetCurrencies) api.GET("/currencies", v1.GetCurrencies)
api.GET("/platform-chains", v1.GetPlatformChains)
api.POST("/platform-chain", v1.AddPlatformChain)
api.DELETE("/platform-chain/:id", v1.DeletePlatformChain)
} }
return r return r
......
package platform_chain_service
import (
"bwallet/models"
)
type PlatformChain struct {
Id int
ChainId int
PlatformId int
PageNum int
PageSize int
}
func (c *PlatformChain) Get() (*models.PlatformChain, error) {
platformChain, err := models.GetPlatformChain(c.Id)
if err != nil {
return nil, err
}
return platformChain, nil
}
func (c *PlatformChain) GetAll() ([]*models.PlatformChain, error) {
var coinsRecommend []*models.PlatformChain
coinsRecommend, err := models.GetPlatformChains(c.PageNum, c.PageSize, c.getMaps())
if err != nil {
return nil, err
}
return coinsRecommend, nil
}
func (c *PlatformChain) Add() error {
platformChain := map[string]interface{}{
"platform_id": c.PlatformId,
"chain_id": c.ChainId,
}
if err := models.AddPlatformChain(platformChain); err != nil {
return err
}
return nil
}
func (c *PlatformChain) ExistById() (bool, error) {
return models.ExistPlatformChainById(c.Id)
}
func (c *PlatformChain) Count() (int, error) {
return models.GetPlatformChainTotal(c.getMaps())
}
func (c *PlatformChain) Delete() error {
return models.DeletePlatformChain(c.Id)
}
func (c *PlatformChain) getMaps() (map[string]interface{}) {
maps := make(map[string]interface{})
if c.PlatformId != 0 {
maps["platform_id"] = c.PlatformId
}
if c.ChainId != 0 {
maps["chain_id"] = c.ChainId
}
return maps
}
...@@ -10,9 +10,9 @@ type Wallet struct { ...@@ -10,9 +10,9 @@ type Wallet struct {
Name string Name string
DownloadUrl string DownloadUrl string
Introduce string Introduce string
ChainId int //ChainId int
IssueCharge float32 //IssueCharge float32
ChargeUnit string //ChargeUnit string
PageNum int PageNum int
PageSize int PageSize int
...@@ -44,9 +44,9 @@ func (w *Wallet) Add() error { ...@@ -44,9 +44,9 @@ func (w *Wallet) Add() error {
"name": w.Name, "name": w.Name,
"download_url": w.DownloadUrl, "download_url": w.DownloadUrl,
"introduce": w.Introduce, "introduce": w.Introduce,
"chain_id": w.ChainId, //"chain_id": w.ChainId,
"charge_unit": w.ChargeUnit, //"charge_unit": w.ChargeUnit,
"issue_charge": w.IssueCharge, //"issue_charge": w.IssueCharge,
} }
if err := models.AddWallet(wallet); err != nil { if err := models.AddWallet(wallet); err != nil {
...@@ -61,9 +61,9 @@ func (w *Wallet) Edit() error { ...@@ -61,9 +61,9 @@ func (w *Wallet) Edit() error {
"name": w.Name, "name": w.Name,
"download_url": w.DownloadUrl, "download_url": w.DownloadUrl,
"introduce": w.Introduce, "introduce": w.Introduce,
"chain_id": w.ChainId, //"chain_id": w.ChainId,
"issue_charge": w.IssueCharge, //"issue_charge": w.IssueCharge,
"charge_unit": w.ChargeUnit, //"charge_unit": w.ChargeUnit,
}) })
} }
......
package validate_service
type PlatformChain struct {
ChainId int `json:"chain_id" validate:"required"`
PlatformId int `json:"platform_id" validate:"required"`
}
type EditPlatformChain struct {
Id int `json:"id" validate:"required"`
}
...@@ -4,9 +4,9 @@ type Wallet struct { ...@@ -4,9 +4,9 @@ type Wallet struct {
Name string `json:"name" validate:"required"` Name string `json:"name" validate:"required"`
DownloadUrl string `json:"download_url" validate:"required,url"` DownloadUrl string `json:"download_url" validate:"required,url"`
Introduce string `json:"introduce" validate:"required"` Introduce string `json:"introduce" validate:"required"`
ChainId int `json:"chain_id" validate:"required"` //ChainId int `json:"chain_id" validate:"required"`
IssueCharge float32 `json:"issue_charge" validate:"required"` //IssueCharge float32 `json:"issue_charge" validate:"required"`
ChargeUnit string `json:"charge_unit" validate:"required"` //ChargeUnit string `json:"charge_unit" validate:"required"`
} }
type EditWallet struct { type EditWallet struct {
......
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