func (engine *Engine) ListOrderItems(orderID uint64, limit uint64, offset uint64) ([]data.OrderItem, error) { if orderID == 0 { return nil, ErrEmptyID } if limit < 1 { return nil, ErrEmptyLimit } tx, err := engine.storage.Begin() if err != nil { return nil, err } items, err := tx.ListOrderItems(orderID, limit, offset) if err != nil { return nil, tx.Rollback(err) } for i, item := range items { populate.OrderItem(&item) items[i] = item } if err := tx.Commit(); err != nil { return nil, err } return items, nil }
func (engine *Engine) LoadOrderItem(orderID uint64, item *data.OrderItem) error { if orderID == 0 { return ErrEmptyID } if item.Product.ID == 0 { return ErrEmptyID } tx, err := engine.storage.Begin() if err != nil { return err } err = tx.LoadOrderItem(orderID, item) if err != nil { return tx.Rollback(err) } populate.OrderItem(item) if err := tx.Commit(); err != nil { return err } return nil }
func TestForestOrderItemUpdate(t *testing.T) { product := data.Product{ Name: generateName("Product"), Price: data.Price{ Vat: 20, Net: 100, }, } createProduct(&product) order := data.Order{ Items: []data.OrderItem{ data.OrderItem{ Product: product, Quantity: 1, }, }, } createOrder(&order) expected := data.OrderItem{ Product: product, Quantity: 20, } populate.OrderItem(&expected) payload := data.OrderItem{ Product: data.Product{ ID: product.ID, }, Quantity: 20, } got := data.OrderItem{} r := getForestClient().PUT(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID).Content(payload, MimeJSON)) 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)) } }
func (engine *Engine) CreateOrderItem(orderID uint64, item *data.OrderItem) error { if orderID == 0 { return ErrEmptyID } if item.Product.ID == 0 { return ErrEmptyID } if item.Quantity < 1 { return ErrOrderItemEmpty } tx, err := engine.storage.Begin() if err != nil { return err } order := &data.Order{ ID: orderID, } if err := tx.LoadOrder(order); err != nil { return tx.Rollback(err) } if order.State != data.OrderStateDraft { return tx.Rollback(ErrNotDraft) } if err := tx.CreateOrderItem(orderID, item); err != nil { return tx.Rollback(err) } populate.OrderItem(item) if err := tx.Commit(); err != nil { return err } return nil }