Beispiel #1
0
        public ActionResult OrderCompletion(int addressId, Order Order, int productId)
        {
            string id      = User.Identity.GetUserId();
            var    address = db.Addresses.Single(a => a.AddressId == addressId);
            var    Product = db.Products.Single(P => P.ProductId == productId);

            var bvm = new BuyViewModel
            {
                Product      = Product,
                address      = address,
                AddressBook  = db.Addresses.ToList(),
                States       = db.States.ToList(),
                PaymentModes = db.Payments.ToList(),
                Order        = Order
            };

            if (!ModelState.IsValid)
            {
                return(View("CheckOut", bvm));
            }

            var user    = db.AspNetUsers.Single(a => a.Id == id);
            var payment = db.Payments.Single(p => p.PaymentId == Order.PaymentId);

            Order.CreatedDate     = DateTime.Now;
            Order.PaymentId       = payment.PaymentId;
            Order.Id              = user.Id;
            Order.Subtotal        = Order.ItemQuantity * Product.Price;
            Order.ShippingAddress = address.HouseNo + "\n" + address.Colony_Street + "\n" +
                                    address.City + "\n " + address.State.Name + "\n " + address.Pincode;
            //   var  address = add;


            if (!ModelState.IsValid)
            {
                return(View("CheckOut", bvm));
            }
            else
            {
                if (Product.NumberOfStock == 0)
                {
                    ModelState.AddModelError("Order.ItemQuantity", "Out Of Stock");
                    return(View("CheckOut", bvm));
                }

                if (Product.NumberOfStock < Order.ItemQuantity)
                {
                    ModelState.AddModelError("Order.ItemQuantity", "Only " + Product.NumberOfStock + " stock is left");

                    return(View("CheckOut", bvm));
                }
            }

            db.Orders.Add(Order);

            var existingcustomer = db.Customers.SingleOrDefault(c => c.Id == user.Id);

            if (existingcustomer == null)
            {
                Customer customer = new Customer()
                {
                    Name        = user.UserName,
                    PhoneNumber = user.PhoneNumber,
                    CreatedDate = DateTime.Now,
                    CreatedBy   = user.Id,
                    Id          = user.Id
                };

                db.Customers.Add(customer);
            }


            db.SaveChanges();


            db.OrderDetails.Add(new OrderDetail
            {
                ProductId  = Product.ProductId,
                OrderId    = Order.OrderId,
                Quantity   = Order.ItemQuantity,
                TotalPrice = Product.Price * Order.ItemQuantity
            });

            Product.NumberOfStock = Product.NumberOfStock - Order.ItemQuantity;

            db.SaveChanges();

            var email = new CartController();

            email.SendEmail(Order);

            return(View(Order));
        }
Beispiel #2
0
        public ActionResult OrderCompletion(int addressId, Order Order, int productId)
        {
            string     id = User.Identity.GetUserId();
            Address    address;
            AspNetUser user;
            Product    Product;
            Payment    payment;


            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var responsetask = client.GetAsync("Addressapi?addressid=" + addressId.ToString());
                responsetask.Wait();
                var result = responsetask.Result;
                var read   = result.Content.ReadAsAsync <Address>();
                read.Wait();
                address = read.Result;
            }
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var responsetask = client.GetAsync("Productapi?Id=" + productId.ToString());
                responsetask.Wait();
                var result = responsetask.Result;
                var read   = result.Content.ReadAsAsync <Product>();
                read.Wait();
                Product = read.Result;
            }
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var responsetask = client.GetAsync("NormalUserapi?id=" + id);
                responsetask.Wait();
                var result = responsetask.Result;
                var read   = result.Content.ReadAsAsync <AspNetUser>();
                read.Wait();
                user = read.Result;
            }
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var responsetask = client.GetAsync("Paymentapi?paymentid=" + Order.PaymentId);
                responsetask.Wait();
                var result = responsetask.Result;
                var read   = result.Content.ReadAsAsync <Payment>();
                read.Wait();
                payment = read.Result;
            }



            Order.CreatedDate = DateTime.Now;
            // Order.PaymentId = payment.PaymentId;
            Order.Id = user.Id;
            // Order.Subtotal = Order.ItemQuantity * Product.Price;
            Order.Subtotal        = CalculateSubtotal(Order.ItemQuantity, Product.Price);
            Order.ShippingAddress = address.HouseNo + "\n" + address.Colony_Street + "\n" +
                                    address.City + "\n " + address.State.Name + "\n " + address.Pincode;


            //   var  address = add;

            BuyViewModel bvm;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var response = client.GetAsync("Productapi?productidtobeby=" + Product.ProductId.ToString() + "&userid=" + id + "&addressid=" + addressId.ToString());
                response.Wait();
                var result = response.Result;
                var read   = result.Content.ReadAsAsync <BuyViewModel>();
                read.Wait();
                bvm = read.Result;
            }

            //    bvm.address = address;
            bvm.Order = Order;
            //var bvm = new BuyViewModel
            //{
            //    Product = Product,
            //    address = address,
            //    AddressBook = db.Addresses.ToList(),
            //    States = db.States.ToList(),
            //    PaymentModes = db.Payments.ToList(),
            //    Order = Order

            //};
            if (!ModelState.IsValid)
            {
                return(View("CheckOut", bvm));
            }
            else
            {
                if (Product.NumberOfStock == 0)
                {
                    ModelState.AddModelError("Order.ItemQuantity", "Out Of Stock");
                    return(View("CheckOut", bvm));
                }

                if (Product.NumberOfStock < Order.ItemQuantity)
                {
                    ModelState.AddModelError("Order.ItemQuantity", "Only " + Product.NumberOfStock + " stock is left");

                    return(View("CheckOut", bvm));
                }
            }

            // db.Orders.Add(Order);

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var response = client.PostAsJsonAsync <Order>("Orderapi/PostOrder", Order);
                response.Wait();
                var result = response.Result;
            }

            Customer existingcustomer = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var response = client.GetAsync("Customerapi?id=" + user.Id);
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var read = result.Content.ReadAsAsync <Customer>();
                    read.Wait();
                    existingcustomer = read.Result;
                }
            }

            // var existingcustomer = db.Customers.SingleOrDefault(c => c.Id == user.Id);
            if (existingcustomer == null)
            {
                Customer customer = new Customer()
                {
                    Name        = user.UserName,
                    PhoneNumber = user.PhoneNumber,
                    CreatedDate = DateTime.Now,
                    CreatedBy   = user.Id,
                    Id          = user.Id
                };

                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:55437/api/Customerapi");
                    var response = client.PostAsJsonAsync <Customer>("Customerapi", customer);
                    response.Wait();

                    var result = response.Result;
                }
                // db.Customers.Add(customer);
            }


            // db.SaveChanges();
            //using(var client = new HttpClient())
            //  {
            //     client.BaseAddress = new Uri("http://localhost:55437/api/");
            //     var response = client.GetAsync("Productapi");
            //     response.Wait();
            //     var result = response.Result;


            // }

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var response = client.GetAsync("Orderapi");
                response.Wait();
                var result = response.Result;
                var read   = result.Content.ReadAsAsync <IList <Order> >();
                read.Wait();
                IEnumerable <Order> orders = read.Result;
                Order = orders.Where(o => o.Id == id).OrderBy(o => o.OrderId).Last();
            }
            OrderDetail orderdetail = new OrderDetail
            {
                ProductId  = Product.ProductId,
                OrderId    = Order.OrderId,
                Quantity   = Order.ItemQuantity,
                TotalPrice = Product.Price * Order.ItemQuantity
            };

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55437/api/");
                var response = client.PostAsJsonAsync <OrderDetail>("Orderapi/PostOrderDetails", orderdetail);
                response.Wait();

                var result = response.Result;
            }

            //  Product.NumberOfStock = Product.NumberOfStock - Order.ItemQuantity;

            DecreaseNumberOfStock(Product, Order.ItemQuantity);

            //using (var client = new HttpClient())
            //{
            //    client.BaseAddress = new Uri("http://localhost:55437/api/");
            //    var response = client.GetAsync("Productapi");
            //    response.Wait();
            //    var result = response.Result;


            //}
            Order.Payment    = payment;
            Order.AspNetUser = user;

            var email = new CartController();

            email.SendEmail(Order);

            return(View(Order));
        }