-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
tag.go
62 lines (52 loc) · 1.49 KB
/
tag.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
package repo
import (
"context"
"github.com/1Panel-dev/1Panel/backend/app/model"
)
type TagRepo struct {
}
type ITagRepo interface {
BatchCreate(ctx context.Context, tags []*model.Tag) error
DeleteAll(ctx context.Context) error
All() ([]model.Tag, error)
GetByIds(ids []uint) ([]model.Tag, error)
GetByKeys(keys []string) ([]model.Tag, error)
GetByAppId(appId uint) ([]model.Tag, error)
}
func NewITagRepo() ITagRepo {
return &TagRepo{}
}
func (t TagRepo) BatchCreate(ctx context.Context, tags []*model.Tag) error {
return getTx(ctx).Create(&tags).Error
}
func (t TagRepo) DeleteAll(ctx context.Context) error {
return getTx(ctx).Where("1 = 1 ").Delete(&model.Tag{}).Error
}
func (t TagRepo) All() ([]model.Tag, error) {
var tags []model.Tag
if err := getDb().Where("1 = 1 ").Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
}
func (t TagRepo) GetByIds(ids []uint) ([]model.Tag, error) {
var tags []model.Tag
if err := getDb().Where("id in (?)", ids).Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
}
func (t TagRepo) GetByKeys(keys []string) ([]model.Tag, error) {
var tags []model.Tag
if err := getDb().Where("key in (?)", keys).Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
}
func (t TagRepo) GetByAppId(appId uint) ([]model.Tag, error) {
var tags []model.Tag
if err := getDb().Where("id in (select tag_id from app_tags where app_id = ?)", appId).Find(&tags).Error; err != nil {
return nil, err
}
return tags, nil
}