This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
66 lines (56 loc) · 1.64 KB
/
service.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package service
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/am3o/overwatch/pkg/domain"
"github.com/patrickmn/go-cache"
"go.uber.org/zap"
)
type Messanger interface {
Message(text string) error
}
type Service struct {
collector ProductCollector
messanger Messanger
logger *zap.Logger
products *cache.Cache
}
func New(collector ProductCollector, messanger Messanger, logger *zap.Logger) Service {
return Service{
collector: collector,
messanger: messanger,
logger: logger,
products: cache.New(10*time.Minute, 15*time.Minute),
}
}
func (s Service) Notify(products domain.Products) {
s.logger.With(zap.Int("Products", len(products))).Info("Notified about new received products")
for _, product := range products {
// s.collector.TrackProduct(product.Name, product.Price)
if !strings.Contains(strings.ToLower(product.Name), "geforce") {
continue
}
key := strconv.FormatUint(product.Hash(), 32)
p, ok := s.products.Get(key)
if !ok {
s.products.Set(key, product, cache.DefaultExpiration)
s.messanger.Message(fmt.Sprintf("Neue Karte steht zur Verfügung: %v € \n %v",
product.Price, product.URI))
continue
}
if obj, ok := p.(domain.Product); ok && obj.Price > product.Price {
s.messanger.Message(fmt.Sprintf("Ist im Preis gesunken von %v € auf %v €: \n %v",
obj.Price, product.Price, product.URI))
}
}
}
func (s Service) Check() []string {
var products = make([]string, 0)
for _, item := range s.products.Items() {
product := item.Object.(domain.Product)
products = append(products, fmt.Sprintf("%v: %v € \n %v", product.Name, product.Price, product.URI))
}
return products
}