Пример #1
0
        public async Task <IActionResult> Post([FromBody] InputModel value)
        {
            var customer = _context.Customers.Find(value.Customer.CustomerId);

            await using var transaction = await _context.Database.BeginTransactionAsync();

            Order newOrder = null;

            try
            {
                if (customer == null)
                {
                    newOrder = new Order {
                        Customer = value.Customer
                    };
                }
                else
                {
                    newOrder = new Order {
                        Customer = customer
                    };
                }
                var sub   = (float)Math.Round(value.Pizzas.Select(o => o.Subtotal).Sum() + value.Sides.Select(o => o.Subtotal).Sum(), 2);
                var tax   = (float)Math.Round(sub * 0.13, 2);
                var total = (float)Math.Round(sub + tax, 2);
                newOrder.Subtotal = sub;
                newOrder.Tax      = tax;
                newOrder.Total    = total;

                foreach (var side in value.Sides)
                {
                    SideOrder newSideOrder = new SideOrder {
                        Order = newOrder, SideId = side.SideId, Qty = side.Qty
                    };
                    await _context.SideOrders.AddAsync(newSideOrder);
                }


                foreach (var pizza in value.Pizzas)
                {
                    //피자 생성
                    Entities.Pizza newPizza = new Entities.Pizza {
                        Order = newOrder, TypeId = pizza.TypeId, SizeId = pizza.SizeId, Qty = pizza.Qty
                    };
                    await _context.Pizzas.AddAsync(newPizza);

                    //피자에 토핑연결
                    foreach (var topping in pizza.Toppings)
                    {
                        PizzaTopping pizzaTopping = new PizzaTopping {
                            Pizza = newPizza, ToppingId = topping
                        };
                        await _context.PizzaToppings.AddAsync(pizzaTopping);
                    }
                }

                //진행상황
                OrderProcess newOrderProcess = new OrderProcess {
                    Order = newOrder, ProcessId = _context.Processes.Where(p => p.ProcessNum == 1).FirstOrDefault().ProcessId
                };
                var ret = await _context.OrderProcess.AddAsync(newOrderProcess);

                await _context.SaveChangesAsync();

                Task.Run(() => PizzaMakingTask(newOrder.OrderId));

                await transaction.CommitAsync();
            }
            catch (DbUpdateException updateEx)
            {
                return(StatusCode(500, "Internal server error"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
            newOrder.Pizzas     = _context.Pizzas.Where(o => o.OrderId == newOrder.OrderId).ToList();
            newOrder.SideOrders = _context.SideOrders.Where(o => o.OrderId == newOrder.OrderId).ToList();
            return(new JsonResult(newOrder));
        }
Пример #2
0
 public void SetPizza(Entities.Pizza pizza)
 {
     this.pizza = pizza;
 }