public async Task <int> PlaceOrder(ShoppingCart carts, OrderVM order, string userEmail) { var orderToAdd = _mapper.Map <OrderVM, Order>(order); orderToAdd.PaymentStatus = false; orderToAdd.CreatedDate = DateTime.Now; orderToAdd.CreatedBy = "admin"; orderToAdd.Status = OrderStatus.Waiting; if (!string.IsNullOrEmpty(userEmail)) { var user = _applicationUserRepository.GetUserByUserName(userEmail); orderToAdd.User = user; } if (!string.IsNullOrEmpty(carts.CartPromoCode)) { var discount = await _discountRepository.GetDiscountByPromoCode(carts.CartPromoCode); orderToAdd.Discount = discount; } else { var discount = await _discountRepository.GetDiscountById(1); orderToAdd.Discount = discount; } await _orderRepository.AddAsync(orderToAdd); foreach (var item in carts.Cart) { var product = await _productRepository.GetSingleByIDAsync(item.Product.ProductId); if (product.AvailableQuantity >= item.Quantity) { var orderDetail = new OrderDetail() { OrderId = orderToAdd.OrderId, ProductId = item.Product.ProductId, Quantity = item.Quantity }; await _orderDetailRepository.AddAsync(orderDetail); //Update product sold var rank = await _productRankRepository.GetSingleByIDAsync(item.Product.ProductId); rank.Sold += item.Quantity; await _productRankRepository.UpdateAsync(rank); product.AvailableQuantity -= item.Quantity; await _productRepository.UpdateAsync(product); } else { return(0); } } return(await _unitOfWork.SaveAsync()); }
public async Task <IActionResult> Post([FromBody] AddShoppingCartModel model) { if (string.IsNullOrEmpty(model.UserID) || Guid.Empty == model.ProductID || 0 >= model.Amount) { return(ResponseHelper.NotAcceptable()); } var product = await _productRepository.GetAsync(model.ProductID); if (null == product) { return(NotFound()); } List <ShipAddress> addressList = await _shipAddressRepository.GetAddressAsync(model.UserID, 50, 0); Order order = new Order() { Address = addressList.Count > 0 ? addressList[0].ToString() + "(mobile)" : string.Empty + "(mobile)", OrderID = Guid.NewGuid(), OrderDate = DateTime.Now, OrderStatus = OrderStatus.PendingPayment, UserID = model.UserID, }; OrderDetail orderDetail = new OrderDetail() { OrderID = order.OrderID, PlaceDate = DateTime.Now, ProductName = product.ProductName, ProductID = product.ProductID, ThumbImagePath = product.ThumbnailImage, UnitPrice = product.UnitPrice, Quantity = model.Amount, SubTotal = product.UnitPrice * model.Amount }; order.TotalPrice = orderDetail.SubTotal; Guid orderID = await _orderRepository.AddAsync(order); // Add order failed. if (orderID == Guid.Empty) { return(ResponseHelper.InternalServerError()); } int count = await _orderDetailRepository.AddAsync(orderDetail); if (0 < count) { return(Ok()); } else { return(ResponseHelper.InternalServerError()); } }
public async Task <IActionResult> Create() { string idList = HttpContext.Request.Form["IDListCtrl"].ToString(); string productID = HttpContext.Request.Form["prodIdCtrl"].ToString(); int amount = int.Parse(HttpContext.Request.Form["amountCtrl"].ToString()); string address = HttpContext.Request.Form["addressCtrl"].ToString(); Order order = new Order() { Address = address, OrderID = Guid.NewGuid(), OrderDate = DateTime.Now, OrderState = OrderStatus.PendingPayment, UserID = User.Identity.Name, }; List <OrderDetail> orderDetails = null; if (string.IsNullOrEmpty(idList)) { orderDetails = await CreateOrderDetailsByProductAsync( order.OrderID, productID, amount); } else { orderDetails = await CreateOrderDetailsByCartIDListAsync( order.OrderID, idList); } foreach (OrderDetail od in orderDetails) { order.TotalPrice += od.SubTotal; } Guid orderID = await _orderRepository.AddAsync(order); // Add order failed. if (orderID == Guid.Empty) { Redirect("/Home"); } foreach (OrderDetail od in orderDetails) { await _orderDetailRepository.AddAsync(od); } return(Redirect("/Orders")); }
public void CreateOrderDetail(OrderDetail OrderDetail) { _repository.AddAsync(OrderDetail); }