Exemple #1
0
func Transact(dbm *gorp.DbMap, f func(gorp.SqlExecutor) error) error {
	txn, err := dbm.Begin()
	if err != nil {
		return err
	}
	defer func() {
		if txn == nil {
			return
		}
		if err := txn.Rollback(); err != nil && err != sql.ErrTxDone {
			panic(err)
		}
	}()

	err = f(txn)
	if err != nil {
		return err
	}

	err = txn.Commit()
	if err != nil && err != sql.ErrTxDone {
		return err
	}
	txn = nil
	return nil
}
Exemple #2
0
func (n New) AddImages(db *gorp.DbMap, iids []int64) error {
	n.GetImages(db)
	for _, v := range n.Images {
		for _, j := range iids {
			if v == j {
				return errors.New("Image already taken")
			}
		}
	}
	tx, err := db.Begin()
	if err != nil {
		return err
	}
	for _, v := range iids {
		NewImage := NewImages{
			NewId:   n.Id,
			ImageId: v,
		}
		err := tx.Insert(&NewImage)
		if err != nil {
			tx.Rollback()
			return err
		}
	}
	tx.Commit()
	return nil
}
Exemple #3
0
func CreateOrder(db *gorp.DbMap, customer, storeid, deliveryid int64, phone,
	address string, products []OrderProduct, price float64) (Order, error) {
	t := time.Now().UnixNano()
	o := Order{
		CustomerId: customer,
		StoreId:    storeid,
		DeliveryId: deliveryid,
		Phone:      phone,
		Price:      price,
		Address:    address,
		Created:    t,
		Updated:    t,
	}
	tx, err := db.Begin()
	if err != nil {
		return o, err
	}
	err = tx.Insert(&o)
	if err != nil {
		tx.Rollback()
		return o, err
	}
	for _, v := range products {
		v.OrderId = o.Id
		err := tx.Insert(&v)
		if err != nil {
			tx.Rollback()
			return o, err
		}
	}
	tx.Commit()
	o.Products = products
	return o, err
}