示例#1
0
// Read retrieves a Customer from database.
func (c *Customer) Read(id uuid.UUID) error {
	pdb, err := utils.OpenDbConnection()
	utils.PanicOnError(err)
	defer pdb.Close()

	db := goqu.New(settings.DatabaseDriver, pdb)
	sql, args, err := db.From("customers").
		Where(goqu.Ex{"id": id}).
		Prepared(true).
		ToSql()
	if err != nil {
		return err
	}

	rows, err := db.Query(sql, args...)
	if err != nil {
		return err
	}

	if rows.Next() {
		err = rows.Scan(&c.ID, &c.Name)
		if err != nil {
			return err
		}
	} else {
		return fmt.Errorf("No Customer found with id %s.", id)
	}

	return nil
}
示例#2
0
// Exists checks if an OrderItem instance is already in database.
// FIXME: Use external db connector.
func (o *OrderItem) Exists() bool {
	pdb, err := utils.OpenDbConnection()
	utils.PanicOnError(err)
	defer pdb.Close()

	db := goqu.New(settings.DatabaseDriver, pdb)
	sql, args, err := db.From("order_items").
		Where(goqu.Ex{
			"order_id":   o.ItemOrder.ID,
			"product_id": o.ItemProduct.ID}).
		Prepared(true).
		ToSql()
	utils.PanicOnError(err)

	rows, err := db.Query(sql, args...)
	utils.PanicOnError(err)

	return rows.Next()
}
示例#3
0
// Read retrieves an Order from database.
func (o *Order) Read(id uuid.UUID) error {
	pdb, err := utils.OpenDbConnection()
	if err != nil {
		return err
	}
	defer pdb.Close()

	db := goqu.New(settings.DatabaseDriver, pdb)
	sql, args, err := db.From("orders", "customers").
		Select("orders.id",
			"customers.id",
			"customers.name").
		Where(goqu.Ex{"orders.id": id}).
		Prepared(true).
		ToSql()
	if err != nil {
		return err
	}

	rows, err := db.Query(sql, args...)
	if err != nil {
		return err
	}

	if rows.Next() {
		c := new(Customer)
		err = rows.Scan(&o.ID, &c.ID, &c.Name)
		utils.PanicOnError(err)

		o.OrderCustomer = c
	} else {
		return fmt.Errorf("No order found with id %s.", id)
	}

	sql, args, err = db.From("order_items").
		Where(goqu.Ex{"order_id": id}).
		Prepared(true).
		ToSql()
	if err != nil {
		return err
	}

	rows, err = db.Query(sql, args...)
	if err != nil {
		return err
	}
	defer rows.Close()

	o.Items = make([]*OrderItem, 0)
	var prodID uuid.UUID
	for rows.Next() {
		item := new(OrderItem)
		// FIXME: Remove ItemOrder from query.
		err = rows.Scan(&uuid.UUID{}, &prodID, &item.Quantity, &item.UnitPrice)
		if err != nil {
			return err
		}

		item.ItemOrder = o
		prod := new(Product)
		prod.Read(prodID)
		item.ItemProduct = prod

		o.Items = append(o.Items, item)
	}

	return nil
}
示例#4
0
// Exists checks if an Order instance already exists in database.
// FIXME: Use external db connector.
func (o *Order) Exists() bool {
	exists, err := utils.SimpleExists("orders", o.ID)
	utils.PanicOnError(err)
	return exists
}
示例#5
0
// Exists checks if a Customer instance already exists in database.
// FIXME: Use external db connector.
func (c *Customer) Exists() bool {
	exists, err := utils.SimpleExists("customers", c.ID)
	utils.PanicOnError(err)
	return exists
}
示例#6
0
// Exists checks if a Product instance is already in database.
// FIXME: Use external db connector.
func (p *Product) Exists() bool {
	exists, err := utils.SimpleExists("products", p.ID)
	utils.PanicOnError(err)
	return exists
}