-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
candlesticks.go
73 lines (61 loc) · 1.9 KB
/
candlesticks.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
package sql
import (
"context"
"fmt"
"time"
"github.com/digital-feather/cryptellation/services/candlesticks/internal/application/ports/db"
"github.com/digital-feather/cryptellation/services/candlesticks/internal/infrastructure/db/sql/entities"
"github.com/digital-feather/cryptellation/services/candlesticks/pkg/models/candlestick"
)
func (d *DB) CreateCandlesticks(ctx context.Context, cs *candlestick.List) error {
listCE := entities.FromModelListToEntityList(cs)
return d.client.WithContext(ctx).Create(&listCE).Error
}
func (d *DB) ReadCandlesticks(ctx context.Context, cs *candlestick.List, start, end time.Time, limit uint) error {
tx := d.client.Where(`
exchange_name = ? AND
pair_symbol = ? AND
period_symbol = ? AND
time BETWEEN ? AND ?`,
cs.ExchangeName(),
cs.PairSymbol(),
cs.Period().String(),
start, end)
if limit != 0 {
tx = tx.Limit(int(limit))
}
cse := []entities.Candlestick{}
if err := tx.WithContext(ctx).Find(&cse).Error; err != nil {
return err
}
for _, ce := range cse {
_, _, _, t, m := ce.ToModel()
if err := cs.Set(t, m); err != nil {
return err
}
}
return nil
}
func (d *DB) UpdateCandlesticks(ctx context.Context, cs *candlestick.List) error {
listCE := entities.FromModelListToEntityList(cs)
for _, ce := range listCE {
tx := d.client.WithContext(ctx).
Select("*").
Model(&entities.Candlestick{}).
Where("exchange_name = ?", ce.ExchangeName).
Where("pair_symbol = ?", ce.PairSymbol).
Where("period_symbol = ?", ce.PeriodSymbol).
Where("time = ?", ce.Time).
Updates(ce)
if tx.Error != nil {
return fmt.Errorf("updating candlestick %q: %w", ce.Time, tx.Error)
} else if tx.RowsAffected == 0 {
return db.ErrNotFound
}
}
return nil
}
func (d *DB) DeleteCandlesticks(ctx context.Context, cs *candlestick.List) error {
listCE := entities.FromModelListToEntityList(cs)
return d.client.WithContext(ctx).Delete(&listCE).Error
}