Ejemplo n.º 1
0
        public WebStore.Domain.Entities.Order CreateOrder(OrderViewModel orderModel, CartViewModel transformCart, string userName)
        {
            User user = null;

            if (userName != null)
            {
                user = _userManager.FindByNameAsync(userName).Result;
            }
            using (var transaction = _context.Database.BeginTransaction())
            {
                var order = new WebStore.Domain.Entities.Order()
                {
                    Address = orderModel.Address,
                    Name    = orderModel.Name,
                    User    = user,
                    Phone   = orderModel.Phone,
                    Date    = DateTime.Now
                };
                _context.Orders.Add(order);


                foreach (var item in transformCart.Items)
                {
                    var productVm = item.Key;
                    var product   = _context.Products.FirstOrDefault(p => p.Id.Equals(productVm.Id));
                    if (product == null)
                    {
                        throw new InvalidOperationException("Продукт не найден");
                    }
                    var orderItem = new OrderItem()
                    {
                        Order    = order,
                        Price    = product.Price,
                        Quantity = item.Value,
                        Product  = product
                    };
                    _context.OrderItems.Add(orderItem);
                }

                _context.SaveChanges();
                transaction.Commit();
                return(order);
            }
        }
        public int StoreOrderAndOrderItems(Cart cart)
        {
            Order order = new Order();
            //order.OrderID
            order.OrderDate = DateTime.Now;
            order.ShipDate = DateTime.Now;
            order.TotalOrder = cart.ComputeTotalValue();
            order.ProductCount = cart.ComputeNumItems();
            order.FirstName = string.Empty;
            order.LastName = string.Empty;
            order.Company = string.Empty;
            order.Email = string.Empty;
            order.StreetLine1 = string.Empty;
            order.StreetLine2 = string.Empty;
            order.StreetLine3 = string.Empty;
            order.City = string.Empty;
            order.PostalCode = string.Empty;
            order.County = string.Empty;
            order.Country = string.Empty;
            order.PaymentConfirmation = string.Empty;
            order.CreatedAt = DateTime.Now;
            order.UpdatedAt = DateTime.Now;
            order.BillToAddressID = 1;
            order.ShipToAddressID = 1;
            order.CustomerID = 1;

            orderRepository.SaveOrder(order);

            foreach (var line in cart.Lines)
            {
                OrderItem orderItem = new OrderItem();

                Product product = repository.Products
                 .FirstOrDefault(p => p.ProductID == line.Product.ProductID);

                //orderItem.OrderItemID
                orderItem.Name = line.Product.Name;
                orderItem.Description= line.Product.Description;
                orderItem.Price = line.Product.Price;
                orderItem.Category = line.Product.Category;
                orderItem.Special = line.Product.Special;
                orderItem.ImageData = line.Product.ImageData;
                orderItem.ImageMimeType = line.Product.ImageMimeType;
                orderItem.Seller = line.Product.Seller;
                orderItem.Buyer = line.Product.Buyer;
                orderItem.Quantity = line.Quantity;
                orderItem.OrderID = order.OrderID;
                orderItem.ProductID = product.ProductID;
                orderItem.CategoryID = product.CategoryID;
                orderItemRepository.SaveOrderItem(orderItem);

            }

            return order.OrderID;
        }
        public void SaveOrder(Order order)
        {
            if (order.OrderID == 0)
            {

                order.CreatedAt = DateTime.Now;
                order.UpdatedAt = DateTime.Now;
                context.Orders.Add(order);
            }
            else
            {
                Order dbEntry = context.Orders.Find(order.OrderID);
                if (dbEntry != null)
                {
                    dbEntry.OrderDate = order.OrderDate;
                    dbEntry.ShipDate = order.ShipDate;
                    dbEntry.TotalOrder = order.TotalOrder;
                    dbEntry.ProductCount = order.ProductCount;

                    dbEntry.FirstName = order.FirstName;
                    dbEntry.LastName = order.LastName;
                    dbEntry.Company = order.Company;
                    dbEntry.Email = order.Email;
                    dbEntry.StreetLine1 = order.StreetLine1;
                    dbEntry.StreetLine2 = order.StreetLine2;
                    dbEntry.StreetLine3 = order.StreetLine3;
                    dbEntry.City = order.City;
                    dbEntry.PostalCode = order.PostalCode;
                    dbEntry.County = order.County;
                    dbEntry.Country = order.Country;
                    dbEntry.PaymentConfirmation = order.PaymentConfirmation;

                    dbEntry.UpdatedAt = DateTime.Now;
                }
            }
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }

              throw raise;
            }
            catch (Exception e)
            {
                int i = 0;
                Console.WriteLine("In Main catch block. Caught: {0}", e.Message);
                Console.WriteLine("Inner Exception is {0}", e.InnerException);

            }
        }