public OrderItemViewModel SaveOrderItemCreateModelToDb(OrderItemCreateModel createModel) { OrderItem orderItem = new OrderItem() { ItemId = createModel.Item.Id, OrderId = createModel.OrderId, ItemsCount = createModel.ItemsCount, ItemPrice = createModel.ItemPrice }; _unitOfWork.OrderItems.Create(orderItem); return(GetOrderItemModelById(orderItem.Id)); }
public async Task Create(OrderItemCreateModel orderItem) { if (orderItem.OrderId == null || await _orderAppService.GetById(orderItem.OrderId) == null) { throw new Exception("Order not found"); } if (orderItem.ProductId == null) { throw new Exception("No product given"); } var product = await _productAppService.GetById(orderItem.ProductId); if (product == null) { throw new Exception("No product match has been found."); } int newProductStock = product.Stock - orderItem.Quantity; product.Stock = newProductStock; await _productAppService.UpdateStock(product.Stock, product.Id); Models.OrderItem newOrderItem = new Models.OrderItem() { Number = product.Number, Price = product.Price, ProductId = product.Id, Description = product.Description, Name = product.Name, OrderId = orderItem.OrderId, Quantity = orderItem.Quantity, SKUCode = product.SKUCode, Tax = product.TaxGroup.Percentage, Weight = product.Weight }; await _repository.InsertAsync(newOrderItem); return; }