Ejemplo n.º 1
0
        // GET: /Home/
        public ActionResult Index()
        {
            var proxy     = new ServiceProxyB2CClient();
            var productos = new PromocionesModel {
                Promociones = proxy.ConsultarCampaniaProducto()
            };

            return(View(productos));
        }
 // GET: Product
 public ActionResult Index(string searchString)
 {
     if (string.IsNullOrEmpty(searchString))
     {
         var proxy     = new ServiceProxyB2CClient();
         var productos = new ProductosModel {
             Productos = proxy.ConsultarCampaniaProducto()
         };
         return(View(productos));
     }
     else
     {
         var proxy     = new ServiceProxyB2CClient();
         var productos = new ProductosModel {
             Productos = proxy.ConsultarProducto(TipoConsultaProducto.DESCRIPCION, null, searchString, null)
         };
         return(View(productos));
     }
 }
        public ActionResult AddItem(int idProducto, int cantidad)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index"));
            }

            //consulto el usuario logueado
            var currentUser = System.Web.HttpContext.Current.User as CustomPrincipal;

            if (currentUser == null)
            {
                return(RedirectToAction("Index", "Account"));
            }

            var clientConfiguration = new MemcachedClientConfiguration {
                Protocol = MemcachedProtocol.Binary
            };

            clientConfiguration.Servers.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 32769));

            using (var client = new MemcachedClient(clientConfiguration))
            {
                //consulto el cache del usuario logueado
                var cart = client.Get <Cart>("Cart-" + currentUser.UserName);

                var proxy = new ServiceProxyB2CClient();
                //var producto = proxy.ConsultarProducto(TipoConsultaProducto.ID, idProducto.ToString(), null, null).First();

                //No se porque el servicio de productos no devuelve datos.
                var producto = proxy.ConsultarCampaniaProducto().First();

                if (null == cart)
                {
                    //si el carrito es vacio cree uno nuevo
                    cart = new Cart
                    {
                        UserId = (int)currentUser.CustId,
                        Items  = new List <Item>()
                    };

                    var item = new Item
                    {
                        Producto = producto,
                        Cantidad = cantidad
                    };
                    cart.Items.Add(item);

                    client.Store(StoreMode.Set, "Cart-" + currentUser.UserName, cart);
                }
                else
                {
                    foreach (var i in cart.Items.Where(i => i.Producto.id_producto == idProducto))
                    {
                        //Si existe un carrito busco el item y adiciono la cantidad
                        i.Cantidad = i.Cantidad + cantidad;
                        client.Store(StoreMode.Set, "Cart-" + currentUser.UserName, cart);
                        return(View("cart", cart));
                    }

                    //si no existe el item en el carrito lo agrego a la coleccion y guardo el carro
                    var item = new Item
                    {
                        Producto = producto,
                        Cantidad = cantidad
                    };
                    cart.Items.Add(item);
                    client.Store(StoreMode.Set, "Cart-" + currentUser.UserName, cart);
                }
                return(View("Cart", cart));
            }
        }