-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoints.go
82 lines (72 loc) · 1.87 KB
/
points.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
package main
import (
"log"
"gopkg.in/mgo.v2/bson"
)
// PointItem represents a document in the collection
func updateScore(item string, operation string, guild string) []bson.ObjectId {
collection := session.DB(mongoDBDatabase).C("PointItems")
theID := returnItemID(item, guild)
println(theID)
if theID != nil {
if operation == "++" {
log.Output(0, "Existing Record, Adding one point")
err := collection.Update(bson.M{"_id": bson.ObjectId(theID[0])}, bson.M{"$inc": bson.M{"points": 1}})
if err != nil {
log.Fatal("Error updating record: ", err)
return nil
}
return []bson.ObjectId{theID[0]}
}
if operation == "--" {
log.Output(0, "Existing Record, Removing one point")
err := collection.Update(bson.M{"_id": theID[0]}, bson.M{"$inc": bson.M{"points": -1}})
if err != nil {
log.Fatal("Error updating record: ", err)
return nil
}
return []bson.ObjectId{theID[0]}
}
} else {
newID := bson.NewObjectId()
log.Output(0, "New Record, Creating and Adding one point")
if operation == "++" {
err := collection.Insert(&PointItem{
Id: newID,
Item: item,
Guild: guild,
Points: 1,
})
if err != nil {
log.Fatal("Error updating record: ", err)
return nil
}
return []bson.ObjectId{newID}
}
if operation == "--" {
log.Output(0, "New Record, Creating and removing one point")
err := collection.Insert(&PointItem{
Id: newID,
Item: item,
Guild: guild,
Points: -1,
})
if err != nil {
log.Fatal("Error updating record: ", err)
return nil
}
return []bson.ObjectId{newID}
}
}
return nil
}
func getPointsByID(id bson.ObjectId) int {
collection := session.DB(mongoDBDatabase).C("PointItems")
item := PointItem{}
err := collection.FindId(bson.ObjectId(id)).One(&item)
if err != nil {
log.Output(0, "Error locating record")
return 0
}
return item.Points
}