Пример #1
0
        public async Task <AddResult> AddOrder(OrderAddModel model)
        {
            if (model.TableId == null && model.CustomerId == null)
            {
                throw new RestaurantBadRequestException("Asztal/kiszállítási adatok nélküli rendelés nem vehető fel!");
            }

            var waiter = await DbContext.Users.FindAsync(model.WaiterId);

            if (waiter == null)
            {
                throw new RestaurantNotFoundException("A megadott pincér nem létezik!");
            }

            await StatusService.CheckRightsForOrderAddDelete();

            OrderSession orderSession = null;

            if (model.TableId != null)
            {
                orderSession = await DbContext.OrderSessions.Where(os => os.TableId == model.TableId && os.Status == OrderSessionStatus.Active).SingleOrDefaultAsync();
            }

            if (orderSession == null)
            {
                orderSession = await OrderSessionService.CreateNewSession(model);
            }

            var order = DbContext.Orders.Add(new Order()
            {
                WaiterUserId   = model.WaiterId,
                Status         = OrderStatus.Ordering,
                OrderSessionId = orderSession.Id,
                OpenedAt       = DateTime.Now
            });

            await DbContext.SaveChangesAsync();

            return(new AddResult(order.Entity.Id));
        }
        public async Task <OrderSession> CreateNewSession(OrderAddModel model)
        {
            if (model.TableId != null)
            {
                var table = await DbContext.Tables.FindAsync(model.TableId);

                if (table == null)
                {
                    throw new RestaurantNotFoundException("A megadott asztal nem létezik!");
                }
            }
            else if (model.CustomerId != null)
            {
                var customer = await DbContext.Customers.FindAsync(model.CustomerId);

                if (customer == null)
                {
                    throw new RestaurantNotFoundException("A megadott vendég nem létezik!");
                }
            }
            else                //Both null
            {
                throw new RestaurantBadRequestException("Vagy az asztal vagy a vendég megadása kötelező!");
            }

            var orderSession = DbContext.OrderSessions.Add(new OrderSession()
            {
                TableId    = model.TableId,
                CustomerId = model.CustomerId,
                Status     = OrderSessionStatus.Active,
                OpenedAt   = DateTime.Now
            });

            await DbContext.SaveChangesAsync();

            return(orderSession.Entity);
        }
 public async Task <AddResult> AddOrder(OrderAddModel order)
 => await OrderService.AddOrder(order);