Exemplo n.º 1
0
func TestForestCreateOrderWithItems(t *testing.T) {

	_time := uint64(time.Now().UTC().Unix())

	expected := data.Order{
		State: data.OrderStateDraft,
		Time:  _time,
	}

	payload := data.Order{
		Time: _time,
	}

	for i := 1; i <= 3; i++ {

		product := data.Product{
			Name: generateName("Product"),
			Price: data.Price{
				Vat: 20,
				Net: uint64(100 * i),
			},
		}

		createProduct(&product)

		item := data.OrderItem{
			Product:  product,
			Quantity: uint64(i),
		}

		// newest come first:
		payload.Items = append([]data.OrderItem{item}, payload.Items...)
		expected.Items = append([]data.OrderItem{item}, expected.Items...)

	}

	populate.Order(&expected)

	got := data.Order{}

	r := getForestClient().POST(t, forest.Path("/orders/{}", "").Content(payload, MimeJSON))

	forest.ExpectStatus(t, r, http.StatusCreated)

	forest.ExpectJSONDocument(t, r, &got)

	expected.ID = got.ID

	if !reflect.DeepEqual(got, expected) {
		t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected))
	}

}
Exemplo n.º 2
0
func Order(order *data.Order) {

	order.Price.Vat = 0
	order.Price.Net = 0
	order.Price.Gross = 0

	if order.Items == nil {
		order.Items = []data.OrderItem{}
	}

	for i, item := range order.Items {
		OrderItem(&item)
		order.Price.Net += item.Price.Net
		order.Price.Gross += item.Price.Gross
		order.Items[i] = item
	}

}
Exemplo n.º 3
0
func TestForestListOrderItems(t *testing.T) {

	order := data.Order{}

	for i := 1; i <= 3; i++ {

		product := data.Product{
			Name: generateName("Product"),
			Price: data.Price{
				Vat: 20,
				Net: 100,
			},
		}

		createProduct(&product)

		item := data.OrderItem{
			Product:  product,
			Quantity: 1,
		}

		order.Items = append([]data.OrderItem{item}, order.Items...)

	}

	createOrder(&order)

	expected := order.Items

	got := []data.OrderItem{}

	r := getForestClient().GET(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Query("limit", len(expected)))

	forest.ExpectStatus(t, r, http.StatusOK)

	forest.ExpectJSONDocument(t, r, &got)

	if !reflect.DeepEqual(got, expected) {
		t.Errorf("Got: %s\n\nExpected: %s", testutils.JSON(got), testutils.JSON(expected))
	}

}
Exemplo n.º 4
0
func (tx *Tx) LoadOrder(order *data.Order) error {
	var err error

	err = tx.tx.QueryRow(
		`
			SELECT state, time FROM orders
			WHERE id = $1
		`,
		order.ID,
	).Scan(
		&order.State,
		&order.Time,
	)
	if err != nil {
		return err
	}
	// end SELECT FROM orders

	// reset item list:
	order.Items = nil

	rows, err := tx.tx.Query(
		`
			SELECT
				products.id,
				products.name,
				products.price_vat,
				products.price_net,
				order_items.quantity
			FROM order_items
			JOIN products ON products.id = order_items.product_id
			WHERE order_items.order_id = $1
		`,
		order.ID,
	)
	if err != nil {
		return err
	}
	// end SELECT FROM order_items JOIN products

	for rows.Next() {

		item := data.OrderItem{}

		err := rows.Scan(
			&item.Product.ID,
			&item.Product.Name,
			&item.Product.Price.Vat,
			&item.Product.Price.Net,
			&item.Quantity,
		)
		if err != nil {
			return err
		}

		order.Items = append(order.Items, item)

	} // end for rows

	return nil
}
Exemplo n.º 5
0
func (tx *Tx) UpdateOrder(order *data.Order) error {

	err := tx.tx.QueryRow(
		`
			UPDATE orders
			SET state = $2, time = $3
			WHERE id = $1
			RETURNING id
		`,
		order.ID,
		order.State,
		order.Time,
	).Scan(
		&order.ID,
	)
	if err != nil {
		return err
	}
	// end UPDATE orders

	// reset items list (if method got well-populated order);
	// TODO: probably, update order items as well (if len > 0)?
	order.Items = nil

	rows, err := tx.tx.Query(
		`
			SELECT
				products.id,
				products.name,
				products.price_vat,
				products.price_net,
				order_items.quantity
			FROM order_items
			JOIN products ON products.id = order_items.product_id
			WHERE order_items.order_id = $1
		`,
		order.ID,
	)
	if err != nil {
		return err
	}
	// end SELECT FROM order_items JOIN products

	for rows.Next() {

		item := data.OrderItem{}

		err := rows.Scan(
			&item.Product.ID,
			&item.Product.Name,
			&item.Product.Price.Vat,
			&item.Product.Price.Net,
			&item.Quantity,
		)
		if err != nil {
			return err
		}

		order.Items = append(order.Items, item)

	} // end for rows

	return nil
}