func getOrder()

in orders.go [54:92]


func getOrder(ctx context.Context, db *sqlx.DB, id int) (*Order, error) {
	queryString := db.Rebind(`SELECT
  orders.id, orders.created_at, customer_id
FROM orders WHERE orders.id=?`)

	row := db.QueryRowContext(ctx, queryString, id)
	var order Order
	if err := row.Scan(&order.ID, &order.CreatedAt, &order.CustomerID); err != nil {
		return nil, errors.Wrap(err, "querying order")
	}

	queryString = db.Rebind(`SELECT
  product_id, amount,
  products.sku, products.name, products.description,
  products.type_id, products.stock, products.cost, products.selling_price
FROM products JOIN order_lines ON products.id=order_lines.product_id
WHERE order_lines.order_id=?`)

	rows, err := db.QueryContext(ctx, queryString, id)
	if err != nil {
		return nil, errors.Wrap(err, "querying product order lines")
	}
	defer rows.Close()

	var lines []ProductOrderLine
	for rows.Next() {
		var l ProductOrderLine
		if err := rows.Scan(
			&l.ID, &l.Amount,
			&l.SKU, &l.Name, &l.Description,
			&l.TypeID, &l.Stock, &l.Cost, &l.SellingPrice,
		); err != nil {
			return nil, err
		}
		lines = append(lines, l)
	}
	order.Lines = lines
	return &order, rows.Err()
}