/
Group.go
91 lines (77 loc) · 2.15 KB
/
Group.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
package models
import (
"github.com/jinzhu/gorm"
)
//Group a group in DB
type Group struct {
gorm.Model
Name string `gorm:"not null"`
NamespaceID uint `sql:"index" gorm:"not null"`
Namespace *Namespace `gorm:"association_autoupdate:false;association_autocreate:false"`
UserID uint `sql:"index" gorm:"not null"`
User *User `gorm:"association_autoupdate:false;association_autocreate:false"`
}
//Insert inserts group into DB
func (group *Group) Insert(db *gorm.DB, user *User) error {
//Use default namespace if nil
group.Namespace = group.GetNamespace()
group.User = user
return db.Create(group).Error
}
//GetNamespace return namespace of group
func (group Group) GetNamespace() *Namespace {
if group.Namespace == nil {
return &DefaultNamespace
}
return group.Namespace
}
//GroupsFromStringArr return tag array from string array
func GroupsFromStringArr(arr []string, namespace Namespace, user *User) []Group {
var tags []Group
for _, tag := range arr {
tags = append(tags, Group{
Name: tag,
User: user,
UserID: user.ID,
Namespace: &namespace,
})
}
return tags
}
//GroupArrToStringArr return string arr from group
func GroupArrToStringArr(groups []Group) []string {
var str []string
for _, group := range groups {
str = append(str, group.Name)
}
return str
}
//FindGroups find group in db
func FindGroups(db *gorm.DB, sGroups []string, namespace *Namespace) []Group {
var groups []Group
db.Model(&Group{}).Where("name in (?) AND namespace_id = ?", sGroups, namespace.ID).Find(&groups)
return groups
}
//GetGroup returns or creates a tag
func GetGroup(db *gorm.DB, name string, namespace *Namespace, user *User) *Group {
var group Group
db.Where(&Group{
Name: name,
NamespaceID: namespace.ID,
UserID: user.ID,
}).FirstOrCreate(&group)
return &group
}
//FindGroup finds a tag
func FindGroup(db *gorm.DB, name string, namespace *Namespace, user *User) (*Group, error) {
var group Group
err := db.Where(&Group{
Name: name,
NamespaceID: namespace.ID,
UserID: user.ID,
}).Find(&group).Error
if err != nil {
return nil, err
}
return &group, nil
}