public IActionResult Create([FromBody] ProductAddVM model)
        {
            if (!ModelState.IsValid)
            {
                var errors = CustomValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errors));
            }
            var    fileDestDir = _env.ContentRootPath;
            string dirName     = _configuration.GetValue <string>("ImagesPath");
            //Папка де зберігаються фотки
            string dirPathSave = Path.Combine(fileDestDir, dirName);

            if (!Directory.Exists(dirPathSave))
            {
                Directory.CreateDirectory(dirPathSave);
            }
            var    bmp       = model.Image.FromBase64StringToImage();
            var    imageName = Path.GetRandomFileName() + ".jpg";
            string fileSave  = Path.Combine(dirPathSave, $"{imageName}");

            bmp.Save(fileSave, ImageFormat.Jpeg);

            DbProduct p = new DbProduct
            {
                Name       = model.Name,
                Price      = model.Price,
                DateCreate = DateTime.Now
            };

            _context.Products.Add(p);
            _context.SaveChanges();
            return(Ok(p.Id));
        }
Example #2
0
        public ActionResult Insert()
        {
            var model = new ProductAddVM
            {
                Product    = new Product(),
                Categories = _categoryService.GetAll()
            };

            return(View(model));
        }
Example #3
0
        public IActionResult AddProduct(ProductAddVM product)
        {
            if (!ModelState.IsValid)
            {
                return(View(product));
            }
            string thmbnailPath  = env.WebRootPath + @"\lib\images\products\thumbnail";
            string saveImagePath = env.WebRootPath + @"\lib\images\products";

            string       imageName1   = string.Empty.GetNewName();
            string       imageName2   = string.Empty.GetNewName();
            string       imageName3   = string.Empty.GetNewName();
            string       imageName4   = string.Empty.GetNewName();
            ProductImage productImage = new ProductImage
            {
                Thumbnail = imageName1,
                Image_1   = imageName1,
                Image_2   = imageName2,
                Image_3   = imageName3,
                Image_4   = imageName4
            };

            var save_Thubnailimage_Result = fileService.SaveImageFile(product.Image1, 229, 229, thmbnailPath, imageName1);
            var save_image1_Result        = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName1);
            var save_image2_Result        = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName2);
            var save_image3_Result        = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName3);
            var save_image4_Result        = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName3);

            var addResult = PService.AddProduct(new Store.Models.DataBase.Entities.Product
            {
                BrandId          = product.BrandId,
                EName            = product.EName,
                ExistCount       = product.ExistCount,
                Features         = product.Features,
                Images_Json      = productImage.ToJsonImage(),
                InsertedDate     = DateTime.Now,
                IsActive         = product.IsActive,
                IsDeleted        = false,
                IsSpecial        = product.IsSpecial,
                MainDescription  = product.MainDescription,
                Name             = product.Name,
                SalesPrice       = product.SalesPrice,
                ShortDescription = product.ShortDescription,
                Tags             = product.Tags,
                SubCategoryId    = product.SubCategoryId,
                UnitType         = product.UnitType,
                WrittenPrice     = product.WrittenPrice.HasValue? product.WrittenPrice.Value:0
            });

            if (addResult)
            {
                return(View("Index"));
            }
            return(View(product));
        }
Example #4
0
        public ActionResult Create()
        {
            ProductAddVM model      = new ProductAddVM();
            var          categories = _categoryRepository.GetAll();
            var          mapper     = MyAutoMapperConfig.GetAutoMapper();
            // сопоставление
            var select = mapper.Map <List <SelectItemVM> >(categories);

            model.SetCategoriesSelect(select);

            return(View(model));
        }
Example #5
0
        public ActionResult Edit(int id, ProductAddVM model)
        {
            Product product = _productRepository.Get(id);

            product.Name       = model.Name;
            product.Price      = model.Price;
            product.Quantity   = model.Quantity;
            product.CategoryId = model.CategoryId;
            product.ModifyDate = DateTime.Now;

            _productRepository.Save();

            return(RedirectToAction("Index"));
        }
Example #6
0
 public ActionResult Create(ProductAddVM model)
 {
     if (ModelState.IsValid)
     {
         Product animal = new Product()
         {
             Name       = model.Name,
             Price      = model.Price,
             Quantity   = model.Quantity,
             CategoryId = model.CategoryId,
             CreateDate = DateTime.Now,
             ModifyDate = DateTime.Now,
             DeleteDate = DateTime.Now
         };
         _productRepository.Create(animal);
         _productRepository.Save();
         //context.Animals.Add(animal);
         //context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(model));
 }
Example #7
0
        public IActionResult AddProduct(ProductAddVM product)
        {
            if (!product.Image1.IsImageValid())
            {
                ModelState.AddModelError("Image1", "فایل تصویر نامعتبر است.");
            }

            if (!product.Image2.IsImageValid() && product.Image2 != null)
            {
                ModelState.AddModelError("Image2", "فایل تصویر نامعتبر است.");
            }

            if (!product.Image3.IsImageValid() && product.Image3 != null)
            {
                ModelState.AddModelError("Image3", "فایل تصویر نامعتبر است.");
            }

            if (!product.Image4.IsImageValid() && product.Image4 != null)
            {
                ModelState.AddModelError("Image4", "فایل تصویر نامعتبر است.");
            }

            if (!ModelState.IsValid)
            {
                return(View(product));
            }

            string thmbnailPath  = env.WebRootPath + @"\lib\images\products\thumbnail";
            string saveImagePath = env.WebRootPath + @"\lib\images\products";

            string       imageName1   = string.Empty.GetNewName() + ".jpg";
            string       imageName2   = product.Image2.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg";
            string       imageName3   = product.Image3.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg";
            string       imageName4   = product.Image4.CheckIsNull()?string.Empty: string.Empty.GetNewName() + ".jpg";
            ProductImage productImage = new ProductImage
            {
                Thumbnail = imageName1,
                Image_1   = imageName1,
                Image_2   = imageName2,
                Image_3   = imageName3,
                Image_4   = imageName4
            };

            if (product.Image1 != null && product.Image1.IsImageValid())
            {
                var save_Thubnailimage_Result = fileService.SaveImageFile(product.Image1, 229, 229, thmbnailPath, imageName1);
                var save_image1_Result        = fileService.SaveImageFile(product.Image1, 700, 700, saveImagePath, imageName1);
            }
            if (product.Image2 != null && product.Image2.IsImageValid())
            {
                var save_image2_Result = fileService.SaveImageFile(product.Image2, 700, 700, saveImagePath, imageName2);
            }
            if (product.Image3 != null && product.Image3.IsImageValid())
            {
                var save_image3_Result = fileService.SaveImageFile(product.Image3, 700, 700, saveImagePath, imageName3);
            }
            if (product.Image4 != null && product.Image4.IsImageValid())
            {
                var save_image4_Result = fileService.SaveImageFile(product.Image4, 700, 700, saveImagePath, imageName4);
            }
            var addResult = PService.AddProduct(new Store.Models.DataBase.Entities.Product
            {
                BrandId          = product.BrandId,
                EName            = product.EName,
                ExistCount       = product.ExistCount,
                Features         = product.Features,
                Images_Json      = productImage.ToJsonImage(),
                InsertedDate     = DateTime.Now,
                IsActive         = product.IsActive,
                IsDeleted        = false,
                IsSpecial        = product.IsSpecial,
                MainDescription  = product.MainDescription,
                Name             = product.Name,
                SalesPrice       = product.SalesPrice,
                ShortDescription = product.ShortDescription,
                Tags             = product.Tags,
                SubCategoryId    = product.SubCategoryId,
                UnitType         = product.UnitType,
                WrittenPrice     = product.WrittenPrice.HasValue ? product.WrittenPrice.Value : 0
            });

            if (addResult)
            {
                return(RedirectToAction("index", "ProductManager"));
            }
            return(View(product));
        }