Пример #1
0
        public IActionResult AddBrand(AdminBrandViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel.Img != null)
                {
                    if (Path.GetExtension(viewModel.Img.FileName) != ".jpg")
                    {
                        ModelState.AddModelError("Img", "فایل با پسوند jpg بارگزاری شود");
                    }
                    else
                    {
                        string filePath = "";
                        viewModel.ImgName = CodeGenerators.FileCode() + Path.GetExtension(viewModel.Img.FileName);
                        filePath          = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/brands/", viewModel.ImgName);

                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            viewModel.Img.CopyTo(stream);
                        }

                        Brand brand = new Brand()
                        {
                            Img     = viewModel.ImgName,
                            Name    = viewModel.Name,
                            NotShow = viewModel.NotShow,
                            StoreId = null
                        };

                        _admin.AddBrand(brand);

                        return(RedirectToAction(nameof(ShowBrands)));
                    }
                }
                else
                {
                    Brand brand = new Brand()
                    {
                        Img     = null,
                        Name    = viewModel.Name,
                        NotShow = viewModel.NotShow,
                        StoreId = null
                    };

                    _admin.AddBrand(brand);

                    return(RedirectToAction(nameof(ShowBrands)));
                }
            }

            return(View(viewModel));
        }