Exemple #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
}
Exemple #2
0
// Read retrieves a single Product from database.
func (p *Product) Read(id uuid.UUID) error {
	pdb, err := utils.OpenDbConnection()
	if err != nil {
		return err
	}

	db := goqu.New(settings.DatabaseDriver, pdb)
	sql, args, err := db.From("products").
		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(&p.ID, &p.Name, &p.Description, &p.Price)
		if err != nil {
			return err
		}
	} else {
		return fmt.Errorf("No Product found with id %s.", id)
	}

	return nil
}
Exemple #3
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()
}
Exemple #4
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
}