Example #1
0
        }//end update

        //CREATE
        //This should take CUSTOMER ID, SHIPPING DETAILS, ARRAY OF LINE ITEMS & CREATE ORDER
        public OrderDetails CreateOrder(NewOrderDetails orderdetails)
        {
            LineItemManager linemgr = new LineItemManager();

            using (var context = DbContextFactory.Create())   //no 'NEW' b/c of static class
            {
                var cust = context.Customer.Include(a => a.Addresses).FirstOrDefault(c => c.Id == orderdetails.CustomerId);

                var shipmethod = context.ShippingMethod.FirstOrDefault(s => s.Id == orderdetails.ShippingMethodId);

                var status = context.OrderDetails.FirstOrDefault(s => Convert.ToInt32(s.OrderStatus) == orderdetails.Status);

                var product = context.Product;

                var lines = new List<LineItem>();

                foreach(NewLine l in orderdetails.LineItems)
                {
                    var prod = product.FirstOrDefault(t => t.Id == l.ProductId);

                    LineItem newLine = new LineItem
                    {
                        Id = 0,
                        ProductDetails = prod,
                        Quantity = l.Quantity,
                        OrderDetails = null
                    };

                    lines.Add(newLine);
                }

                OrderDetails newOrder = new OrderDetails {
                    CustomerDetails = cust,
                    Shipmethod = shipmethod,
                    Lineitems = lines,
                    OrderStatus = status.OrderStatus,
                    OrderDate = orderdetails.OrderDate
                };

                try
                {
                    //Add order
                    context.OrderDetails.Add(newOrder);
                    context.SaveChanges();

                    Console.WriteLine($"Order {newOrder.Id} added....");

                    return newOrder;

                }
                catch (Exception)
                {
                    Console.WriteLine("Unable to add order....");
                    return null;
                }
            }
        }
Example #2
0
        public IActionResult CreateOrder([FromBody] NewOrderDetails orderdetails)
        {
            //CHECK IF NULL - RETURN BAD REQUEST
            if (orderdetails == null)
            {
                return(BadRequest());
            }

            var retrieval = new OrderManager().CreateOrder(orderdetails);

            if (retrieval != null)
            {
                return(new ObjectResult(retrieval));
            }

            else
            {
                Console.WriteLine("Unable to create order...");
                return(BadRequest());
            }
        }