-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
45 lines (37 loc) · 1.02 KB
/
stat.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
package dao
import (
"context"
"github.com/aattwwss/telegram-expense-bot/entity"
"github.com/aattwwss/telegram-expense-bot/util"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5/pgxpool"
)
type StatDAO struct {
db *pgxpool.Pool
}
func NewStatDAO(db *pgxpool.Pool) StatDAO {
return StatDAO{db: db}
}
func (dao StatDAO) GetMonthly(ctx context.Context, from util.YearMonth, to util.YearMonth, userId int64) ([]*entity.MonthlySummary, error) {
fromString, err := from.String("2006-01")
if err != nil {
return nil, err
}
toString, err := to.String("2006-01")
if err != nil {
return nil, err
}
var summaries []*entity.MonthlySummary
sql := `
SELECT datetime, amount, transaction_type_label, multiplier
FROM monthly_transaction_agg
WHERE datetime >= TO_TIMESTAMP($1, 'YYYY-MM')
AND datetime <= TO_TIMESTAMP($2, 'YYYY-MM')
AND user_id = $3
`
err = pgxscan.Select(ctx, dao.db, &summaries, sql, fromString, toString, userId)
if err != nil {
return nil, err
}
return summaries, nil
}