This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
89 lines (79 loc) · 1.77 KB
/
namespace.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
package database
import (
"context"
"log"
"github.com/thoas/go-funk"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type Namespace struct {
collection *mongo.Collection
}
func (self *Client) Namespace() *Namespace {
name := Namespace{}
if self.database == nil {
return nil
}
name.collection = self.database.Collection("namespace")
return &name
}
func (self Namespace) ListNamespace(uuid string) ([]string, error) {
result := self.collection.FindOne(context.TODO(), bson.D{
{"uuid", uuid},
})
var elem bson.M
err := result.Decode(&elem)
if err != nil {
return []string{}, err
} else {
data := funk.Map(elem["namespace"], func(data interface{}) string {
return data.(string)
}).([]string)
return data, nil
}
}
func (self Namespace) CreateNamespace(uuid, namespace string) (string, bool) {
data := self.collection.FindOne(context.TODO(), bson.D{
{"uuid", uuid},
})
t := bson.M{}
err := data.Decode(&t)
// 에러가 있다 == 내용물이 없다.
if err != nil {
result, err := self.collection.InsertOne(context.TODO(), bson.D{
{"uuid", uuid},
{"namespace", bson.A{
namespace,
}},
})
log.Println(result, err)
} else {
if funk.Contains(t["namespace"], namespace) {
return namespace, true
}
result, err := self.collection.UpdateOne(context.TODO(), bson.D{
{"_id", t["_id"]},
},
bson.M{
"$push": bson.M{
"namespace": namespace,
},
},
)
log.Println(result, err)
}
return namespace, false
}
func (self Namespace) DeleteNamespace(uuid, namespace string) (string, bool) {
result, err := self.collection.UpdateOne(context.TODO(), bson.D{
{"uuid", uuid},
},
bson.M{
"$pull": bson.M{
"namespace": namespace,
},
},
)
log.Println(result, err)
return namespace, true
}