/
store.go
158 lines (129 loc) · 3.91 KB
/
store.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package chef
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Store interface {
/*
AddChefEnvironment Adds a chef environment to alert on
*/
AddChefEnvironment(environment, friendlyName string, chatid uint32) error
/*
GetChefEnvironments will return all the chef environments in the database
*/
GetChefEnvironments() ([]ChefEnvironment, error)
/*
GetEnvironmentForGroup will return all chef environments for a group
*/
GetEnvironmentForGroup(chat uint32) ([]ChefEnvironment, error)
/*
GetEnvironmentFromFriendlyName returns the chef environment name based on the user friendly name supplied
*/
GetEnvironmentFromFriendlyName(recipe string, chat uint32) (string, error)
/*
GetRecipesForGroup will return all the recipes configured for a group
*/
GetRecipesForGroup(chat uint32) ([]Recipe, error)
/*
AddRecipe will add a recipe to the watch list for the bot
*/
AddRecipe(recipeName, friendlyName string, chatid uint32) error
/*
GetRecipes returns all the configured chef recipes. 0 length if none exists or there is an error.
*/
GetRecipes() ([]Recipe, error)
GetRecipeFromFriendlyName(recipe string, chat uint32) (string, error)
}
func NewMongoStore(mongo *mgo.Database) Store {
return &mongoStore{mongo}
}
type mongoStore struct {
mongo *mgo.Database
}
func (s *mongoStore) GetEnvironmentForGroup(chat uint32) ([]ChefEnvironment, error) {
c := s.mongo.C("chefenvironments")
q := c.Find(bson.M{"chatid": chat})
var r []ChefEnvironment
err := q.All(&r)
return r, err
}
func (s *mongoStore) GetRecipesForGroup(chat uint32) ([]Recipe, error) {
c := s.mongo.C("recipes")
q := c.Find(bson.M{"chatid": chat})
var r []Recipe
err := q.All(&r)
return r, err
}
/*
ChefEnvironment contains the chef environments this bot is allowed to use in CHEF
*/
type ChefEnvironment struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Environment string
FriendlyName string
ChatID uint32
}
/*
Recipe are the chef recipes the bot wants to interact with
*/
type Recipe struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Recipe string
FriendlyName string
ChatID uint32
}
func (s *mongoStore) AddChefEnvironment(environment, friendlyName string, chatid uint32) error {
c := s.mongo.C("chefenvironments")
q := c.Find(bson.M{"environment": environment, "chatid": chatid})
n, err := q.Count()
if err != nil {
return err
}
if n > 0 {
return fmt.Errorf("ENvironment %v already exists for group %v", environment, chatid)
}
chefEnvironment := ChefEnvironment{Environment: environment, FriendlyName: friendlyName, ChatID: chatid}
return c.Insert(chefEnvironment)
}
func (s *mongoStore) GetChefEnvironments() ([]ChefEnvironment, error) {
c := s.mongo.C("chefenvironments")
var r []ChefEnvironment
err := c.Find(nil).All(&r)
return r, err
}
func (s *mongoStore) GetEnvironmentFromFriendlyName(recipe string, chat uint32) (string, error) {
c := s.mongo.C("chefenvironments")
var r ChefEnvironment
err := c.Find(bson.M{"friendlyname": recipe, "chatid": chat}).One(&r)
return r.Environment, err
}
func (s *mongoStore) AddRecipe(recipeName, friendlyName string, chatid uint32) error {
c := s.mongo.C("recipes")
q := c.Find(bson.M{"recipe": recipeName, "chatid": chatid})
n, err := q.Count()
if err != nil {
return err
}
if n > 0 {
return fmt.Errorf("recipe with name %v already exists in group %v", recipeName, chatid)
}
recipeItem := Recipe{Recipe: recipeName, FriendlyName: friendlyName, ChatID: chatid}
return c.Insert(recipeItem)
}
func (s *mongoStore) GetRecipes() ([]Recipe, error) {
c := s.mongo.C("recipes")
q := c.Find(nil)
var recipes []Recipe
err := q.All(&recipes)
if err != nil {
return nil, err
}
return recipes, nil
}
func (s *mongoStore) GetRecipeFromFriendlyName(recipe string, chat uint32) (string, error) {
c := s.mongo.C("recipes")
var r Recipe
err := c.Find(bson.M{"friendlyname": recipe, "chatit": chat}).One(&r)
return r.Recipe, err
}