forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_resource_persister.go
119 lines (94 loc) · 2.86 KB
/
pattern_resource_persister.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
package models
import (
"strings"
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/database"
)
type PatternResourcePersister struct {
DB *database.Handler
}
type PatternResourcePage struct {
Page uint64 `json:"page,omitempty"`
PageSize uint64 `json:"page_size,omitempty"`
TotalCount int `json:"total_count,omitempty"`
Resources []*PatternResource `json:"resources,omitempty"`
}
func (prp *PatternResourcePersister) SavePatternResource(pr *PatternResource) (*PatternResource, error) {
if pr.ID == nil {
id, err := uuid.NewV4()
if err != nil {
return nil, ErrGenerateUUID(err)
}
pr.ID = &id
}
return pr, prp.DB.Create(pr).Error
}
func (prp *PatternResourcePersister) DeletePatternResource(id uuid.UUID) error {
return prp.DB.Model(&PatternResource{}).Where("id = ?", id).Update("deleted", true).Error
}
func (prp *PatternResourcePersister) GetPatternResource(id uuid.UUID) (*PatternResource, error) {
var pr PatternResource
err := prp.DB.First(&pr, id).Error
return &pr, err
}
func (prp *PatternResourcePersister) GetPatternResourceByAttributes(name, namespace, typ, oamType string) (*PatternResource, error) {
var pr PatternResource
err := Paginate(0, 1)(prp.DB.Model(&PatternResource{}).Where(
"name = ? AND namespace = ? AND type = ? AND oam_type = ? AND deleted = false",
name,
namespace,
typ,
oamType,
)).Scan(&pr).Error
return &pr, err
}
func (prp *PatternResourcePersister) GetPatternResources(search, order, name, namespace, typ, oamType string, page, pageSize uint64) (*PatternResourcePage, error) {
order = sanitizeOrderInput(order, []string{"created_at", "updated_at", "name"})
if order == "" {
order = "updated_at desc"
}
count := int64(0)
resources := []*PatternResource{}
query := prp.DB.Order(order).Where("deleted = false")
if search != "" {
like := "%" + strings.ToLower(search) + "%"
query = query.Where("(lower(pattern_resources.name) like ?)", like)
}
if name != "" {
query = query.Where("name = ?", name)
}
if namespace != "" {
query = query.Where("namespace = ?", namespace)
}
if typ != "" {
query = query.Where("type = ?", typ)
}
if oamType != "" {
query = query.Where("oam_type = ?", oamType)
}
query.Model(&PatternResource{}).Count(&count)
Paginate(uint(page), uint(pageSize))(query).Find(&resources)
patternResourcePage := &PatternResourcePage{
Page: page,
PageSize: pageSize,
TotalCount: int(count),
Resources: resources,
}
return patternResourcePage, nil
}
func (prp *PatternResourcePersister) Exists(name, namespace, typ, oamType string) bool {
var result struct {
Found bool
}
prp.DB.
Raw(`
SELECT
EXISTS(SELECT 1 FROM pattern_resources WHERE name = ? AND namespace = ? AND type = ? AND oam_type = ? AND deleted = false) AS "found"`,
name,
namespace,
typ,
oamType,
).
Scan(&result)
return result.Found
}