public async Task <IActionResult> PutRecipe([FromRoute] int id, [FromBody] Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != recipe.Id)
            {
                return(BadRequest());
            }

            _context.Entry(recipe).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult> AsyncCurbsideOrders([FromBody] PostSyncCurbsideOrdersRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                var orderToSave = new CurbSideOrder
                {
                    For    = request.For,
                    Items  = request.Items,
                    Status = CurbSideOrderStatus.Processing
                };

                _context.CurbSide.Add(orderToSave);
                await _context.SaveChangesAsync();

                var response = new GetCurbsideOrderResponse
                {
                    Id         = orderToSave.Id,
                    For        = orderToSave.For,
                    Items      = orderToSave.Items,
                    PickupDate = null,
                    Status     = orderToSave.Status
                };
                await _channel.AddCurbside(new CurbsideChannelRequest { ReservationId = response.Id });

                return(CreatedAtRoute("curbsideorders#getbyid", new { id = response.Id }, response));
            }
        }
        public async Task <CurbsideOrder> AddOrder(PostCurbsideOrderRequest orderToPlace, bool doAsync)
        {
            var order = _mapper.Map <CurbsideOrder>(orderToPlace);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            if (doAsync)
            {
                try
                {
                    await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id });
                }
                catch (OperationCanceledException ex)
                {
                    // do something?
                    throw;
                }
            }
            else
            {
                var numberOfItems = order.Items.Split(',').Count();
                for (var t = 0; t < numberOfItems; t++)
                {
                    await Task.Delay(300);
                }
            }
            return(order);
        }
        async Task <CurbsideOrder> IDoCurbsideCommands.AddOrder(PostCurbsideOrderRequest orderToPlace, bool doAsync)
        {
            // place that order..
            // await Task.Delay(3000);
            // that represents all the very important things we have to do.
            var order = _mapper.Map <CurbsideOrder>(orderToPlace);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            if (doAsync)
            {
                try
                {
                    await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id });
                }
                catch (OperationCanceledException ex)
                {
                    // for some reason the background worker punched you in the nose (more likely the channel).
                    // Do some cleanup.. Whatever.
                    throw;
                }
            }
            else
            {
                var numberOfItems = order.Items.Split(',').Count();
                for (var t = 0; t < numberOfItems; t++)
                {
                    await Task.Delay(300); // what important work needs to happen.
                }
            }
            return(order);
        }
Beispiel #5
0
        public virtual async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request)
        {
            // do the "procesing" (??)
            await Task.Delay(1000 *request.Items.Length);

            // produce a pickupDateTime for it.
            // Save it to the database,
            var order = new CurbsideOrder
            {
                For           = request.For,
                Items         = String.Join(",", request.Items),
                PickupReadyAt = DateTime.Now.AddHours(new Random().Next(2, 5))
            };

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            // map it, return in.
            return(new GetCurbsideResponse
            {
                Id = order.Id,
                For = order.For,
                Items = order.Items.Split(',').Select(int.Parse).ToArray(),
                PickupReadyAt = order.PickupReadyAt.Value
            });
        }
Beispiel #6
0
        public async Task <GetProductDetailsResponse> Add(PostProductRequest productToAdd)
        {
            //
            var product = _mapper.Map <Product>(productToAdd);

            //product.Category.Name = productToAdd.Category;
            var category = await _context.Categories.SingleOrDefaultAsync(c => c.Name == productToAdd.Category);

            if (category == null)
            {
                var newCategory = new Category {
                    Name = productToAdd.Category
                };
                _context.Categories.Add(newCategory);
                product.Category = newCategory;
            }
            else
            {
                product.Category = category;
            }

            product.Price = productToAdd.Cost.Value * _configurationForPricing.Value.Markup; // get this from config

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(_mapper.Map <GetProductDetailsResponse>(product));
        }
        public async Task <GetProductDetailsResponse> Add(PostProductRequest productToAdd)
        {
            // we have a PostProductRequest (which we assume to be valid at this point)
            var product = _mapper.Map <Product>(productToAdd);
            // deal the category...
            var category = await _context.Categories.SingleOrDefaultAsync(c => c.Name == productToAdd.Category);

            if (category == null)
            {
                // do the upsert.
                var newCategory = new Category {
                    Name = productToAdd.Category
                };
                _context.Categories.Add(newCategory);
                product.Category = newCategory;
            }
            else
            {
                product.Category = category;
            }
            // take the cost, apply the markup, and make that the price.
            product.Price = productToAdd.Cost.Value * _pricingOptions.Value.Markup; // TODO: We want this "configurable"

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            // We have to map it to a Product (that's what we can add to the _context.Products
            // And THEN we have to map that to a GetProductDetailsResponse because that's what we have to return.
            return(_mapper.Map <GetProductDetailsResponse>(product));
        }
Beispiel #8
0
        public async Task <GetProductDetailsResponse> AddProduct(PostProductRequest productToAdd)
        {
            var product = _mapper.Map <Product>(productToAdd);

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(_mapper.Map <GetProductDetailsResponse>(product));
        }
        async Task <CurbsideOrder> IMapCurbsideOrders.PlaceOrder(CreateCurbsideOrder orderToPlace)
        {
            var order = Mapper.Map <OrderForCurbside>(orderToPlace);

            DataContext.CurbsideOrders.Add(order);
            await DataContext.SaveChangesAsync();

            var response = Mapper.Map <CurbsideOrder>(order);

            return(response);
        }
Beispiel #10
0
        public async Task <ActionResult> PlaceOrderSynchronously([FromBody] CreateCurbsideOrder orderToPlace)
        {
            var temp = await CurbsideMapper.PlaceOrder(orderToPlace);

            for (var t = 0; t < temp.Items.Count; t++)
            {
                Thread.Sleep(1000);
            }
            temp.Status = CurbsideOrderStatus.Processed;
            var order = await Context.SaveChangesAsync();

            return(Ok(temp)); // not going to map it... just want you to see.
        }
Beispiel #11
0
        public async Task <ActionResult> AsyncCurbsideOrders([FromBody] PostSyncCurbsideOrdersRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                // Save it to the DB
                var orderToSave = new CurbsideOrder
                {
                    For    = request.For,
                    Items  = request.Items,
                    Status = CurbsideOrderStatus.Processing
                };

                // return 201, Location Header, Copy of the Entity

                _context.CurbsideOrders.Add(orderToSave);

                await _context.SaveChangesAsync();

                var response = new GetCurbsideOrderResponse
                {
                    Id         = orderToSave.Id,
                    For        = orderToSave.For,
                    Items      = orderToSave.Items,
                    PickupDate = null,
                    Status     = orderToSave.Status
                };
                var didWrite = await _channel.AddCurbside(new CurbsideChannelRequest { ReservationId = response.Id });

                if (!didWrite)
                {
                    // what to do?
                }
                return(CreatedAtRoute("curbsideorders#getbyid", new { id = response.Id }, response));
            }
        }
        public async Task <CurbsideOrder> PlaceOrder(CreateCurbisdeOrder orderToPlace)
        {
            var order = _mapper.Map <OrderForCurbside>(orderToPlace);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            var response = _mapper.Map <CurbsideOrder>(order);

            //Process each of the items

            return(response);
        }
        public async Task <GetCurbsideDetailsResponse> PlaceOrderAsync(PostCurbsideRequest request, string wsClientId = null)
        {
            var orderToSave = _mapper.Map <CurbsideOrder>(request);

            _context.CurbsideOrders.Add(orderToSave);
            await _context.SaveChangesAsync();

            var response = _mapper.Map <GetCurbsideDetailsResponse>(orderToSave);
            var didWrite = await _channel.AddCurbsideAsync(
                new CurbsideChannelRequest
            {
                CurbsideOrderId = response.Id,
                WsConnectionId  = wsClientId
            }
                );

            if (!didWrite)
            {
                // let's talk about this after we see that it works.
            }
            return(response);
        }
Beispiel #14
0
        async Task <CurbsideOrder> IDoCurbsideCommands.AddOrderWs(PostCurbsideOrderRequest orderToBePlaced, string connectionId)
        {
            // place that order..
            // await Task.Delay(3000);
            // that represents all the very important things we have to do.
            var order = _mapper.Map <CurbsideOrder>(orderToBePlaced);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            try
            {
                await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id, ClientId = connectionId });
            }
            catch (OperationCanceledException ex)
            {
                // for some reason the background worker punched you in the nose (more likely the channel).
                // Do some cleanup.. Whatever.
                throw;
            }

            return(order);
        }
Beispiel #15
0
        public async Task <ActionResult> AddItem([FromBody] PostCatalogRequest newItem)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogInformation("Got a bad request. Looked like this {@newItem}", newItem);
                return(BadRequest(ModelState));
            }
            else
            {
                var item = _mapper.Map <ShoppingItem>(newItem);
                _context.ShoppingItems.Add(item);
                await _context.SaveChangesAsync();

                var response = _mapper.Map <GetCatalogResponseSummaryItem>(item);
                return(StatusCode(201, response));
            }
        }
Beispiel #16
0
        async Task <CurbsideOrder> IMapCurbsideOrders.PlaceOrder(CreateCurbsideOrder orderToPlace)
        {
            var order = Mapper.Map <OrderForCurbside>(orderToPlace);

            DataContext.CurbsideOrders.Add(order);
            await DataContext.SaveChangesAsync();

            var response = Mapper.Map <CurbsideOrder>(order);

            response.Items = order.Items.Split(",").ToList();
            // Process each of the items.
            foreach (var item in response.Items)
            {
                Thread.Sleep(1000);
            }
            return(response);
        }
Beispiel #17
0
        public async Task <ActionResult> AddItem([FromBody] PostCatalogRequest newItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                // what am I missing?
                var item = _mapper.Map <ShoppingItem>(newItem);
                _context.ShoppingItems.Add(item);
                await _context.SaveChangesAsync();

                var response = _mapper.Map <GetCatalogResponseSummaryItem>(item);
                return(StatusCode(201, response));
            }
        }
        //public async Task<CurbsideOrder> AddOrder(PostCurbsideOrderRequest orderToPlace)
        async Task <CurbsideOrder> IDoCurbsideCommands.AddOrder(PostCurbsideOrderRequest orderToPlace)
        {
            // place the order...
            //await Task.Delay(3000);
            var order = _mapper.Map <CurbsideOrder>(orderToPlace);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            try
            {
                await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id });
            } catch (OperationCanceledException ex)
            {
                throw;
            }
            return(order);
        }
        async Task <CurbsideOrder> IDoCurbsideCommands.AddOrder(PostCurbsideOrderRequest orderToPlace)
        {
            await Task.Delay(300);

            var order = _mapper.Map <CurbsideOrder>(orderToPlace);

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            try
            {
                await _channel.AddCurbside(new CurbsideChannelRequest { OrderId = order.Id });
            }catch (OperationCanceledException ex)
            {
                // for some reason the background worker punched you in the nose
                // do some cleanup
                throw;
            }
            return(order);
        }
Beispiel #20
0
        public virtual async Task <GetCurbsideResponse> PlaceOrder(PostCurbsideRequest request)
        {
            await Task.Delay(1000 *request.Items.Length);

            var order = new CurbsideOrder
            {
                For           = request.For,
                Items         = String.Join(",", request.Items),
                PickupReadyAt = DateTime.Now.AddHours(new Random().Next(2, 5))
            };

            _context.CurbsideOrders.Add(order);
            await _context.SaveChangesAsync();

            return(new GetCurbsideResponse
            {
                Id = order.Id,
                For = order.For,
                Items = order.Items.Split(',').Select(int.Parse).ToArray(),
                PickupReadyAt = order.PickupReadyAt.Value
            });
        }
        public async Task <ActionResult> SyncCurbsideOrders([FromBody] PostSyncCurbsideOrdersRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                var orderToSave = new CurbsireOrder
                {
                    WhoTheOrderIsFor = request.WhoTheOrderIsFor,
                    Items            = request.Items
                };


                var numberOfItems = orderToSave.Items.Split(',').Count();
                for (var t = 0; t < numberOfItems; t++)
                {
                    await Task.Delay(1000);
                }
                orderToSave.PickupDate = DateTime.Now.AddHours(numberOfItems);



                _context.CurbsireOrders.Add(orderToSave);

                await _context.SaveChangesAsync();

                var response = new GetCurbsideOrderResponse
                {
                    Id = orderToSave.Id,
                    WhoTheOrderIsForThisIsTheGETModelVariant = orderToSave.WhoTheOrderIsFor,
                    Items      = orderToSave.Items,
                    PickupDate = orderToSave.PickupDate.Value
                };
                return(Ok(response));
            }
        }
        public async Task PlaceOrder(PostSyncCurbsideOrdersRequest request)
        {
            var orderToSave = new CurbSideOrder
            {
                For    = request.For,
                Items  = request.Items,
                Status = CurbSideOrderStatus.Processing
            };

            _context.CurbSide.Add(orderToSave);
            await _context.SaveChangesAsync();

            var response = new GetCurbsideOrderResponse
            {
                Id         = orderToSave.Id,
                For        = orderToSave.For,
                Items      = orderToSave.Items,
                PickupDate = null,
                Status     = orderToSave.Status
            };
            await _channel.AddCurbside(new CurbsideChannelRequest { ReservationId = response.Id, ConnectionId = Context.ConnectionId });

            await Clients.Caller.SendAsync("OrderPlaced", response);
        }