-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts_dao.go
59 lines (49 loc) · 1.17 KB
/
products_dao.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
package main
import (
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type ProductsDAO struct {
Server string
Database string
}
var db *mgo.Database
const (
COLLECTION = "products"
)
// Establish a connection to database
func (m *ProductsDAO) Connect() error {
session, err := mgo.Dial(m.Server)
if err != nil {
return err
}
db = session.DB(m.Database)
return nil
}
// Find list of movies
func (m *ProductsDAO) FindAll() ([]Product, error) {
var products []Product
err := db.C(COLLECTION).Find(bson.M{}).All(&products)
return products, err
}
// Find a movie by its id
func (m *ProductsDAO) FindById(id string) (Product, error) {
var product Product
err := db.C(COLLECTION).FindId(bson.ObjectIdHex(id)).One(&product)
return product, err
}
// Insert a movie into database
func (m *ProductsDAO) Insert(product Product) error {
err := db.C(COLLECTION).Insert(&product)
return err
}
// Delete an existing movie
func (m *ProductsDAO) Delete(product Product) error {
err := db.C(COLLECTION).Remove(&product)
return err
}
// Update an existing movie
func (m *ProductsDAO) Update(product Product) error {
err := db.C(COLLECTION).UpdateId(product.ID, &product)
return err
}