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 (session *Session) UpdateUserId(userId string) error { s := db.NewMongoSession() defer s.Close() coll := getCollection(s) return coll.UpdateId(session.Id, session) }
func (session *Session) Save() error { s := db.NewMongoSession() defer s.Close() session.Id = bson.NewObjectId() coll := getCollection(s) return coll.Insert(session) }
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 GetByItemId(id string, restaurantKey string) (*Item, error) { s := db.NewMongoSession() defer s.Close() c := s.DB(restaurantKey).C(coll) var i Item err := c.FindId(bson.ObjectIdHex(id)).One(&i) return &i, 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 GetMenuById(id string, restaurantKey string) (*Menu, error) { s := db.NewMongoSession() defer s.Close() var m Menu c := s.DB(restaurantKey).C(coll) err := c.FindId(bson.ObjectIdHex(id)).One(&m) return &m, err }
func GetSessionById(sid string) (*Session, error) { s := db.NewMongoSession() defer s.Close() id := bson.ObjectIdHex(sid) coll := getCollection(s) session := &Session{} err := coll.Find(bson.M{"_id": id}).One(session) return session, 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 *Item) Save(restaurantKey string) error { s := db.NewMongoSession() defer s.Close() var ins bool now := time.Now() if this.Id == "" { this.Id = bson.NewObjectId() this.CreateTime = now ins = true } this.UpdateTime = now c := s.DB(restaurantKey).C(coll) if ins { return c.Insert(&this) } else { return c.UpdateId(this.Id, &this) } }
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) } }