Example #1
0
func TestForestCreateOrder(t *testing.T) {

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

	expected := data.Order{
		State: data.OrderStateDraft,
		Time:  _time,
	}
	populate.Order(&expected)

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

	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))
	}

}
Example #2
0
func (engine *Engine) ListOrders(limit uint64, offset uint64) ([]data.Order, error) {

	if limit < 1 {
		return nil, ErrEmptyLimit
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return nil, err
	}

	orders, err := tx.ListOrders(limit, offset)
	if err != nil {
		return nil, tx.Rollback(err)
	}

	for i, order := range orders {
		populate.Order(&order)
		orders[i] = order
	}

	if err := tx.Commit(); err != nil {
		return nil, err
	}

	return orders, nil
}
Example #3
0
func (engine *Engine) CreateOrder(order *data.Order) error {

	if order.ID != 0 {
		return ErrNonEmptyID
	}

	if order.State == "" {
		order.State = data.OrderStateDraft
	} else if order.State != data.OrderStateDraft {
		return ErrNotDraft
	}

	if order.Time == 0 {
		order.Time = uint64(time.Now().UTC().Unix())
	} else if order.Time < uint64(time.Now().UTC().Add(-PastTimeTreshold).Unix()) {
		return ErrIsPast
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return err
	}

	if err := tx.CreateOrder(order); err != nil {
		return tx.Rollback(err)
	}

	populate.Order(order)

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}
Example #4
0
func (engine *Engine) UpdateOrder(order *data.Order) error {

	if order.ID == 0 {
		return ErrEmptyID
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return err
	}

	existingOrder := &data.Order{
		ID: order.ID,
	}

	if err := tx.LoadOrder(existingOrder); err != nil {
		return tx.Rollback(err)
	}

	if order.Time == 0 {
		order.Time = existingOrder.Time
	} else if order.Time < uint64(time.Now().UTC().Add(-PastTimeTreshold).Unix()) {
		return tx.Rollback(ErrIsPast)
	}

	if order.State == "" {
		order.State = existingOrder.State
	} else if order.State != existingOrder.State {
		if existingOrder.State != data.OrderStateDraft {
			return tx.Rollback(ErrNotDraft)
		}
		switch order.State {
		case data.OrderStatePlaced:
			if len(existingOrder.Items) < 1 {
				return tx.Rollback(ErrOrderEmpty)
			}
		case data.OrderStateCancelled:
		default:
			return tx.Rollback(ErrUnknownState)
		}
	}

	if err := tx.UpdateOrder(order); err != nil {
		return tx.Rollback(err)
	}

	populate.Order(order)

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}
Example #5
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))
	}

}
Example #6
0
func (engine *Engine) LoadOrder(order *data.Order) error {

	if order.ID == 0 {
		return ErrEmptyID
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return err
	}

	if err := tx.LoadOrder(order); err != nil {
		return tx.Rollback(err)
	}

	populate.Order(order)

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}