-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_service.go
88 lines (78 loc) · 2.16 KB
/
currency_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package service
import (
"crypto-portfolio/app/model"
"crypto-portfolio/app/util"
_ "crypto-portfolio/app/util"
"errors"
"log"
)
type CurrencyService struct {
currencies []*model.Currency
}
func NewCurrencyService() *CurrencyService {
return &CurrencyService{
currencies: make([]*model.Currency, 0),
}
}
func (s *CurrencyService) AddCurrency(code string, amount float64) (*model.Currency, error) {
for _, c := range s.currencies {
if c.Code == code {
return nil, errors.New("currency already exists")
}
}
price, err := util.GetCryptoPrice(code)
if err != nil {
return nil, err
}
totalValue := price * amount
newCurrency := &model.Currency{
ID: len(s.currencies) + 1,
Code: code,
Amount: amount,
Price: price,
TotalValue: totalValue,
History: []model.Price{},
}
s.currencies = append(s.currencies, newCurrency)
log.Printf("Added currency with code: %s, amount: %f, total value: %f", code, amount, totalValue)
return newCurrency, nil
}
func (s *CurrencyService) EditCurrency(id int, code string, amount float64) (*model.Currency, error) {
for _, c := range s.currencies {
if c.ID == id {
price, err := util.GetCryptoPrice(code)
if err != nil {
return nil, err
}
totalValue := price * amount
c.Code = code
c.Amount = amount
c.Price = price
c.TotalValue = totalValue
log.Printf("Edited currency with ID: %d, new code: %s, new amount: %f, new total value: %f", id, code, amount, totalValue)
return c, nil
}
}
return nil, errors.New("currency with that ID does not exist")
}
func (s *CurrencyService) DeleteCurrency(id int) error {
for i, c := range s.currencies {
if c.ID == id {
s.currencies = append(s.currencies[:i], s.currencies[i+1:]...)
log.Printf("Deleted currency with ID: %d", id)
return nil
}
}
return errors.New("currency with that ID does not exist")
}
func (s *CurrencyService) GetCurrencyByID(id int) (*model.Currency, error) {
for _, c := range s.currencies {
if c.ID == id {
return c, nil
}
}
return nil, errors.New("currency with that ID does not exist")
}
func (s *CurrencyService) GetAllCurrencies() ([]*model.Currency, error) {
return s.currencies, nil
}