public async Task <ActionResult <int> > PlaceOrder(Order order)
        {
            order.CreatedTime      = DateTime.Now;
            order.DeliveryLocation = new LatLong(51.5001, -0.1239);
            order.UserId           = GetUserId();

            // Enforce existence of Pizza.SpecialId and Topping.ToppingId
            // in the database - prevent the submitter from making up
            // new specials and toppings
            foreach (var pizza in order.Pizzas)
            {
                pizza.SpecialId = pizza.Special.Id;
                pizza.Special   = null;

                foreach (var topping in pizza.Toppings)
                {
                    topping.ToppingId = topping.Topping.Id;
                    topping.Topping   = null;
                }
            }

            _db.Orders.Attach(order);
            await _db.SaveChangesAsync();

            // In the background, send push notifications if possible
            var subscription = await _db.NotificationSubscriptions.Where(e => e.UserId == GetUserId()).SingleOrDefaultAsync();

            if (subscription != null)
            {
                _ = TrackAndSendNotificationsAsync(order, subscription);
            }

            return(order.OrderId);
        }
        public async Task <ActionResult <int> > PlaceOrder(Order order)
        {
            order.CreatedTime      = DateTime.Now;
            order.DeliveryLocation = new LatLong(51.5001, -0.1239);
            // order.UserId = GetUserId();

            // Enforce existence of Pizza.SpecialId and Topping.ToppingId
            // in the database - prevent the submitter from making up
            // new specials and toppings
            foreach (var pizza in order.Pizzas)
            {
                pizza.SpecialId = pizza.Special.Id;
                pizza.Special   = null;

                foreach (var topping in pizza.Toppings)
                {
                    topping.ToppingId = topping.Topping.Id;
                    topping.Topping   = null;
                }
            }

            _db.Orders.Attach(order);
            await _db.SaveChangesAsync();

            await Task.Delay(5000); // Wait 5 seconds

            return(order.OrderId);
        }
        public async Task <ActionResult <int> > PlaceOrder(Order order)
        {
            order.CreatedTime      = DateTime.Now;
            order.DeliveryLocation = new LatLong(51.5001, -0.1239);
            order.UserId           = GetUserId();

            _db.Orders.Attach(order);
            await _db.SaveChangesAsync();

            return(order.OrderId);
        }
        public async Task <ActionResult> PlaceOrder(Order order)
        {
            order.CreatedTime      = DateTime.Now;
            order.DeliveryLocation = new LatLong(51.5001, -0.1239);

            // stamp each order with the users identity
            order.UserId = GetUserId();

            _db.Orders.Attach(order);
            await _db.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <ActionResult> PlaceOrder([FromBody] Order order)
        {
            // Strip off milliseconds as deserialisation was failing in the client
            var now = DateTime.Now;

            order.CreatedTime      = new DateTime(now.Ticks - (now.Ticks % TimeSpan.TicksPerSecond), now.Kind);
            order.DeliveryLocation = new LatLong(51.5001, -0.1239);
            order.UserId           = GetUserId();

            _db.Orders.Attach(order);
            await _db.SaveChangesAsync();

            return(NoContent());
        }
示例#6
0
        public async Task <NotificationSubscription> Subscribe(NotificationSubscription subscription)
        {
            // We're storing at most one subscription per user, so delete old ones.
            // Alternatively, you could let the user register multiple subscriptions from different browsers/devices.
            var userId           = GetUserId();
            var oldSubscriptions = _db.NotificationSubscriptions.Where(e => e.UserId == userId);

            _db.NotificationSubscriptions.RemoveRange(oldSubscriptions);

            // Store new subscription
            subscription.UserId = userId;
            _db.NotificationSubscriptions.Attach(subscription);

            await _db.SaveChangesAsync();

            return(subscription);
        }