-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
60 lines (53 loc) · 1.38 KB
/
category.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
package dao
import (
"context"
"github.com/aattwwss/telegram-expense-bot/entity"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5/pgxpool"
)
type CategoryDAO struct {
db *pgxpool.Pool
}
func NewCategoryDAO(db *pgxpool.Pool) CategoryDAO {
return CategoryDAO{db: db}
}
func (dao CategoryDAO) FindAll(ctx context.Context) ([]*entity.Category, error) {
var categories []*entity.Category
sql := `
SELECT id, name, transaction_type_id
FROM category
ORDER BY display_order
`
err := pgxscan.Select(ctx, dao.db, &categories, sql)
if err != nil {
return nil, err
}
return categories, nil
}
func (dao CategoryDAO) FindByTransactionTypeId(ctx context.Context, transactionTypeId int) ([]*entity.Category, error) {
var categories []*entity.Category
sql := `
SELECT id, name, transaction_type_id
FROM category
WHERE transaction_type_id = $1
ORDER BY display_order
`
err := pgxscan.Select(ctx, dao.db, &categories, sql, transactionTypeId)
if err != nil {
return nil, err
}
return categories, nil
}
func (dao CategoryDAO) GetById(ctx context.Context, id int) (entity.Category, error) {
var categories []*entity.Category
sql := `
SELECT id, name, transaction_type_id
FROM category
WHERE id = $1
`
err := pgxscan.Select(ctx, dao.db, &categories, sql, id)
if err != nil {
return entity.Category{}, err
}
return *categories[0], nil
}