-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocument.go
48 lines (38 loc) · 1.03 KB
/
document.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
package main
import (
"log"
"time"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type Document struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name"`
Path string `bson:"path"`
CreatedAt time.Time `bson:"created_at"`
Binary []byte `bson:"binary"`
ContentType string `bson:"content_type,omitempty"`
}
func (d Document) Collection(s *mgo.Session) *mgo.Collection {
return s.DB(Configuration.DBName).C(Configuration.Collection)
}
func (d *Document) Save(s *mgo.Session) error {
coll := d.Collection(s)
if !bson.IsObjectIdHex(d.Id.Hex()) {
d.Id = bson.NewObjectId()
d.CreatedAt = time.Now()
}
_, err := coll.Upsert(bson.M{"_id": d.Id}, d)
if err != nil {
log.Panicln(err)
return err
}
return nil
}
func (d Document) Find(s *mgo.Session, name string, path string) (*Document, error) {
result := new(Document)
coll := d.Collection(s)
query := coll.Find(bson.M{"name": name, "path": path})
err := query.One(result)
return result, err
}