-
Notifications
You must be signed in to change notification settings - Fork 3
/
expressions.go
105 lines (78 loc) · 2.19 KB
/
expressions.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
package database
import (
"fmt"
"strings"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
func (mg *mongo) UpdateExpressionUsage(id bson.ObjectId) ExpressionItem {
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
exp := mg.GetExpression(id)
if len(exp.ExpressionCmp) > 0 {
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"usage": 1}},
ReturnNew: false,
}
c.Find(bson.M{"_id": id}).Apply(change, &exp)
exp.Usage = exp.Usage + 1
return exp
}
return ExpressionItem{}
}
func (mg *mongo) AddExpression(item ExpressionItem) ExpressionItem {
expID := strings.Replace(item.Expression, " ", "", -1)
item.ExpressionCmp = expID
item.Usage = 1 // default to 1 because your the one to use it derp...
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
index := mgo.Index{
Key: []string{"cid"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
c.EnsureIndex(index)
item.MGID = bson.NewObjectId()
c.Insert(item)
return item
}
func (mg *mongo) GetExpression(id bson.ObjectId) ExpressionItem {
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
var exp ExpressionItem
c.Find(bson.M{"_id": id}).One(&exp)
if len(exp.Expression) > 0 {
return exp
}
return ExpressionItem{}
}
func (mg *mongo) GetExpressionByName(name string) ExpressionItem {
name = strings.TrimSpace(name)
name = strings.Replace(name, " ", "", -1)
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
var exp ExpressionItem
c.Find(bson.M{"cid": name}).One(&exp)
if len(exp.Expression) > 0 {
return exp
}
return ExpressionItem{}
}
func (mg *mongo) OrderExpression(mode string, page, limit int) []ExpressionItem {
items := make([]ExpressionItem, limit)
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
c.Find(bson.M{}).Sort(fmt.Sprintf("%susage", mode)).Limit(limit).Skip(page * limit).All(&items)
return items
}
func (mg *mongo) AddCateoryToExpression(exp ExpressionItem, cat bson.ObjectId) ExpressionItem {
session, c := mg.collection(EXPRESSION_COL)
defer session.Close()
c.Update(
bson.M{"_id": exp.MGID},
bson.M{"$push": bson.M{"cats": cat}},
)
return exp
}