func GetNew(db *gorp.DbMap, id int64) (New, error) { obj, err := db.Get(New{}, id) if err != nil { return New{}, err } if obj == nil { return New{}, errors.New("New not found") } n := obj.(*New) return *n, nil }
func GetProduct(db *gorp.DbMap, productid int64) (StoreProduct, error) { var sp = StoreProduct{} obj, err := db.Get(StoreProduct{}, productid) if err != nil { return sp, err } if obj == nil { return sp, errors.New("Product not found") } sp = *obj.(*StoreProduct) return sp, nil }
func Get(db *gorp.DbMap, id int64) (User, error) { var user User obj, err := db.Get(User{}, id) if err != nil { return user, err } if obj == nil { return user, errors.New("User not found") } user = *obj.(*User) return user, nil }
func GetDelivery(db *gorp.DbMap, deliveryid int64) (OrderDelivery, error) { var dlv OrderDelivery obj, err := db.Get(OrderDelivery{}, deliveryid) if err != nil { return dlv, err } if obj == nil { return dlv, errors.New("Delivery not found") } dlv = *obj.(*OrderDelivery) return dlv, nil }
func ProductsShow(db *gorp.DbMap, params martini.Params, render render.Render) { id, err := strconv.Atoi(params["id"]) if err != nil { panic(err) } product, err := db.Get(models.Product{}, id) if err == nil { render.JSON(200, product) } else { render.JSON(404, map[string]string{"error": err.Error()}) } }
func GetUsers(db *gorp.DbMap, ids []int64) ([]User, error) { var users []User for _, id := range ids { obj, err := db.Get(User{}, id) if err != nil { return users, err } if obj == nil { return users, errors.New("User not found") } user := *obj.(*User) users = append(users, user) } return users, nil }
func (gs *GameServiceImpl) GetGame(db *gorp.DbMap, gameId string, playerId int) (*Game, *Player, error) { obj, err := db.Get(Player{}, playerId) if err != nil { return nil, nil, err } player := obj.(*Player) // get the game from the db to load the state, other info g, err := db.Get(Game{}, gameId) if err != nil { return nil, nil, err } game := g.(*Game) return game, player, nil }
func GetTask(r render.Render, params martini.Params, db *gorp.DbMap) { id, err := strconv.Atoi(params["id"]) if err != nil { r.JSON(400, map[string]string{"message": "id must be an integer"}) return } task, err := db.Get(Task{}, id) if err != nil { r.JSON(500, map[string]string{"message": "error while retrieving task"}) return } if task == nil { r.JSON(404, map[string]string{"message": "task not found"}) return } r.JSON(200, task) }
func (gs *GameServiceImpl) ConnectToGame(db *gorp.DbMap, gameId string, playerObj interface{}) (*Game, *Player, error) { obj, err := db.Get(Game{}, gameId) if err != nil { return nil, nil, err } if obj == nil { return nil, nil, errors.New("Player not saved to session") } game := obj.(*Game) var player *Player if playerObj == nil { // no, it's a new player player = &Player{ Game: game.Id, } // save to db so we can find them if they disconnect err = db.Insert(player) if err != nil { return nil, nil, err } } else { // player is rejoining playerObj, err := db.Get(Player{}, playerObj) if err != nil { return nil, nil, err } player = playerObj.(*Player) // TODO: this would screw with any games they are currently already in? if player.Game != game.Id { player.Game = game.Id count, err := db.Update(player) if count == 0 { return nil, nil, errors.New("Player update effected 0 rows") } if err != nil { return nil, nil, err } log.Printf("Joining player id is: %#v", player.Id) } else { log.Printf("Returning player id is: %#v", player.Id) } } return game, player, nil }
func GetJail(db *gorp.DbMap, field map[string]interface{}) *Jail { uuid := LookupUUID(db, field) if len(uuid) == 0 { return nil } obj, err := db.Get(Jail{}, uuid) if err != nil { panic(err) return nil } if obj == nil { /* Jail not found */ return nil } return obj.(*Jail) }
func main() { var ( dbMap *gorp.DbMap user *User iUser interface{} ) dbMap = SetupDb() //START CODE OMIT dbMap.Insert(&User{Id: 1, FirstName: "John", LastName: "Doe"}) PrintTable(dbMap) iUser, _ = dbMap.Get(User{}, 1) user = iUser.(*User) user.FirstName = "James" dbMap.Update(user) PrintTable(dbMap) dbMap.Delete(user) PrintTable(dbMap) //END CODE OMIT }