generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 78
/
category.go
82 lines (75 loc) · 2.24 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
package v1alpha1
import (
"encoding/json"
"sync"
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"gorm.io/gorm"
)
var categoryCreationLock sync.Mutex //Each model will perform a check and if the category already doesn't exist, it will create a category. This lock will make sure that there are no race conditions.
// swagger:response Category
type Category struct {
ID uuid.UUID `json:"-" yaml:"-"`
Name string `json:"name"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
}
type CategoryDB struct {
ID uuid.UUID `json:"-"`
Name string `json:"categoryName" gorm:"categoryName"`
Metadata []byte `json:"categoryMetadata" gorm:"categoryMetadata"`
}
type CategoryFilter struct {
Name string
OrderOn string
Greedy bool
Sort string //asc or desc. Default behavior is asc
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
}
const DefaultCategory = "Miscellaneous"
// Create the filter from map[string]interface{}
func (cf *CategoryFilter) Create(m map[string]interface{}) {
if m == nil {
return
}
cf.Name = m["name"].(string)
}
func CreateCategory(db *database.Handler, cat Category) (uuid.UUID, error) {
if cat.Name == "" {
cat.Name = DefaultCategory
}
byt, err := json.Marshal(cat)
if err != nil {
return uuid.UUID{}, err
}
catID := uuid.NewSHA1(uuid.UUID{}, byt)
var category CategoryDB
categoryCreationLock.Lock()
defer categoryCreationLock.Unlock()
err = db.First(&category, "id = ?", catID).Error
if err != nil && err != gorm.ErrRecordNotFound {
return uuid.UUID{}, err
}
if err == gorm.ErrRecordNotFound { //The category is already not present and needs to be inserted
cat.ID = catID
catdb := cat.GetCategoryDB(db)
err = db.Create(&catdb).Error
if err != nil {
return uuid.UUID{}, err
}
return catdb.ID, nil
}
return category.ID, nil
}
func (cdb *CategoryDB) GetCategory(db *database.Handler) (cat Category) {
cat.ID = cdb.ID
cat.Name = cdb.Name
_ = json.Unmarshal(cdb.Metadata, &cat.Metadata)
return
}
func (c *Category) GetCategoryDB(db *database.Handler) (catdb CategoryDB) {
catdb.ID = c.ID
catdb.Name = c.Name
catdb.Metadata, _ = json.Marshal(c.Metadata)
return
}