예제 #1
0
        public async Task CreateOrdersAsync(IEnumerable <int> restaurantIds)
        {
            var existingRestaurantIds = await _context.Restaurants
                                        .Where(r => restaurantIds.Contains(r.Id))
                                        .Select(r => r.Id)
                                        .ToListAsync();

            var ordersToAdd = existingRestaurantIds
                              .Select(id => new Order
            {
                Username     = Context.User.GetSub(),
                OrderedAt    = DateTime.Now.Truncate(),
                CompletedAt  = null,
                RestaurantId = id,
            })
                              .ToList();

            await _context.Orders.AddRangeAsync(ordersToAdd);

            await _context.SaveChangesAsync();

            _updateService.SendRestaurantsUpdated(existingRestaurantIds).Forget();

            var newOrderIds = ordersToAdd.Select(o => o.Id).ToList();

            _updateService.SendDispatchPendingUpdate(newOrderIds).Forget();
            _updateService.SendUserOrdersCreated(newOrderIds, Context.User.GetSub()).Forget();
        }
예제 #2
0
        public async Task CompleteOrderAsync(int orderId)
        {
            var order = await _context.Orders.SingleAsync(o => o.Id == orderId);

            if (order.CompletedAt != null)
            {
                throw new AlreadyCompletedOrderException(orderId);
            }

            order.CompletedAt = DateTime.Now.Truncate();
            await _context.SaveChangesAsync();

            _updateService.SendDispatchOrderCompleted(orderId).Forget();
            _updateService.SendRestaurantsUpdated(new[] { order.RestaurantId }).Forget();
            _updateService.SendUserOrdersUpdated(new[] { orderId }, order.Username).Forget();
        }