/
models.go
65 lines (52 loc) · 954 Bytes
/
models.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
package main
import (
"code.google.com/p/goweb/goweb"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
//"reflect"
)
type Model interface {
C() *mgo.Collection
Fill(*goweb.Context)
}
type Person struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string
Phone string
}
func (p Person) C() *mgo.Collection {
return Coll("Person")
}
func (p *Person) Fill(cx *goweb.Context) {
cx.Fill(&p)
}
func newPerson() *Person {
return &Person{}
}
type Product struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string
Price float32
}
func (p Product) C() *mgo.Collection {
return Coll("Product")
}
func (p *Product) Fill(cx *goweb.Context) {
cx.Fill(&p)
}
func (p Product) TestFunc(s string) string {
return s
}
func newProduct() *Product {
return &Product{}
}
func newModel(c string) Model {
switch c {
case "person":
return newPerson()
}
return nil
}
func Coll(c string) *mgo.Collection {
return mSession.DB("").C(c)
}