protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string[] AdmitedImageType = { "image/jpeg", "image/png" }; const int MAX_SIZE = 2; ProductViewModelDTO product = (ProductViewModelDTO)validationContext.ObjectInstance; float bytes = 1024; float megabyte = 0; if (product.UploadedFile == null) { return(ValidationResult.Success); } megabyte = (product.UploadedFile.ContentLength / bytes) / bytes; if (!AdmitedImageType.Contains(product.UploadedFile.ContentType)) { return(new ValidationResult("Solo puede introducir imagenes con formato JPG y PNG")); } if (megabyte > MAX_SIZE || megabyte > Section.MaxRequestLength) { return(new ValidationResult("El archivo no puede exceder los 2mb")); } return(ValidationResult.Success); }
private Product GetEditedProduct(Product product, ProductViewModelDTO productViewModel) { product.Name = productViewModel.Product.Name; product.ShoppingPrice = productViewModel.Product.ShoppingPrice; product.SellingPrice = productViewModel.Product.SellingPrice; product.CategoryId = productViewModel.Product.CategoryId; product.StoreId = productViewModel.Product.StoreId; product.Stock = productViewModel.Product.Stock; product.MinimunStock = productViewModel.Product.MinimunStock; product.Description = productViewModel.Product.Description; product.ProviderId = productViewModel.Product.ProviderId; return(product); }
private ProductViewModelDTO CreateProductViewModel() { ProductViewModelDTO productViewModel = new ProductViewModelDTO() { Categories = dgrosStore.Categories.Where(p => p.State == STATE).ToList(), Stores = dgrosStore.Stores.ToList(), Product = new Product(), Providers = dgrosStore.Providers.Where(p => p.State == STATE).ToList(), Discounts = dgrosStore.Discounts.ToList() }; return(productViewModel); }
public ActionResult Edit(int id) { ProductViewModelDTO productViewModel = CreateProductViewModel(); Product productInDB = GetProduct(id); if (productInDB == null) { return(HttpNotFound()); } else { productViewModel.Product = productInDB; return(View("SaveProduct", productViewModel)); } }
public ActionResult Save(ProductViewModelDTO productView) { string url = "/Content/Images/Products/"; string defaultImageName = "prueba.jpg"; Image image; productView.Product.State = STATE; if (productView.Product.ProductId == 0) { if (!ModelState.IsValid) { return(View("SaveProduct", CreateProductViewModel())); } try { if (productView.UploadedFile != null) { image = CreateImageObject(productView.UploadedFile.FileName, url); productView.Product = SetImageToProduct(productView.Product, image); productView.UploadedFile.SaveAs(image.AbsolutePath); } else { productView.Product.Image = url + defaultImageName; } dgrosStore.Products.Add(productView.Product); } catch (Exception ex) { return(Content(ex.Message)); } } else { Product productInDb = GetProduct(productView.Product.ProductId); if (productInDb == null) { return(View("SaveProduct", CreateProductViewModel())); } if (!ModelState.IsValid) { ProductViewModelDTO editproduct = CreateProductViewModel(); editproduct.Product = productInDb; return(View("SaveProduct", editproduct)); } productInDb = GetEditedProduct(productInDb, productView); if (productView.UploadedFile != null) { image = CreateImageObject(productView.UploadedFile.FileName, url); try { productInDb = SetImageToProduct(productInDb, image); productView.UploadedFile.SaveAs(image.AbsolutePath); } catch (Exception ex) { return(Content(ex.Message)); } } } try { dgrosStore.SaveChanges(); return(RedirectToAction("Index", "Product")); }catch (Exception ex) { return(Content(ex.Message)); } }
public ActionResult Create() { ProductViewModelDTO productViewModel = CreateProductViewModel(); return(View("SaveProduct", productViewModel)); }