/
category.go
153 lines (137 loc) · 3.32 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
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
package types
import (
"fmt"
"strings"
"time"
)
// A Category is a "folder" for grouping translation-keys together
// swagger:model Category
type Category struct {
Entity
Title string `json:"title"`
Description string `json:"description,omitempty"`
Key string `json:"key"`
ProjectID string `json:"project_id,omitempty"`
TranslationIDs []string `json:"translation_ids,omitempty"`
}
// Splits the key into multiple keys. Root-values are removed
func (c Category) Keys() []string {
list := []string{}
if c.Key == RootCategory {
return list
}
return strings.Split(c.Key, ".")
}
const (
// RootCategories are accessible without a key, but we do need a key.
// A bit dirty.
// Deprecated, will simply be an empty path
RootCategory = ""
)
func (c Category) IsRoot() bool {
return c.Key == RootCategory
}
func (e Category) Namespace() string {
return e.Kind()
}
func (e Category) Kind() string {
return string(PubTypeCategory)
}
// Used to filter and search along with Category.Filter(CategoryFilter)
type CategoryFilter struct {
OrganizationID string
Key string
ID string
ProjectID string
}
func (cat Category) Path() []string {
if cat.IsRoot() {
return []string{}
}
return strings.Split(cat.Key, ".")
}
func (cat *Category) Update(payload Category, options ...UpdateEntityOptions) error {
err := cat.Entity.Update(payload.Entity, options...)
if err != nil {
return err
}
outer:
for _, v := range payload.TranslationIDs {
for _, vv := range cat.TranslationIDs {
if v == vv {
continue outer
}
}
cat.TranslationIDs = append(cat.TranslationIDs, v)
}
if payload.Key != "" {
cat.Key = payload.Key
}
if payload.Title != "" {
cat.Title = payload.Title
}
if payload.Description != "" {
cat.Description = payload.Description
}
if cat.ProjectID != "" {
cat.ProjectID = payload.ProjectID
}
return nil
}
type UpdateEntityOptions struct {
IgnoreMissingFields bool
SkipUpdatingUpdatedAt bool
}
// Updates an existing entity-struct with changes.
// NOTE: This does NOT update the db-value itself., but is meant as a helper-func
func (existing *Entity) Update(changes Entity, options ...UpdateEntityOptions) error {
var opts UpdateEntityOptions
if len(options) > 0 {
opts = options[0]
}
if existing.ID == "" {
return fmt.Errorf("The existing-value does not have an ID.")
}
updatedBy := changes.UpdatedBy
if updatedBy == "" {
updatedBy = changes.CreatedBy
}
if updatedBy == "" {
if !opts.IgnoreMissingFields {
return fmt.Errorf("UpdatedBy is not set")
}
}
if changes.UpdatedAt == nil {
if !opts.SkipUpdatingUpdatedAt {
now := time.Now()
changes.UpdatedAt = &now
}
}
existing.UpdatedAt = changes.UpdatedAt
existing.UpdatedBy = changes.UpdatedBy
return nil
}
// Used to filter and search
func (cat Category) Filter(f CategoryFilter) bool {
if f.OrganizationID != "" && f.OrganizationID != cat.OrganizationID {
return false
}
if f.Key != "" && f.Key != cat.Key {
return false
}
if f.ID != "" && f.ID != cat.ID {
return false
}
if f.ProjectID != "" && f.ProjectID != cat.ProjectID {
return false
}
return true
}
func (cat Category) AsUniqueFilter() CategoryFilter {
return CategoryFilter{
OrganizationID: cat.OrganizationID,
Key: cat.Key,
ID: cat.ID,
ProjectID: cat.ProjectID,
}
}