-
Notifications
You must be signed in to change notification settings - Fork 3
/
category.go
75 lines (61 loc) · 2.03 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package models
import (
"strings"
"github.com/google/uuid"
"gorm.io/gorm"
)
// Category represents a category of envelopes.
type Category struct {
DefaultModel
CategoryCreate
Budget Budget `json:"-"`
Archived bool `json:"archived" example:"true" default:"false" gorm:"-"` // Is the Category archived?
}
type CategoryCreate struct {
Name string `json:"name" gorm:"uniqueIndex:category_budget_name" example:"Saving" default:""` // Name of the category
BudgetID uuid.UUID `json:"budgetId" gorm:"uniqueIndex:category_budget_name" example:"52d967d3-33f4-4b04-9ba7-772e5ab9d0ce"` // ID of the budget the category belongs to
Note string `json:"note" example:"All envelopes for long-term saving" default:""` // Notes about the category
Hidden bool `json:"hidden" example:"true" default:"false"` // Is the category hidden?
}
func (c Category) Self() string {
return "Category"
}
func (c *Category) BeforeSave(_ *gorm.DB) error {
c.Name = strings.TrimSpace(c.Name)
c.Note = strings.TrimSpace(c.Note)
return nil
}
func (c *Category) AfterFind(_ *gorm.DB) (err error) {
// Set the Archived field to the value of Hidden
c.Archived = c.Hidden
return nil
}
// BeforeUpdate archives all envelopes when the category is archived.
func (c *Category) BeforeUpdate(tx *gorm.DB) (err error) {
if tx.Statement.Changed("Hidden") && !c.Hidden {
var envelopes []Envelope
err = tx.Where(&Envelope{EnvelopeCreate: EnvelopeCreate{
CategoryID: c.ID,
}}).
Find(&envelopes).Error
if err != nil {
return
}
for _, e := range envelopes {
e.Hidden = true
err = tx.Model(&e).Updates(&e).Error
if err != nil {
return
}
}
}
return nil
}
func (c *Category) Envelopes(tx *gorm.DB) ([]Envelope, error) {
var envelopes []Envelope
err := tx.Where(&Envelope{EnvelopeCreate: EnvelopeCreate{CategoryID: c.ID}}).Find(&envelopes).Error
if err != nil {
return []Envelope{}, err
}
return envelopes, nil
}