-
Notifications
You must be signed in to change notification settings - Fork 3
/
spend.go
167 lines (144 loc) · 4.04 KB
/
spend.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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"
)
// Spend represents spend entity in PostgreSQL db
type Spend struct {
tableName struct{} `pg:"spends"`
// DayID is a foreign key to 'days' table
DayID uint `pg:"day_id"`
ID uint `pg:"id,pk"`
Title string `pg:"title"`
TypeID uint `pg:"type_id"`
Type *SpendType `pg:"rel:has-one,fk:type_id"`
Notes string `pg:"notes"`
Cost money.Money `pg:"cost,use_zero"`
}
// ToCommon converts Spend to common Spend structure from
// "github.com/ShoshinNikita/budget-manager/internal/db" package
func (s Spend) ToCommon(year int, month time.Month, day int) common.Spend {
return common.Spend{
ID: s.ID,
Year: year,
Month: month,
Day: day,
Title: s.Title,
Type: s.Type.ToCommon(),
Notes: s.Notes,
Cost: s.Cost,
}
}
// AddSpend adds a new Spend
func (db DB) AddSpend(ctx context.Context, args common.AddSpendArgs) (id uint, err error) {
if !db.checkDay(ctx, args.DayID) {
return 0, common.ErrDayNotExist
}
err = db.db.RunInTransaction(ctx, func(tx *pg.Tx) (err error) {
spend := &Spend{
DayID: args.DayID,
Title: args.Title,
Notes: args.Notes,
TypeID: args.TypeID,
Cost: args.Cost,
}
if _, err := tx.ModelContext(ctx, spend).Returning("id").Insert(); err != nil {
return err
}
id = spend.ID
monthID, err := db.selectMonthIDByDayID(ctx, tx, args.DayID)
if err != nil {
return err
}
return db.recomputeAndUpdateMonth(tx, monthID)
})
if err != nil {
return 0, err
}
return id, nil
}
// EditSpend edits existeng Spend
func (db DB) EditSpend(ctx context.Context, args common.EditSpendArgs) error {
if !db.checkSpend(ctx, args.ID) {
return common.ErrSpendNotExist
}
return db.db.RunInTransaction(ctx, func(tx *pg.Tx) error {
dayID, err := db.selectSpendDayID(ctx, tx, args.ID)
if err != nil {
return err
}
query := tx.ModelContext(ctx, (*Spend)(nil)).Where("id = ?", args.ID)
if args.Title != nil {
query = query.Set("title = ?", *args.Title)
}
if args.TypeID != nil {
if *args.TypeID == 0 {
query = query.Set("type_id = NULL")
} else {
query = query.Set("type_id = ?", *args.TypeID)
}
}
if args.Notes != nil {
query = query.Set("notes = ?", *args.Notes)
}
if args.Cost != nil {
query = query.Set("cost = ?", *args.Cost)
}
if _, err := query.Update(); err != nil {
return err
}
if args.Cost != nil {
// Recompute month only when cost has been changed
monthID, err := db.selectMonthIDByDayID(ctx, tx, dayID)
if err != nil {
return err
}
return db.recomputeAndUpdateMonth(tx, monthID)
}
return nil
})
}
// RemoveSpend removes Spend with passed id
func (db DB) RemoveSpend(ctx context.Context, id uint) error {
if !db.checkSpend(ctx, id) {
return common.ErrSpendNotExist
}
return db.db.RunInTransaction(ctx, func(tx *pg.Tx) error {
dayID, err := db.selectSpendDayID(ctx, tx, id)
if err != nil {
return err
}
_, err = tx.ModelContext(ctx, (*Spend)(nil)).Where("id = ?", id).Delete()
if err != nil {
return err
}
monthID, err := db.selectMonthIDByDayID(ctx, tx, dayID)
if err != nil {
return err
}
return db.recomputeAndUpdateMonth(tx, monthID)
})
}
func (DB) selectSpendDayID(ctx context.Context, tx *pg.Tx, id uint) (dayID uint, err error) {
query := tx.ModelContext(ctx, (*Spend)(nil)).Column("day_id").Where("id = ?", id)
err = query.Select(&dayID)
if err != nil {
return 0, errors.Wrap(err, "couldn't select day id of Spend")
}
return dayID, nil
}
func (DB) selectMonthIDByDayID(ctx context.Context, tx *pg.Tx, dayID uint) (monthID uint, err error) {
query := tx.ModelContext(ctx, (*Day)(nil)).Column("month_id").Where("id = ?", dayID)
err = query.Select(&monthID)
if err != nil {
if errors.Is(err, pg.ErrNoRows) {
return 0, common.ErrDayNotExist
}
return 0, errors.Wrap(err, "couldn't get Month which contains Day with passed id")
}
return monthID, nil
}