Redis -> YDB
This commit is contained in:
parent
1f4888069f
commit
a33680d527
21 changed files with 448 additions and 322 deletions
|
|
@ -1,7 +1,9 @@
|
|||
package scenario
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
|
|
@ -27,8 +29,9 @@ type UserMessage struct {
|
|||
}
|
||||
|
||||
type CardCache interface {
|
||||
Get(cardName string) ([]cardsinfo.ScgCardPrice, error)
|
||||
Set(cardName string, prices []cardsinfo.ScgCardPrice)
|
||||
Init(ctx context.Context) error
|
||||
Get(ctx context.Context, cardName string) ([]cardsinfo.ScgCardPrice, error)
|
||||
Set(ctx context.Context, cardName string, prices []cardsinfo.ScgCardPrice) error
|
||||
}
|
||||
|
||||
type CardInfoFetcher interface {
|
||||
|
|
@ -42,7 +45,7 @@ type Sender interface {
|
|||
SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice)
|
||||
}
|
||||
|
||||
func (s *Scenario) HandleSearch(msg *UserMessage) {
|
||||
func (s *Scenario) HandleSearch(ctx context.Context, msg *UserMessage) {
|
||||
cardName, err := s.getCardNameByCommand(msg.Body)
|
||||
if err != nil {
|
||||
s.Sender.Send(msg.UserId, incorrectMessage)
|
||||
|
|
@ -51,7 +54,7 @@ func (s *Scenario) HandleSearch(msg *UserMessage) {
|
|||
s.Sender.Send(msg.UserId, cardNotFoundMessage)
|
||||
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
|
||||
} else {
|
||||
prices, err := s.Cache.Get(cardName)
|
||||
prices, err := s.Cache.Get(ctx, cardName)
|
||||
if err == nil {
|
||||
s.Sender.SendPrices(msg.UserId, cardName, prices)
|
||||
return
|
||||
|
|
@ -62,7 +65,10 @@ func (s *Scenario) HandleSearch(msg *UserMessage) {
|
|||
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
||||
return
|
||||
}
|
||||
s.Cache.Set(cardName, prices)
|
||||
err = s.Cache.Set(ctx, cardName, prices)
|
||||
if err != nil {
|
||||
s.Logger.Println(fmt.Errorf("failed add entry in cache: %w", err))
|
||||
}
|
||||
s.Sender.SendPrices(msg.UserId, cardName, prices)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package scenario
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -9,7 +10,7 @@ import (
|
|||
|
||||
func TestScenario_HandleSearch_BadCommand(t *testing.T) {
|
||||
testCtx := GetTestScenarioCtx()
|
||||
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||
testCtx.Scenario.HandleSearch(context.Background(), &UserMessage{
|
||||
Body: "!s",
|
||||
UserId: 1,
|
||||
})
|
||||
|
|
@ -24,7 +25,7 @@ func TestScenario_HandleSearch_BadCommand(t *testing.T) {
|
|||
|
||||
func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
|
||||
testCtx := GetTestScenarioCtx()
|
||||
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||
testCtx.Scenario.HandleSearch(context.Background(), &UserMessage{
|
||||
Body: "!s grn 228",
|
||||
UserId: 1,
|
||||
})
|
||||
|
|
@ -38,7 +39,7 @@ func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
|
|||
|
||||
func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
|
||||
testCtx := GetTestScenarioCtx()
|
||||
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||
testCtx.Scenario.HandleSearch(context.Background(), &UserMessage{
|
||||
Body: "absolutely_random_card",
|
||||
UserId: 1,
|
||||
})
|
||||
|
|
@ -53,7 +54,7 @@ func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
|
|||
|
||||
func TestScenario_HandleSearch_BadCard(t *testing.T) {
|
||||
testCtx := GetTestScenarioCtx()
|
||||
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||
testCtx.Scenario.HandleSearch(context.Background(), &UserMessage{
|
||||
Body: "bad",
|
||||
UserId: 1,
|
||||
})
|
||||
|
|
@ -67,7 +68,7 @@ func TestScenario_HandleSearch_BadCard(t *testing.T) {
|
|||
}
|
||||
func TestScenario_HandleSearch_Uncached(t *testing.T) {
|
||||
testCtx := GetTestScenarioCtx()
|
||||
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||
testCtx.Scenario.HandleSearch(context.Background(), &UserMessage{
|
||||
Body: "uncached",
|
||||
UserId: 1,
|
||||
})
|
||||
|
|
@ -77,6 +78,6 @@ func TestScenario_HandleSearch_Uncached(t *testing.T) {
|
|||
message: "uncached",
|
||||
},
|
||||
}, testCtx.Sender.sent)
|
||||
_, err := testCtx.Scenario.Cache.Get("uncached")
|
||||
_, err := testCtx.Scenario.Cache.Get(context.Background(), "uncached")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package scenario
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
|
|
@ -10,7 +11,11 @@ type testCache struct {
|
|||
table map[string][]cardsinfo.ScgCardPrice
|
||||
}
|
||||
|
||||
func (t *testCache) Get(cardName string) ([]cardsinfo.ScgCardPrice, error) {
|
||||
func (t *testCache) Init(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testCache) Get(ctx context.Context, cardName string) ([]cardsinfo.ScgCardPrice, error) {
|
||||
msg, ok := t.table[cardName]
|
||||
if !ok {
|
||||
return nil, errors.New("test")
|
||||
|
|
@ -18,6 +23,7 @@ func (t *testCache) Get(cardName string) ([]cardsinfo.ScgCardPrice, error) {
|
|||
return msg, nil
|
||||
}
|
||||
|
||||
func (t *testCache) Set(cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||
func (t *testCache) Set(ctx context.Context, cardName string, prices []cardsinfo.ScgCardPrice) error {
|
||||
t.table[cardName] = prices
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue