Update Coin data if slug is an empty string

pull/275/head
Vuong 3 years ago
parent df5bc19544
commit 9ae18814ea
No known key found for this signature in database
GPG Key ID: 9E32EEF440B0A5D4

@ -1,6 +1,10 @@
package cointop
import log "github.com/sirupsen/logrus"
import (
"errors"
log "github.com/sirupsen/logrus"
)
// Coin is the row structure
type Coin struct {
@ -92,3 +96,42 @@ func (ct *Cointop) CoinByID(id string) *Coin {
}
return nil
}
// UpdateCoin updates coin info after fetching from API
func (ct *Cointop) UpdateCoin(coin *Coin) error {
log.Debug("UpdateCoin()")
v, err := ct.api.GetCoinData(coin.Name, ct.State.currencyConversion)
if err != nil {
log.Debugf("UpdateCoin() could not fetch coin data %s", coin.Name)
return err
}
k, found := ct.State.allCoinsSlugMap.Load(coin.Name)
if !found {
log.Debugf("UpdateCoin() could not found coin %s in the slug map", coin.Name)
return errors.New("could not find coin index in allCoinsSlugMap")
}
coin = &Coin{
ID: v.ID,
Name: v.Name,
Symbol: v.Symbol,
Rank: v.Rank,
Price: v.Price,
Volume24H: v.Volume24H,
MarketCap: v.MarketCap,
AvailableSupply: v.AvailableSupply,
TotalSupply: v.TotalSupply,
PercentChange1H: v.PercentChange1H,
PercentChange24H: v.PercentChange24H,
PercentChange7D: v.PercentChange7D,
PercentChange30D: v.PercentChange30D,
PercentChange1Y: v.PercentChange1Y,
LastUpdated: v.LastUpdated,
Slug: v.Slug,
}
ct.State.allCoinsSlugMap.Store(k, coin)
return nil
}

@ -165,6 +165,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) {
c.PercentChange1Y = cm.PercentChange1Y
c.LastUpdated = cm.LastUpdated
c.Favorite = cm.Favorite
c.Slug = cm.Slug
}
}

@ -197,6 +197,15 @@ func (ct *Cointop) RowLink() string {
return ""
}
// TODO: Can remove this one after some releases
// because it is a way to force old client refresh coin to have a slug
if coin.Slug == "" {
if err := ct.UpdateCoin(coin); err != nil {
log.Debugf("RowLink() Update coin got err %s", err.Error())
return ""
}
}
return ct.api.CoinLink(coin.Slug)
}

Loading…
Cancel
Save