/
model.go
44 lines (37 loc) · 1.17 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package mongo
import (
"time"
"github.com/dominikus1993/xbox-promotion-checker-bot/pkg/data"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type screapedXboxHame struct {
ID string `bson:"_id,omitempty"`
Title string `bson:"Title"`
Link string `bson:"Link"`
Price string `bson:"Price"`
OldPrice string `bson:"OldPrice"`
Promotion string `bson:"Promotion"`
CrawledAt primitive.DateTime `bson:"CrawledAt"`
}
func fromXboxGame(game data.XboxStoreGame) screapedXboxHame {
link, err := game.GetLink()
if err != nil {
panic(err)
}
return screapedXboxHame{
ID: game.ID,
Title: game.Title,
Link: link.String(),
Price: game.FormatPrice(),
Promotion: game.FormatPromotionPercentage(),
OldPrice: game.FormatOldPrice(),
CrawledAt: primitive.NewDateTimeFromTime(time.Now()),
}
}
func toMongoWriteModel(games []data.XboxStoreGame) []mongo.WriteModel {
return lo.Map(games, func(game data.XboxStoreGame, _ int) mongo.WriteModel {
return mongo.NewInsertOneModel().SetDocument(fromXboxGame(game))
})
}