-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
140 lines (126 loc) · 4.02 KB
/
transaction.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package dao
import (
"context"
"fmt"
"time"
"github.com/aattwwss/telegram-expense-bot/entity"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5/pgxpool"
)
const (
ASC = "ASC"
DESC = "DESC"
)
type TransactionDAO struct {
db *pgxpool.Pool
}
func NewTransactionDao(db *pgxpool.Pool) TransactionDAO {
return TransactionDAO{db: db}
}
func (dao TransactionDAO) GetById(ctx context.Context, id int, userId int64) (entity.Transaction, error) {
var transactions []*entity.Transaction
sql := `
SELECT t.id, t.datetime, t.category_id, t.description, t.user_id, t.amount, t.currency, c.name as category_name
FROM transaction t JOIN category c on t.category_id = c.id
WHERE t.id = $1 and t.user_id = $2
`
err := pgxscan.Select(ctx, dao.db, &transactions, sql, id, userId)
if err != nil {
return entity.Transaction{}, err
}
return *transactions[0], nil
}
func (dao TransactionDAO) FindLatestByUserId(ctx context.Context, userId int64) (*entity.Transaction, error) {
var transactions []*entity.Transaction
sql := `
SELECT id, datetime, category_id, description, user_id, amount, currency
FROM transaction
WHERE user_id = $1
ORDER BY datetime DESC LIMIT 1;
`
err := pgxscan.Select(ctx, dao.db, &transactions, sql, userId)
if err != nil {
return nil, err
}
if len(transactions) == 0 {
return nil, nil
}
return transactions[0], nil
}
func (dao TransactionDAO) Insert(ctx context.Context, transaction entity.Transaction) error {
sql := `
INSERT INTO transaction (datetime, category_id, description, user_id, amount, currency)
VALUES ($1, $2, $3, $4, $5, $6)
`
_, err := dao.db.Exec(ctx, sql, transaction.Datetime, transaction.CategoryId, transaction.Description, transaction.UserId, transaction.Amount, transaction.Currency)
if err != nil {
return err
}
return nil
}
func (dao TransactionDAO) DeleteById(ctx context.Context, id int, userId int64) error {
sql := `
DELETE FROM transaction
WHERE id = $1 AND user_id = $2 ;
`
_, err := dao.db.Exec(ctx, sql, id, userId)
if err != nil {
return err
}
return nil
}
func (dao TransactionDAO) GetBreakdownByCategory(ctx context.Context, dateFrom time.Time, dateTo time.Time, userId int64) ([]entity.TransactionBreakdown, error) {
var entities []entity.TransactionBreakdown
sql := `
SELECT c.name as category_name,
sum(t.amount) amount
FROM transaction t JOIN category c on t.category_id = c.id
WHERE datetime >= $1::timestamptz
AND datetime < $2::timestamptz
AND user_id = $3
GROUP BY c.name
ORDER BY amount DESC;
`
err := pgxscan.Select(ctx, dao.db, &entities, sql, dateFrom.Format(time.RFC3339), dateTo.Format(time.RFC3339), userId)
if err != nil {
return nil, err
}
return entities, nil
}
func (dao TransactionDAO) ListByMonthAndYear(ctx context.Context, dateFrom time.Time, dateTo time.Time, offset int, limit int, isAsc bool, userId int64) ([]entity.Transaction, error) {
sortOrder := ASC
if !isAsc {
sortOrder = DESC
}
var entities []entity.Transaction
sql := `
SELECT t.id, t.datetime, t.category_id, t.description, t.user_id, t.amount, t.currency, c.name as category_name
FROM transaction t JOIN category c on t.category_id = c.id
WHERE t.datetime >= $1::timestamptz
AND t.datetime < $2::timestamptz
AND t.user_id = $3
ORDER BY t.datetime %s
OFFSET $4 LIMIT $5
`
sql = fmt.Sprintf(sql, sortOrder)
err := pgxscan.Select(ctx, dao.db, &entities, sql, dateFrom.Format(time.RFC3339), dateTo.Format(time.RFC3339), userId, offset, limit)
if err != nil {
return nil, err
}
return entities, nil
}
func (dao TransactionDAO) CountListByMonthAndYear(ctx context.Context, dateFrom time.Time, dateTo time.Time, userId int64) (int, error) {
var count int
sql := `
SELECT COUNT(*)
FROM transaction t
WHERE t.datetime >= $1::timestamptz
AND t.datetime < $2::timestamptz
AND t.user_id = $3
`
err := dao.db.QueryRow(ctx, sql, dateFrom.Format(time.RFC3339), dateTo.Format(time.RFC3339), userId).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}