Пример #1
2
func GetRestaurantById(id string) (*Restaurant, error) {
	s := db.NewMongoSession()
	defer s.Close()

	c := s.DB(db.DbName()).C(collection)
	var r Restaurant
	err := c.FindId(bson.ObjectIdHex(id)).One(&r)
	return &r, err
}
Пример #2
0
func GetRestaurantByKey(key string) (*Restaurant, error) {
	s := db.NewMongoSession()
	defer s.Close()

	c := s.DB(db.DbName()).C(collection)
	var r Restaurant
	err := c.Find(bson.M{"key": key}).One(&r)
	return &r, err
}
Пример #3
0
func GetUserByOAuthId(provider, id string) (*User, error) {
	s := db.NewMongoSession()
	defer s.Close()

	u := &User{}
	c := s.DB(db.DbName()).C(collection)
	err := c.Find(bson.M{"oap": provider, "uid": id}).One(u)

	return u, err
}
Пример #4
0
func (user *User) Save() error {
	s := db.NewMongoSession()
	defer s.Close()

	var ins bool
	if user.Id == "" {
		user.Id = bson.NewObjectId()
		ins = true
	}
	c := s.DB(db.DbName()).C(collection)
	c.EnsureIndex(mgo.Index{Key: []string{"oap", "uid"}, Unique: true})
	if ins {
		return c.Insert(&user)
	} else {
		return c.UpdateId(user.Id, &user)
	}
}
Пример #5
0
func (this *Restaurant) Save() error {
	s := db.NewMongoSession()
	defer s.Close()

	var ins bool
	now := time.Now()
	if this.Id == "" {
		this.Id = bson.NewObjectId()
		this.Key = bson.NewObjectId().Hex()
		this.CreateTime = now
		ins = true
	}
	this.UpdateTime = now
	c := s.DB(db.DbName()).C(collection)
	c.EnsureIndex(mgo.Index{Key: []string{"key"}, Unique: true})

	if ins {
		return c.Insert(&this)
	} else {
		return c.UpdateId(this.Id, &this)
	}
}
Пример #6
0
func getCollection(s *mgo.Session) *mgo.Collection {
	return s.DB(db.DbName()).C(collName)
}