public bool SaveFile(HttpRequestBase Request, string server, ProdutCreationViewModel product) { bool successfull = false; try { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; var extention = Path.GetExtension(file.FileName); if (extention.ToLower() != ".jpg" && extention.ToLower() != ".png" && extention.ToLower() != ".gif") { throw new NotValidExtentionException("Not valid Extention"); } if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(server, product.ProducName + "-" + product.Gender.GenderName + extention); file.SaveAs(path); successfull = true; } else { successfull = false; } } } catch (System.Exception ex) { throw new FilesRequestFailedException(ex.Message); } return(successfull); }
// GET: Product/Create public ActionResult Create() { LoadGender(); ProdutCreationViewModel ProdutViewModel = LoadCategories(); return(View(ProdutViewModel)); }
public static Product MappFromProductViewToProductBO(ProdutCreationViewModel productviewModel) { var productBusiness = new Product { Description = productviewModel.Description, Gender = new Gender { GenderName = productviewModel.Gender.GenderName, IdGender = productviewModel.Gender.IdGender }, IdProduct = productviewModel.IdProduct, Image = productviewModel.Image, Price = productviewModel.Price, ProducName = productviewModel.ProducName, State = productviewModel.State, Title = productviewModel.Title, ListCategories = productviewModel.ListCategories.Select(e => new Category { CategoryName = e.CategoryName, CatergoryState = e.CatergoryState, IdCategory = e.IdCategory }).ToList() }; return(productBusiness); }
private ProdutCreationViewModel LoadCategories() { var ProdutViewModel = new ProdutCreationViewModel(); var listCategories = _getCategoriesListQuery.GetAll(); var listCategoriesViewModel = MappingUtility.MappFromCategoriesBOToCategoriesViewModel(listCategories); ProdutViewModel.ListCategories = listCategoriesViewModel; return(ProdutViewModel); }
public ActionResult Create(ProdutCreationViewModel product) { try { LoadGender(); ViewBag.ListCategories = LoadCategories(); product.Gender = GetGenderById(product.IdGender); string filePath = string.Empty; if (ModelState.IsValid) { var saveFile = _saleFilesFromRequest.SaveFile(Request, Server.MapPath("~/Images/").ToString(), product); filePath = ValidationPath(product, saveFile); var productBussines = MappingUtility.MappFromProductViewToProductBO(product); _commandProduct.AddProduct(productBussines); //hub ProductHub.ProductData(); ViewBag.Message = "Product created success"; } else { throw new NoValidaDataException("The form is not right please check it again"); } return(View(LoadCategories())); } catch (FilesRequestFailedException ex) { ViewBag.ErrorMessage = ex.Message; return(View(LoadCategories())); } catch (NotValidExtentionException ex) { ViewBag.ErrorMessage = ex.Message; return(View(LoadCategories())); } catch (NoValidaDataException ex) { ViewBag.ErrorMessage = ex.Message; return(View(LoadCategories())); } catch (Exception ex) { ViewBag.ErrorMessage = "Ups something it's wrong please try again later"; return(View(LoadCategories())); } }
public string GetUrlFileFromRequest(HttpRequestBase Request, string server, ProdutCreationViewModel product) { string ImageUrl = string.Empty; for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { string fileName = GetFileName(file); string extention = GetExtentionFile(file); string path = GetNewPathFile(server, product, extention); if (!string.IsNullOrEmpty(path)) { ImageUrl = path; } } } return(ImageUrl); }
private string ValidationPath(ProdutCreationViewModel product, bool saveFile) { string filePath; if (saveFile) { filePath = _getFilesFromRequest.GetUrlFileFromRequest(Request, Server.MapPath("~/Images/").ToString(), product); } else { throw new FilesRequestFailedException("The file is mandatory"); } if (!string.IsNullOrEmpty(filePath)) { product.Image = filePath; } else { throw new FilesRequestFailedException("The file is mandatory"); } return(filePath); }
public string GetNewPathFile(string server, ProdutCreationViewModel product, string extention) { return(string.Concat("/images/", product.ProducName + "-" + product.Gender.GenderName + extention)); }