generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 79
/
category.go
83 lines (70 loc) · 2.07 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
package v1beta1
import (
"encoding/json"
"fmt"
"sync"
"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/meshmodel/entity"
"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:"-"`
Name string `json:"name" gorm:"name"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata" gorm:"type:bytes;serializer:json"`
}
func (c Category) TableName() string {
return "category_dbs"
}
// "Uncategorized" is assigned when Category is empty in the component definitions.
const DefaultCategory = "Uncategorized"
func (cat Category) Type() entity.EntityType {
return entity.Category
}
func (cat *Category) GenerateID() (uuid.UUID, error) {
categoryIdentifier := Category{
Name: cat.Name,
}
byt, err := json.Marshal(categoryIdentifier)
if err != nil {
return uuid.UUID{}, err
}
catID := uuid.NewSHA1(uuid.UUID{}, byt)
return catID, nil
}
func (cat Category) GetID() uuid.UUID {
return cat.ID
}
func (cat *Category) GetEntityDetail() string {
return fmt.Sprintf("name: %s", cat.Name)
}
func (cat *Category) Create(db *database.Handler, _ uuid.UUID) (uuid.UUID, error) {
if cat.Name == "" {
cat.Name = DefaultCategory
}
catID, err := cat.GenerateID()
if err != nil {
return catID, err
}
var category Category
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
err = db.Create(&cat).Error
if err != nil {
return uuid.UUID{}, err
}
return cat.ID, nil
}
return category.ID, nil
}
func (m *Category) UpdateStatus(db database.Handler, status entity.EntityStatus) error {
return nil
}