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 }
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 }
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 }
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) } }
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) } }
func getCollection(s *mgo.Session) *mgo.Collection { return s.DB(db.DbName()).C(collName) }