Пример #1
0
        public async Task <IActionResult> Product(ProductView product, IFormFile?formFile)
        {
            ViewBag.Brands   = new List <Brand>();
            ViewBag.Sections = new List <Section>();

            if (ModelState.IsValid)
            {
                if (product.Id != -1)
                {
                    var productDB = _sqlProductData.GetProductById(product.Id);
                    productDB.Name      = product.Name;
                    productDB.Order     = product.Order;
                    productDB.Price     = product.Price;
                    productDB.ImageUrl  = formFile?.FileName ?? product.ImageUrl;
                    productDB.BrandId   = product.BrandId;
                    productDB.SectionId = product.SectionId;
                }
                else
                {
                    var productNew = new Product()
                    {
                        Name      = product.Name,
                        Order     = product.Order,
                        Price     = product.Price,
                        ImageUrl  = formFile?.FileName ?? product.ImageUrl,
                        BrandId   = product.BrandId,
                        SectionId = product.SectionId
                    };
                    _sqlProductData.CreateProduct(productNew);
                }

                if (formFile != null)
                {
                    var path = "/images/shop/" + formFile.FileName;

                    using (var fs = new FileStream(_environment.WebRootPath + path, FileMode.Create))
                    {
                        await formFile.CopyToAsync(fs);
                    }
                }
                _sqlProductData.SaveDB();
                return(RedirectToAction("Products", "ShopElemet", new { area = "Admin" }));
            }

            var brands = _sqlProductData.GetBrands();

            ViewBag.Brands = new SelectList(brands, "Id", "Name");

            var sections = _sqlProductData.GetSections();

            ViewBag.Sections = new SelectList(sections, "Id", "Name");

            return(View(product));
        }
Пример #2
0
        public IActionResult Edit(ProductViewModel model)
        {
            var notParentSections = _productData.GetSections().Where(s =>
                                                                     s.ParentId != null);

            var brands = _productData.GetBrands();

            var section = _productData.GetSectionById(model.SectionId);

            BrandDto brand = null;

            if (model.BrandId.HasValue)
            {
                brand = _productData.GetBrandById(model.BrandId.Value);
            }

            if (ModelState.IsValid)
            {
                var productDto = new ProductDto()
                {
                    Id       = model.Id,
                    ImageUrl = model.ImageUrl,

                    Name    = model.Name,
                    Order   = model.Order,
                    Price   = model.Price,
                    Brand   = brand,
                    Section = section
                };
                if (model.Id > 0)
                {
                    _productData.UpdateProduct(productDto);
                }
                else
                {
                    _productData.CreateProduct(productDto);
                }
                return(RedirectToAction(nameof(ProductList)));
            }
            model.Brands   = new SelectList(brands, "Id", "Name", model.BrandId);
            model.Sections = new SelectList(notParentSections, "Id", "Name",
                                            model.SectionId);
            return(View(model));
        }
Пример #3
0
        public ActionResult Create(ProductModel productModel, HttpPostedFileBase fileUpload)

        {
            Stream stream          = null;
            var    fileName        = "";
            var    fullPictureName = "";

            string pictureNamePrefix = Guid.NewGuid().ToString();

            fileName                   = Path.GetFileName(fileUpload.FileName);
            fullPictureName            = pictureNamePrefix + "_" + fileName;
            productModel.ImageLocation = fullPictureName;

            stream = fileUpload.InputStream;
            var fileBinary = new byte[stream.Length];

            stream.Read(fileBinary, 0, fileBinary.Length);

            ProductData.CreateProduct(productModel);
            PictureHandler.SavePictureInFile(fileBinary, fullPictureName);

            return(View());
        }
Пример #4
0
        public SaveResult CreateProduct([FromBody] ProductDto productDto)
        {
            var result = _productData.CreateProduct(productDto);

            return(result);
        }
Пример #5
0
        public async Task <ActionResult> Create(ProductModel productToCreate)
        {
            await _productData.CreateProduct(productToCreate);

            return(View());
        }
Пример #6
0
 public void CreateProduct(Product product, decimal productPrice)
 {
     _productdata.CreateProduct(product, productPrice);
 }