private async Task <bool> AddOrderItemsToCartAsync(ICollection <OrderItemDto> orderItems, Customer customer, int storeId) { var shouldReturnError = false; foreach (var orderItem in orderItems) { if (orderItem.ProductId != null) { var product = await _productService.GetProductByIdAsync(orderItem.ProductId.Value); if (!product.IsRental) { orderItem.RentalStartDateUtc = null; orderItem.RentalEndDateUtc = null; } var attributesXml = await _productAttributeConverter.ConvertToXmlAsync(orderItem.Attributes.ToList(), product.Id); var errors = await _shoppingCartService.AddToCartAsync(customer, product, ShoppingCartType.ShoppingCart, storeId, attributesXml, 0M, orderItem.RentalStartDateUtc, orderItem.RentalEndDateUtc, orderItem.Quantity ?? 1); if (errors.Count > 0) { foreach (var error in errors) { ModelState.AddModelError("order", error); } shouldReturnError = true; } } } return(shouldReturnError); }
public async Task <IActionResult> CreateShoppingCartItem( [ModelBinder(typeof(JsonModelBinder <ShoppingCartItemDto>))] Delta <ShoppingCartItemDto> shoppingCartItemDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } var newShoppingCartItem = await _factory.InitializeAsync(); shoppingCartItemDelta.Merge(newShoppingCartItem); // We know that the product id and customer id will be provided because they are required by the validator. // TODO: validate var product = await _productService.GetProductByIdAsync(newShoppingCartItem.ProductId); if (product == null) { return(Error(HttpStatusCode.NotFound, "product", "not found")); } var customer = await CustomerService.GetCustomerByIdAsync(newShoppingCartItem.CustomerId); if (customer == null) { return(Error(HttpStatusCode.NotFound, "customer", "not found")); } var shoppingCartType = (ShoppingCartType)Enum.Parse(typeof(ShoppingCartType), shoppingCartItemDelta.Dto.ShoppingCartType); if (!product.IsRental) { newShoppingCartItem.RentalStartDateUtc = null; newShoppingCartItem.RentalEndDateUtc = null; } var attributesXml = await _productAttributeConverter.ConvertToXmlAsync(shoppingCartItemDelta.Dto.Attributes, product.Id); var currentStoreId = _storeContext.GetCurrentStore().Id; var warnings = await _shoppingCartService.AddToCartAsync(customer, product, shoppingCartType, currentStoreId, attributesXml, 0M, newShoppingCartItem.RentalStartDateUtc, newShoppingCartItem.RentalEndDateUtc, shoppingCartItemDelta.Dto.Quantity ?? 1); if (warnings.Count > 0) { foreach (var warning in warnings) { ModelState.AddModelError("shopping cart item", warning); } return(Error(HttpStatusCode.BadRequest)); } // the newly added shopping cart item should be the last one newShoppingCartItem = (await _shoppingCartService.GetShoppingCartAsync(customer, ShoppingCartType.ShoppingCart)).LastOrDefault(); // Preparing the result dto of the new product category mapping var newShoppingCartItemDto = await _dtoHelper.PrepareShoppingCartItemDTOAsync(newShoppingCartItem); var shoppingCartsRootObject = new ShoppingCartItemsRootObject(); shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto); var json = JsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty); return(new RawJsonActionResult(json)); }