public async Task <IActionResult> PutOrder(int id, Order order)
        {
            if (id != order.Id)
            {
                return(BadRequest());
            }

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

            if (order.IsCompleted)
            {
                await SendBookOrderMsg(order);
            }

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

            return(NoContent());
        }
        public async Task <ActionResult <Order> > PostOrder(Order order)
        {
            order.CreationDateTime = DateTime.Now;
            order.OrderStatus      = OrderStatus.Created;
            _context.Orders.Add(order);

            await _context.SaveChangesAsync();

            foreach (var item in order.Items)
            {
                item.Order = null;
            }

            return(CreatedAtAction(nameof(PostOrder), new { id = order.OrderId }, order));
        }
Example #3
0
        public async Task AddOrder(PaymentVM pvm)
        {
            using (OrderServiceContext ctx = new OrderServiceContext())
            {
                Orders order = new Orders()
                {
                    OrderAdress = pvm.DeliveryAdress,
                    OrderDate   = DateTime.Now,
                    FirstName   = pvm.FirstName,
                    LastName    = pvm.LastName,
                    Email       = pvm.Email
                };

                ctx.Orders.Add(order);
                ctx.SaveChanges();

                foreach (Product p in pvm.Cart.Products)
                {
                    ctx.OrderLine.Add(new OrderLine()
                    {
                        ProductId = p.Id,
                        OrderId   = order.Id,
                        Total     = p.Price
                    });
                }

                System.Diagnostics.Debug.WriteLine("ORDER ADDED: " + order.ToString());
                await ctx.SaveChangesAsync();
            }
        }