mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-12 02:42:48 +00:00
Started working on MGO
This commit is contained in:
68
model/driver.go
Normal file
68
model/driver.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_URL = "localhost"
|
||||
)
|
||||
|
||||
type DataStore struct {
|
||||
session *mgo.Session
|
||||
}
|
||||
|
||||
func NewSession() *DataStore {
|
||||
|
||||
d := new(DataStore)
|
||||
s, err := mgo.Dial(DB_URL)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d.session = s
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (ds *DataStore) TestStore(fname string, lname string) {
|
||||
session := ds.session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
// Collection People
|
||||
c := session.DB("test").C("people")
|
||||
|
||||
// Insert Datas
|
||||
c.Insert(&Person{FirstName: fname, LastName: lname, Timestamp: time.Now()})
|
||||
|
||||
}
|
||||
|
||||
func (ds *DataStore) SearchName(fname string) []Person {
|
||||
|
||||
session := ds.session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
// Collection People
|
||||
c := session.DB("test").C("people")
|
||||
|
||||
var results []Person
|
||||
|
||||
err := c.Find(bson.M{"firstname": fname}).All(&results)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/*func (ds *DataStore) ucol() *mgo.Collection {
|
||||
session = ds.session.Copy()
|
||||
defer session.Close()
|
||||
}
|
||||
*/
|
||||
|
||||
//func (ds *DataStore) UserExist(user string) bool { ... }
|
||||
Reference in New Issue
Block a user