Exemplo n.º 1
0
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     HttpContextBase httpContext = controllerContext.HttpContext;
     Customer customer = new Customer();
     customer.CustomerName = httpContext.Request["CustomerName"].ToString();
     customer.CustomerCode = httpContext.Request["CustomerCode"].ToString();
     return customer;
 }
Exemplo n.º 2
0
        public List<Customer> Put(Customer oCustomer)
        {
            CustomerDB db = new CustomerDB();
            if (ModelState.IsValid)
            {
                Customer customer = (from x in db.Customers
                             where x.CustomerCode == oCustomer.CustomerCode
                             select x).Single<Customer>();

                customer.CustomerAmount = oCustomer.CustomerAmount;
                customer.CustomerName = oCustomer.CustomerName;

                List<Order> customerOrders = (from x in db.Orders
                                              where x.CustomerCode == oCustomer.CustomerCode
                                              select x).ToList<Order>();

                foreach (Order order in oCustomer.CustomerOrders)
                {
                    //check if order exists than update else insert
                    Order currentOrder = customerOrders.Find(i => i.OrderId == order.OrderId);
                    if (currentOrder != null)
                    {
                        //update
                        currentOrder.OrderNumber = order.OrderNumber;
                        currentOrder.Amount = order.Amount;
                    }
                    else
                    {
                        //insert
                        db.Orders.Add(new Order {
                            OrderNumber = order.OrderNumber,
                            CustomerCode = order.CustomerCode,
                            Amount = order.Amount
                        });
                    }
                }

                db.SaveChanges();
            }

            return db.Customers.ToList<Customer>();
        }
Exemplo n.º 3
0
        public List<Customer> Post(Customer oCustomer)
        {
            CustomerDB db = new CustomerDB();
            if (ModelState.IsValid)
            {
                db.Customers.Add(oCustomer);
                foreach (Order order in oCustomer.CustomerOrders)
                {
                    db.Orders.Add(new Order
                    {
                        OrderNumber = order.OrderNumber,
                        CustomerCode = order.CustomerCode,
                        Amount = order.Amount
                    });
                }

                db.SaveChanges();
            }

            return db.Customers.ToList<Customer>();
        }