-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.go
35 lines (29 loc) · 1.06 KB
/
record.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
package storage
import (
"github.com/gaocegege/the-big-brother-is-watching-you/common"
"github.com/satori/go.uuid"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// RecordCollectionManager is the manager for Record in mongo
type RecordCollectionManager struct {
manager *CollectionManager
}
// NewRecordCollectionManager returns a new RecordCollectionManager object
func NewRecordCollectionManager(s *mgo.Session) *RecordCollectionManager {
return &RecordCollectionManager{
manager: newCollectionManager(s, common.DefaultDBName, common.RecordCollectionName),
}
}
// NewRecordDocument generates a new Record document
func (m *RecordCollectionManager) NewRecordDocument(r *common.Record) (string, error) {
r.RecordID = uuid.NewV4().String()
_, err := m.manager.col.Upsert(bson.M{"_id": r.RecordID}, r)
return r.RecordID, err
}
// FindRecordByID finds a Record entity by ID.
func (m *RecordCollectionManager) FindRecordByID(rID string) (*common.Record, error) {
Record := &common.Record{}
err := m.manager.col.Find(bson.M{"_id": rID}).One(Record)
return Record, err
}