public Order CreateOrder(IUnitOfWork unitOfWork, ICartService cartService, IOrderService orderService, IOrderDetailService orderDetailService, IProductService productService, ICategoryService categoryService, Order order) { decimal orderTotal = 0; var cartItems = GetCartItems(cartService, productService, categoryService); foreach (var item in cartItems) { var orderDetail = new OrderDetail { ProductId = item.ProductId, OrderId = order.Id, UnitPrice = item.Product.Price, Quantity = item.Count }; orderTotal += (item.Count * item.Product.Price); orderDetailService.Insert(orderDetail); } // update total into order order.Total = orderTotal; orderService.Update(order); // commit to changes unitOfWork.SaveChanges(); //emty cart session EmptyCart(unitOfWork, cartService); return order; }
private int CreateOrder(CheckoutModel model) { var order = new Order(); TryUpdateModel(model); try { order.CreatedDate = DateTime.Now; order.CustomerName = model.FullName; order.Email = model.Email; order.Address = model.ShippingAddress; order.Phone = model.Phone; order.OrderStatusId = (int)OrderStatusType.New; order.Note = model.Note; order.HomeDelivery = true; order.Deleted = false; _orderService.Insert(order); _unitOfWork.SaveChanges(); var cart = ShoppingCart.GetCart(this.HttpContext); order = cart.CreateOrder(_unitOfWork, _cartService, _orderService, _orderDetailService, _productService, _categoryService, order); return order.Id; } catch { return 0; } }
public string Edit(Order model) { if (ModelState.IsValid) { var entity = _orderService.Find(model.Id); if (entity != null) { if (entity.OrderStatusId != model.OrderStatusId) entity.OrderStatusId = model.OrderStatusId; if (entity.CustomerName != model.CustomerName) entity.CustomerName = model.CustomerName; if (entity.Email != model.Email) entity.Email = model.Email; if (entity.Phone != model.Phone) entity.Phone = model.Phone; if (entity.Address != model.Address) entity.Address = model.Address; entity.ObjectState = ObjectState.Modified; _orderService.Update(entity); _unitOfWork.SaveChanges(); } return JsonConvert.SerializeObject(new { Status = 1, Message = "Update success." }); } else { return JsonConvert.SerializeObject(new { Status = 0, Message = "Update unsuccess." }); } }