impl priceiswrong.Item

main
Bel LaPointe 2025-02-12 15:59:39 -07:00
parent fa46313386
commit a7910adc26
4 changed files with 33 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"gitea/price-is-wrong/src/lib/db"
priceiswrong "gitea/price-is-wrong/src/state/fsm/priceiswrong/internal"
"html"
"math/rand"
)
@ -25,6 +26,10 @@ func (p *PriceIsWrong) apply(e priceiswrong.Event) error {
p.Contestants[i].Score += e.Score
}
}
case *priceiswrong.Item:
p.Item.ImageURL = e.ImageURL
p.Item.Title = html.EscapeString(e.Title)
p.Item.Description = html.EscapeString(e.Description)
}
return nil
}
@ -41,6 +46,14 @@ func (p *PriceIsWrong) Score(ctx context.Context, id, score int) error {
return p.upsertEvent(ctx, &priceiswrong.Score{ID: id, Score: score})
}
func (p *PriceIsWrong) SetItem(ctx context.Context, imageURL, title, description string) error {
return p.upsertEvent(ctx, &priceiswrong.Item{
ImageURL: imageURL,
Title: title,
Description: description,
})
}
func (p *PriceIsWrong) upsertEvent(ctx context.Context, e priceiswrong.Event) error {
if err := upsertEvent(ctx, p.id, e); err != nil {
return err

View File

@ -20,11 +20,18 @@ type Score struct {
Score int
}
type Item struct {
ImageURL string
Title string
Description string
}
func ParseEvent(b []byte) (Event, error) {
typesToPointers := map[string]any{
"*priceiswrong.Players": &Players{},
"*priceiswrong.Host": &Host{},
"*priceiswrong.Score": &Score{},
"*priceiswrong.Item": &Item{},
}
t, err := event.Parse(b, typesToPointers)
return typesToPointers[t], err

View File

@ -12,6 +12,7 @@ type PriceIsWrong struct {
id int
Host int
Contestants []Player
Item priceiswrong.Item
}
type Player struct {

View File

@ -39,4 +39,16 @@ func TestPriceIsWrong(t *testing.T) {
} else if p.Contestants[1].Score != 0 {
t.Errorf("gave someone else points: %+v", p.Contestants)
}
if err := p.SetItem(ctx, "a", "b", "c"); err != nil {
t.Fatal(err)
} else if err := p.SetItem(ctx, "imageURL", "title", "description"); err != nil {
t.Fatal(err)
} else if p.Item.ImageURL != "imageURL" {
t.Errorf("wrong image url: %s", p.Item.ImageURL)
} else if p.Item.Title != "title" {
t.Errorf("wrong title: %s", p.Item.Title)
} else if p.Item.Description != "description" {
t.Errorf("wrong description: %s", p.Item.Description)
}
}