Ejemplo n.º 1
0
        public ActionResult Index(int id)
        {
            int currentProductId = id;
            var product = _dataManager.Products.GetProductById(currentProductId);
            var productsCustomers = _dataManager.ProductsCustomers.GetProductsCustomersByProductId(currentProductId);

            if (product == null || productsCustomers == null)
                return RedirectToAction("Pity", "Error");

            if (productsCustomers.CustomerId != (int) Session["UserId"])
                return RedirectToAction("Index", "Purchase", new { id = currentProductId });

            Session["CurrentProductId"] = currentProductId;
            _model = new CreateProduct
            {
                Name = product.Name,
                Cost = product.Cost,
                Image = product.Image,
                Description = product.Description,
                IsAvailable = product.IsAvailable,
                Count = productsCustomers.Count
            };

            return View(_model);
        }
Ejemplo n.º 2
0
 private void ReadModel(CreateProduct model, IProduct item)
 {
     item.Name = model.Name;
     item.Image = model.Image;
     item.Description = model.Description;
     item.Cost = model.Cost;
     item.IsAvailable = model.IsAvailable;
 }
Ejemplo n.º 3
0
 private void FillModel(IProduct product, IProductsCustomers productsCustomers, out CreateProduct model)
 {
     model = new CreateProduct
         {
             Name = product.Name,
             Image = product.Image,
             Cost = product.Cost,
             Description = product.Description,
             IsAvailable = product.IsAvailable,
             IsMine = productsCustomers.CustomerId == (int) Session["UserId"]
         };
     Session["CreateProductModel"] = model;
 }
Ejemplo n.º 4
0
        public ActionResult Index(CreateProduct model, HttpPostedFileBase uploadImage)
        {
            if (uploadImage != null)
                ReadImage(uploadImage, model);

            if (ModelState.IsValid && uploadImage != null)
            {
                IProduct item = new Product();
                ReadModel(model, item);

                return Redirect(Auxiliary.Actions.Create, CreateProduct(model, item)
                        ? Auxiliary.Result.OperationSuccess : Auxiliary.Result.Error);
            }
            return View(model);
        }
Ejemplo n.º 5
0
 private bool CreateProduct(CreateProduct model, IProduct item)
 {
     try
     {
         var currentProductId = _dataManager.Products.AddProduct(item);
         if (currentProductId == -1) return false;
         if (_dataManager.ProductsCustomers.AddProdCustRelation(
             (int)Session["UserId"], currentProductId, model.Count))
             return true;
         _dataManager.Products.DeleteProduct(item.Id);
         return false;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 6
0
        public ActionResult Index(CreateProduct model)
        {
            if (Session["UserId"] != null && (int) Session["UserId"] == -1)
                return RedirectToAction("LogIn", "Account");

            if (model.Count <= 0)
            {
                model = Session["CreateProductModel"] as CreateProduct;
                return View(model);
            }

            var product = _dataManager.Products.GetProductById((int)Session["CurrentProductId"]);
            if (product == null)
                return RedirectToAction("Pity", "Error");

            AddToCart(model, product);

            ViewBag.IsAddToCart = true;
            return View(Session["CreateProductModel"]);
        }
Ejemplo n.º 7
0
        public ActionResult Index(CreateProduct model, FormCollection form, HttpPostedFileBase uploadImage)
        {
            if (form.GetKey(form.Keys.Count - 1) == "delete") // if input 'delete' stands always in the end
            {
                if (_dataManager.Products != null)
                    return Redirect(Auxiliary.Actions.Delete, DeleteProduct(model)
                        ? Auxiliary.Result.OperationSuccess : Auxiliary.Result.Error);
            }
            else
            {
                if (!ModelState.IsValid)
                    return View(model);
                if (_dataManager.Products != null)
                {
                    var oldProduct = _dataManager.Products.GetProductById(model.Id);
                    if (oldProduct == null)
                        return RedirectToAction("Pity", "Error");

                    var item = new Product();
                    ReadModel(model, item);
                    item.Id = (int) Session["CurrentProductId"];

                    if (uploadImage != null)
                        ReadImage(uploadImage, item);
                    else
                        item.Image = oldProduct.Image;

                    if (_dataManager.Products != null)
                        if (UpdateProduct(item, model))
                            return Redirect(Auxiliary.Actions.Delete,
                                _dataManager.ProductsCustomers.UpdateProdCastRelation(item.Id, model.Count)
                                ? Auxiliary.Result.OperationSuccess : Auxiliary.Result.Error);
                }
            }
            return View(model);
        }
Ejemplo n.º 8
0
 private bool UpdateProduct(IProduct item, CreateProduct model)
 {
     try
     {
         var currentProductId = _dataManager.Products.UpdateProduct(item);
         if (currentProductId == -1)
             return false;
         if (_dataManager.ProductsCustomers.UpdateProdCastRelation(currentProductId, model.Count))
             return true;
         return false;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 9
0
 private bool DeleteProduct(CreateProduct model)
 {
     var orders = _dataManager.Orders.GetOrderById(model.Id);
     return orders != null ? _dataManager.ProductsCustomers.DeleteProdCustRelation(model.Id)
                           : _dataManager.Products.DeleteProduct(model.Id);
 }
Ejemplo n.º 10
0
 private void ReadImage(HttpPostedFileBase uploadImage, CreateProduct model)
 {
     Auxiliary.ReadImage(uploadImage, model);
 }
Ejemplo n.º 11
0
 private void AddToCart(CreateProduct model, IProduct product)
 {
     GetCart().AddItem(product, model.Count);
     Session["Cart"] = GetCart();
 }