public int Save(Order order)
        {
            using (var context = new LunchDbContext())
            {
                context.Orders.Add(order);
                context.SaveChanges();
            }

            return order.Id;
        }
        public ActionResult Submit(SubmitOrderModel model)
        {
            var orderRepository = new OrderRepository();
            var order = new Order();
            order.Email = model.Email;
            foreach (var orderedDish in model.Dishes)
            {
                if (orderedDish.Quantity > 0)
                {
                    order.AddOrderDetail(orderedDish.Id, orderedDish.Quantity);
                }
            }

            var orderId = orderRepository.Save(order);

            return Json(new { orderId, confirmLink = Url.Action("Confirm", new { id = orderId }) });
        }
Example #3
0
        public JsonResult AddOrderToDb(string[][] data)
        {
            var context = new SushiTest1Entities1();
            var ordersDetails = context.OrderDetails.ToList();
            Order order = new Order();
            order.Name = data[0][0] ?? "no data";
            order.PhoneNumber = data[0][1];
            order.Email = data[0][2] ?? "no data";
            order.Street = data[0][3];
            order.House = data[0][4];
            order.Room = data[0][5];

            //context.Orders.Add(order);
            context.AddOrder(order.Name, order.PhoneNumber, order.Email, order.Street, order.House, order.Room);

            context.SaveChanges();

            var nextContext = new SushiTest1Entities1();
            var statusList = nextContext.OrderStatus.ToList();
            var orders = nextContext.Orders.ToList();
            var lastId = orders[orders.Count - 1].OrderId;

            var myStatus = statusList[4];
            var myOrder = orders[orders.Count - 1];

            var lastStatus = new OrdersTimeChanged();

            lastStatus.OrderId = lastId;
            lastStatus.OrderStatus = 5;
            lastStatus.Time = DateTime.Now;

            //nextContext.OrdersTimeChangeds.Add(lastStatus);
            nextContext.AddOrderTimeChanged(lastStatus.OrderId,
                lastStatus.OrderStatus, lastStatus.Time);

            var productList = nextContext.Products.ToList();
            var resList = new List<Product>();

            var productforUpd = new List<ProductForUpdate>();
            for (int i = 0; i < data[1].Length; i++)
            {
                var prod = new ProductForUpdate();
                prod.Id = Convert.ToInt32(data[1][i]);
                prod.count = Convert.ToInt32(data[2][i]);
                productforUpd.Add(prod);
            }

            foreach (var q in productforUpd)
            {
                foreach (var w in productList)
                {
                    if (q.Id == w.ProductId)
                    {
                        nextContext.AddOrdersTimeProduct(q.count + w.NumberOfOrders, q.Id);
                        nextContext.SaveChanges();
                    }
                }
            }

            nextContext.SaveChanges();

            OrderDetail orderDetail = new OrderDetail();

            for (int i = 0; i < data[1].Length; i++)
            {
                orderDetail.OrderId = lastId;
                orderDetail.ProductId = Convert.ToInt32(data[1][i]);
                orderDetail.Count = Convert.ToInt32(data[2][i]);
                orderDetail.Price = Convert.ToDecimal(data[3][i]);
                //nextContext.OrderDetails.Add(orderDetail);
                nextContext.AddOrderDetails(orderDetail.OrderId, orderDetail.ProductId, orderDetail.Count,
                    orderDetail.Price);
                nextContext.SaveChanges();
            }

            var model = new object[1];
            model[0] = new { res = lastId };
            return Json(model);
        }