-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata_imp.go
134 lines (115 loc) · 3.17 KB
/
metadata_imp.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
package caskin
import (
"github.com/ahmetb/go-linq/v3"
"gorm.io/gorm"
)
type builtinMetadataDB[U User, R Role, O Object, D Domain] struct {
DB *gorm.DB
}
func (b *builtinMetadataDB[U, R, O, D]) Create(item any) error {
return b.DB.Create(item).Error
}
func (b *builtinMetadataDB[U, R, O, D]) Recover(item any) error {
if err := b.TakeUnscoped(item); err != nil {
return err
}
return b.DB.Unscoped().Model(item).Update("delete_at", nil).Error
}
func (b *builtinMetadataDB[U, R, O, D]) Update(item any) error {
return b.DB.Updates(item).Error
}
func (b *builtinMetadataDB[U, R, O, D]) UpsertType(item any) UpsertType {
if err := b.Take(item); err == nil {
return UpsertTypeUpdate
}
if err := b.TakeUnscoped(item); err == nil {
return UpsertTypeRecover
}
return UpsertTypeCreate
}
func (b *builtinMetadataDB[U, R, O, D]) Take(item any) error {
return b.DB.Where(item).Take(item).Error
}
func (b *builtinMetadataDB[U, R, O, D]) TakeUnscoped(item any) error {
return b.DB.Unscoped().Where(item).Take(item).Error
}
func (b *builtinMetadataDB[U, R, O, D]) Find(items any, cond ...any) error {
return b.DB.Find(items, cond...).Error
}
func (b *builtinMetadataDB[U, R, O, D]) DeleteByID(item any, id uint64) error {
return b.DB.Delete(item, id).Error
}
func (b *builtinMetadataDB[U, R, O, D]) GetUserByID(id []uint64) ([]User, error) {
user, err := GetByID[U](b, id)
if err != nil {
return nil, err
}
var out []User
linq.From(user).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetRoleInDomain(domain Domain) ([]Role, error) {
var role []R
if err := b.DB.Find(&role, "domain_id = ?", domain.GetID()).Error; err != nil {
return nil, err
}
var out []Role
linq.From(role).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetRoleByID(id []uint64) ([]Role, error) {
role, err := GetByID[R](b, id)
if err != nil {
return nil, err
}
var out []Role
linq.From(role).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetObjectInDomain(domain Domain, objectType ...ObjectType) ([]Object, error) {
d := b.DB.Where("domain_id = ?", domain.GetID())
if len(objectType) > 0 {
d = d.Where("type IN ?", objectType)
}
var object []O
if err := d.Find(&object).Error; err != nil {
return nil, err
}
var out []Object
linq.From(object).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetObjectByID(id []uint64) ([]Object, error) {
object, err := GetByID[O](b, id)
if err != nil {
return nil, err
}
var out []Object
linq.From(object).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetDomainByID(id []uint64) ([]Domain, error) {
domain, err := GetByID[D](b, id)
if err != nil {
return nil, err
}
var out []Domain
linq.From(domain).ToSlice(&out)
return out, nil
}
func (b *builtinMetadataDB[U, R, O, D]) GetAllDomain() ([]Domain, error) {
var domain []D
if err := b.DB.Find(&domain).Error; err != nil {
return nil, err
}
var ret []Domain
linq.From(domain).ToSlice(&ret)
return ret, nil
}
func GetByID[T any](db MetaDB, id []uint64) ([]T, error) {
var out []T
if err := db.Find(&out, "id IN ?", id); err != nil {
return nil, err
}
return out, nil
}