-
Notifications
You must be signed in to change notification settings - Fork 0
/
priceagent.go
68 lines (58 loc) · 2.44 KB
/
priceagent.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
67
68
package models
import (
"fmt"
"time"
"github.com/d-Rickyy-b/gogeizhalsbot/v2/internal/geizhals"
)
type PriceAgent struct {
CreatedAt time.Time
ID int64 `json:"id" gorm:"primarykey;autoIncrement:true"`
Name string `json:"name"`
UserID int64 `json:"user_id" gorm:"not null;default:null;index:user_entity_idx,unique"`
User User `json:"user" gorm:"foreignkey:UserID"`
EntityID int64 `json:"-" gorm:"index:user_entity_idx,unique"`
Entity geizhals.Entity `json:"entity" gorm:"foreignkey:EntityID"`
Location string `json:"location" gorm:"default:de"`
NotificationID int64 `json:"-"`
NotificationSettings NotificationSettings `json:"notificationSettings" gorm:"foreignkey:NotificationID;constraint:OnDelete:CASCADE;"`
Enabled bool `json:"enabled" gorm:"default:1"`
}
func (pa PriceAgent) String() string {
return fmt.Sprintf("%d - '%s' (%s) | User: %d", pa.ID, pa.Name, pa.Entity.Name, pa.UserID)
}
func (pa PriceAgent) EntityURL() string {
return pa.Entity.FullURL(pa.Location)
}
func (pa PriceAgent) CurrentEntityPrice() geizhals.EntityPrice {
return pa.Entity.GetPrice(pa.Location)
}
func (pa PriceAgent) CurrentPrice() float64 {
return pa.Entity.GetPrice(pa.Location).Price
}
func (pa PriceAgent) GetCurrency() geizhals.Currency {
return pa.Entity.GetPrice(pa.Location).Currency
}
type NotificationSettings struct {
CreatedAt time.Time
UpdatedAt time.Time
ID int64 `gorm:"primarykey"`
NotifyAlways bool `json:"notifyAlways" gorm:"default:true"`
NotifyPriceDrop bool `json:"notifyPriceDrop" gorm:"default:false"`
NotifyPriceRise bool `json:"notifyPriceRise" gorm:"default:false"`
NotifyAbove bool `json:"notifyAbove" gorm:"default:false"`
NotifyBelow bool `json:"notifyBelow" gorm:"default:false"`
AbovePrice float64 `json:"abovePrice" gorm:"default:0"`
BelowPrice float64 `json:"belowPrice" gorm:"default:0"`
}
func (ns NotificationSettings) String() string {
var humanReadableSettings string
switch {
case ns.NotifyBelow:
humanReadableSettings = fmt.Sprintf("Unter %.2f €", ns.BelowPrice)
case ns.NotifyAlways:
humanReadableSettings = "Immer alarmieren"
default:
humanReadableSettings = "Unbekannt"
}
return humanReadableSettings
}