Exemple #1
0
        public async Task <ActionResult> Insert(ProductInsertViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model.Id != 0)
            {
                return(BadRequest());
            }

            try
            {
                return(Created("", await _productsRepository.Insert(model.Front2Entity())));
            }
            catch (CustomErrorException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        public async Task <IActionResult> Add(ProductInsertViewModel product)
        {
            _logger.LogDebug("INSERT product");
            var result = await _service.Add(product);

            return(Ok(result));
        }
Exemple #3
0
        public IActionResult Insert()
        {
            ProductInsertViewModel model = new ProductInsertViewModel()
            {
                Categories = _categoryService.GetAll()
            };

            return(View(model));
        }
 public static ProductModel Front2Entity(this ProductInsertViewModel model)
 {
     return(new ProductModel()
     {
         Id = model.Id,
         Name = model.Name,
         Price = model.Price,
         Image = model.Image != null?Util.ByteEncode(model.Image) : null
     });
 }
Exemple #5
0
        public ActionResult Insert_ForModel()
        {
            using (var context = new NorthwindContext())
            {
                var model = new ProductInsertViewModel
                {
                    Product    = new Product(),
                    Categories = context.Categories.ToList()
                };

                return(View(model));
            }
        }
Exemple #6
0
        public ActionResult Insert(Product product, HttpPostedFileBase file)
        {
            //if (string.IsNullOrEmpty(product.ProductName))
            //{
            //    ModelState.AddModelError("", "ProductName is required");
            //}

            //if (product.CategoryId == 0)
            //{
            //    ModelState.AddModelError("", "Category is required");
            //}

            //var categoryName = product.Category.CategoryName;
            using (var context = new NorthwindContext())
            {
                if (!ModelState.IsValid)
                {
                    //ViewBag.Categories = context.Categories.ToList();

                    //return View("Insert_HtmlHelpers");

                    var model = new ProductInsertViewModel
                    {
                        Product    = product,
                        Categories = context.Categories.ToList()
                    };

                    return(View("Insert_ForModel", model));
                }

                #region File Upload

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/Content/files"), fileName);

                    file.SaveAs(path);
                }

                #endregion

                context.Products.Add(product);
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        public async Task <IActionResult> Update(string id, ProductInsertViewModel product)
        {
            _logger.LogDebug("UPDATE product");
            try
            {
                var model = await _service.Update(new ProductUpdateViewModel()
                {
                    Id          = id,
                    Name        = product.Name,
                    Description = product.Description,
                    Price       = product.Price
                });

                return(Ok(model));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Exemple #8
0
        public async Task <IActionResult> Insert(ProductInsertViewModel viewmodel)
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <ProductInsertViewModel, ProductDTO>();
            });
            IMapper    mapper = configuration.CreateMapper();
            ProductDTO dto    = mapper.Map <ProductDTO>(viewmodel);

            try
            {
                await _productService.Insert(dto);

                return(RedirectToAction("Buscar", "Product"));
            }
            catch (Exception ex)
            {
                ViewBag.Erros = ex.Message;
            }
            return(View());
        }
        public async Task <IActionResult> Insert(ProductInsertViewModel viewmodel)
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <ProductInsertViewModel, ProductDTO>();
            });
            IMapper mapper = configuration.CreateMapper();
            // new SERService().GetSERByID(4);
            //Transforma o ClienteInsertViewModel em um ClienteDTO
            ProductDTO dto = mapper.Map <ProductDTO>(viewmodel);

            try
            {
                await new ProductService().Insert(dto);
                return(RedirectToAction("Index", "Category"));
            }
            catch (Exception ex)
            {
                ViewBag.Erros = ex.Message;
            }
            return(View());
        }
        public async Task <IActionResult> Create(ProductInsertViewModel product
                                                 , ProductFeatureInsertViewModel vm
                                                 , ProductGalleryViewModel Pics)
        {
            // ثبت محصول
            var productId = await _productRepostitory.SubmitProduct(product, Pics.file);

            vm.ProductId = productId;

            if (Pics.galleryImage != null)
            {
                // آپلود گالری
                await _productGalleryRepository.UploadGalley(Pics.galleryImage, productId);
            }

            // ویژگی ها
            await _productFeatureRepository.AddFeatureRange(vm);

            // نمایش پیغام
            TempData.AddResult(SweetAlertExtenstion.Ok());

            // بازگشت به لیست محصولات
            return(Redirect(IndexUrlWithQueryString));
        }
        public async Task <ProductViewModel> Add(ProductInsertViewModel product)
        {
            var model = await _repository.Add(product.Name, product.Description, product.Price);

            return(ConverToView(model));
        }