Added SCG errors handler
This commit is contained in:
parent
6348a729d1
commit
4ad7d8286e
1 changed files with 19 additions and 8 deletions
|
|
@ -3,11 +3,12 @@ package vk
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/flygrounder/go-mtg-vk/caching"
|
"github.com/flygrounder/go-mtg-vk/caching"
|
||||||
"github.com/flygrounder/go-mtg-vk/cardsinfo"
|
"github.com/flygrounder/go-mtg-vk/cardsinfo"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const CARDSLIMIT = 8
|
const CARDSLIMIT = 8
|
||||||
|
|
@ -41,7 +42,11 @@ func handleSearch(c *gin.Context, req *MessageRequest) {
|
||||||
} else if cardName == "" {
|
} else if cardName == "" {
|
||||||
Message(req.Object.UserId, "Карта не найдена")
|
Message(req.Object.UserId, "Карта не найдена")
|
||||||
} else {
|
} else {
|
||||||
prices := GetPrices(cardName)
|
prices, err := GetPrices(cardName)
|
||||||
|
if err != nil {
|
||||||
|
Message(req.Object.UserId, "Цены временно недоступны, попробуйте позже")
|
||||||
|
return
|
||||||
|
}
|
||||||
elements := min(CARDSLIMIT, len(prices))
|
elements := min(CARDSLIMIT, len(prices))
|
||||||
prices = prices[:elements]
|
prices = prices[:elements]
|
||||||
priceInfo := cardsinfo.FormatCardPrices(cardName, prices)
|
priceInfo := cardsinfo.FormatCardPrices(cardName, prices)
|
||||||
|
|
@ -49,18 +54,24 @@ func handleSearch(c *gin.Context, req *MessageRequest) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPrices(cardName string) []cardsinfo.CardPrice {
|
func GetPrices(cardName string) ([]cardsinfo.CardPrice, error) {
|
||||||
client := caching.GetClient()
|
client := caching.GetClient()
|
||||||
val, err := client.Get(cardName)
|
val, err := client.Get(cardName)
|
||||||
var prices []cardsinfo.CardPrice
|
var prices []cardsinfo.CardPrice
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prices, _ = cardsinfo.GetSCGPrices(cardName)
|
prices, err = cardsinfo.GetSCGPrices(cardName)
|
||||||
serialized, _ := json.Marshal(prices)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
serialized, err := json.Marshal(prices)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
client.Set(cardName, string(serialized))
|
client.Set(cardName, string(serialized))
|
||||||
return prices
|
return prices, nil
|
||||||
}
|
}
|
||||||
json.Unmarshal([]byte(val), &prices)
|
json.Unmarshal([]byte(val), &prices)
|
||||||
return prices
|
return prices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCardNameByCommand(command string) (string, error) {
|
func getCardNameByCommand(command string) (string, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue