This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
income.go
131 lines (110 loc) · 3.14 KB
/
income.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
package pg
import (
"context"
"time"
"github.com/go-pg/pg/v10"
"github.com/pkg/errors"
common "github.com/ShoshinNikita/budget-manager/internal/db"
"github.com/ShoshinNikita/budget-manager/internal/pkg/money"
)
// Income represents income entity in PostgreSQL db
type Income struct {
tableName struct{} `pg:"incomes"`
ID uint `pg:"id,pk"`
// MonthID is a foreign key to 'months' table
MonthID uint `pg:"month_id"`
Title string `pg:"title"`
Notes string `pg:"notes"`
Income money.Money `pg:"income"`
}
// ToCommon converts Income to common Income structure from
// "github.com/ShoshinNikita/budget-manager/internal/db" package
func (in Income) ToCommon(year int, month time.Month) common.Income {
return common.Income{
ID: in.ID,
Year: year,
Month: month,
Title: in.Title,
Notes: in.Notes,
Income: in.Income,
}
}
// AddIncome adds a new income with passed params
func (db DB) AddIncome(ctx context.Context, args common.AddIncomeArgs) (id uint, err error) {
if !db.checkMonth(ctx, args.MonthID) {
return 0, common.ErrMonthNotExist
}
err = db.db.RunInTransaction(ctx, func(tx *pg.Tx) (err error) {
income := &Income{
MonthID: args.MonthID,
//
Title: args.Title,
Notes: args.Notes,
Income: args.Income,
}
if _, err = tx.ModelContext(ctx, income).Returning("id").Insert(); err != nil {
return err
}
id = income.ID
return db.recomputeAndUpdateMonth(tx, args.MonthID)
})
if err != nil {
return 0, err
}
return id, nil
}
// EditIncome edits income with passed id, nil args are ignored
func (db DB) EditIncome(ctx context.Context, args common.EditIncomeArgs) error {
if !db.checkIncome(ctx, args.ID) {
return common.ErrIncomeNotExist
}
return db.db.RunInTransaction(ctx, func(tx *pg.Tx) error {
monthID, err := db.selectIncomeMonthID(tx, args.ID)
if err != nil {
return err
}
query := tx.ModelContext(ctx, (*Income)(nil)).Where("id = ?", args.ID)
if args.Title != nil {
query = query.Set("title = ?", *args.Title)
}
if args.Notes != nil {
query = query.Set("notes = ?", *args.Notes)
}
if args.Income != nil {
query = query.Set("income = ?", *args.Income)
}
if _, err := query.Update(); err != nil {
return err
}
if args.Income != nil {
// Recompute month only when income has been changed
return db.recomputeAndUpdateMonth(tx, monthID)
}
return nil
})
}
// RemoveIncome removes income with passed id
func (db DB) RemoveIncome(ctx context.Context, id uint) error {
if !db.checkIncome(ctx, id) {
return common.ErrIncomeNotExist
}
return db.db.RunInTransaction(ctx, func(tx *pg.Tx) error {
monthID, err := db.selectIncomeMonthID(tx, id)
if err != nil {
return err
}
_, err = tx.ModelContext(ctx, (*Income)(nil)).Where("id = ?", id).Delete()
if err != nil {
return err
}
return db.recomputeAndUpdateMonth(tx, monthID)
})
}
func (DB) selectIncomeMonthID(tx *pg.Tx, id uint) (monthID uint, err error) {
ctx := tx.Context()
err = tx.ModelContext(ctx, (*Income)(nil)).Column("month_id").Where("id = ?", id).Select(&monthID)
if err != nil {
return 0, errors.Wrap(err, "couldn't select month id of Income")
}
return monthID, nil
}