Exemplo n.º 1
0
        public ActionResult AddProductForm(ProductUploadModel obj)
        {
            //! Lets make a connection to products inside DB
            ProductDAL proDAL = new ProductDAL();

            /*
             * Lets test if the current SKU is uniqe;
             * if not we would like to tell the admin
             * to change the SKU
             * */
            var uniqeSKU = proDAL.Products.FirstOrDefault(item => item.SKU == obj.pr.SKU);

            if (uniqeSKU != null)
            {
                return(View("ProductSKUError"));
            }

            /*
             * Files uploading is-in the block below
             * */
            if (obj.pic != null && obj.pic.ContentLength > 0)
            {
                var path  = "~/PicData/";
                var fname = "pic_" + obj.pr.Id + "_" + obj.pic.FileName;
                obj.pic.SaveAs(
                    Path.Combine(
                        Server.MapPath(path), fname));

                //! Setting picURL to be used farther
                obj.pr.PicURL = fname;
            }

            try
            {
                //! Lets store the product inside the Products DB instance.
                proDAL.Products.Add(obj.pr);
                proDAL.SaveChanges();

                //! Case all fine, open user message.
                return(View("ProductSuccessfulAdded"));
            }
            catch (Exception)
            {
                //! Case not:
                return(View("ProductProccessError"));
            }
        }
Exemplo n.º 2
0
        public virtual ActionResult Dodaj(ProductUploadModel model)
        {
            if (ModelState.IsValid)
            {
                var category = model.Category == 0 ? null : model.Category;
                var price    = model.Price == null
                    ? (decimal?)null
                    : Math.Floor((Convert.ToDecimal(model.Price.Replace(".", ","))) * 100) / 100;

                var weight = model.Weight == null
                    ? (decimal?)null
                    : Math.Floor((Convert.ToDecimal(model.Weight.Replace(".", ","))) * 100) / 100;


                if ((price == null || category == null || model.Packing == null || weight == null) && model.PublishAfterCreate)
                {
                    ModelState.AddModelError("PublishAfterCreate",
                                             "Cena, kategoria, waga oraz sposób pakowania muszą być podane przed publikacją!");
                    GetDropDownLists(model.Category);
                    return(View(model));
                }

                if (model.SubCategory != 0)
                {
                    category = model.SubCategory;
                }

                var product = new Products
                {
                    Quantity         = model.Quantity,
                    CategoryId       = category,
                    Name             = model.Title,
                    DateAdded        = DateTime.Now,
                    Description      = model.Description,
                    Published        = model.PublishAfterCreate,
                    Packing          = model.Packing,
                    Promotion        = model.Promotion,
                    Price            = price,
                    ReservedQuantity = 0,
                    Weight           = weight
                };

                _dbContext.Products.AddOrUpdate(product);
                _dbContext.SaveChanges();

                var prod = _appRepository.GetAll <Products>().OrderByDescending(x => x.ProductId).First();
                if (model.ImageBig != null && model.ImageBig.ContentLength != 0)
                {
                    var pathForSaving = Server.MapPath("~/Content/Images/Shop/");
                    var imageBig      = new ImageJob(model.ImageBig, pathForSaving + prod.ProductId + "_normal",
                                                     new Instructions("maxwidth=700&maxheight=700&format=jpg"))
                    {
                        CreateParentDirectory = true,
                        AddFileExtension      = true
                    };
                    imageBig.Build();
                    var imageSmall = new ImageJob(model.ImageBig, pathForSaving + prod.ProductId + "_small",
                                                  new Instructions("maxwidth=127&maxheight=127&format=jpg"))
                    {
                        CreateParentDirectory = true,
                        AddFileExtension      = true
                    };
                    imageSmall.Build();
                    prod.IconName  = prod.ProductId + "_small";
                    prod.ImageName = prod.ProductId + "_normal";
                }

                prod.IsComplete = true;
                foreach (var prop in product.GetType().GetProperties().Where(prop => prop.GetValue(prod, null) == null))
                {
                    prod.IsComplete = false;
                }
                if (prod.Price == null || prod.CategoryId == null || prod.Packing == null)
                {
                    prod.WrongModel = true;
                    prod.IsComplete = false;
                }
                _dbContext.Products.AddOrUpdate(prod);
                _dbContext.SaveChanges();
                return(RedirectToAction(MVC.Admin.Produkty.Lista(prod.Name)));
            }
            ViewBag.SelectedOpt = 1;
            GetDropDownLists(model.Category);
            return(View(model));
        }