public int AddInfographic(Infographic infographic, IEnumerable<HttpPostedFileBase> Images = null, IEnumerable<HttpPostedFileBase> Thumbs = null, IEnumerable<HttpPostedFileBase> ImagesAr = null, IEnumerable<HttpPostedFileBase> ThumbsAr = null) { DbContext.Infographics.Add(infographic); try { DbContext.SaveChanges(); if (infographic.ID > 0 && Images != null && Images.Count() > 0 && Images.Where(i => i != null).Count() > 0) { AddInfographicImages(infographic.ID, Images,1); } if (infographic.ID > 0 && Thumbs != null && Thumbs.Count() > 0 && Thumbs.Where(i => i != null).Count() > 0) { AddInfographicImages(infographic.ID, Thumbs,2); } if (infographic.ID > 0 && ImagesAr != null && ImagesAr.Count() > 0 && ImagesAr.Where(i => i != null).Count() > 0) { AddInfographicImages(infographic.ID, ImagesAr,3); } if (infographic.ID > 0 && ThumbsAr != null && ThumbsAr.Count() > 0 && ThumbsAr.Where(i => i != null).Count() > 0) { AddInfographicImages(infographic.ID, ThumbsAr,4); } return infographic.ID; } catch (Exception ex) { return -1; } }
public int UpdateInfographic(Infographic infographic, IEnumerable<HttpPostedFileBase> Images = null, IEnumerable<HttpPostedFileBase> Thumbs = null, IEnumerable<HttpPostedFileBase> ImagesAr = null, IEnumerable<HttpPostedFileBase> ThumbsAr = null) { Infographic A = DbContext.Infographics.Where(a => a.ID == infographic.ID).FirstOrDefault(); if (A == null) return -1; A.FriendlyURL = infographic.FriendlyURL; A.FriendlyURLAr = infographic.FriendlyURLAr; A.InfographicCategoryID = infographic.InfographicCategoryID; A.Name = infographic.Name; A.NameAr = infographic.NameAr; A.Order = infographic.Order; A.MetaDescriptionAr = infographic.MetaDescriptionAr; A.MetaDescription = infographic.MetaDescription; try { DbContext.SaveChanges(); if (A.ID > 0 && Images != null && Images.Where(i => i != null).Count() > 0) { if (A.ImageURL != null && A.ImageURL != "") { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + A.ImageURL)); } AddInfographicImages(A.ID, Images, 1); } if (A.ID > 0 && Thumbs != null && Thumbs.Where(i => i != null).Count() > 0) { if (A.ThumbURL != null && A.ThumbURL != "") { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + A.ThumbURL)); } AddInfographicImages(A.ID, Thumbs, 2); } if (A.ID > 0 && ImagesAr != null && ImagesAr.Where(i => i != null).Count() > 0) { if (A.ImageURLAr != null && A.ImageURLAr != "") { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + A.ImageURLAr)); } AddInfographicImages(A.ID, ImagesAr, 3); } if (A.ID > 0 && ThumbsAr != null && ThumbsAr.Where(i => i != null).Count() > 0) { if (A.ThumbURLAr != null && A.ThumbURLAr != "") { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + A.ThumbURLAr)); } AddInfographicImages(A.ID, ThumbsAr, 4); } return A.ID; } catch (Exception ex) { return -1; } }
public ActionResult EditInfographic(Infographic infographic, IEnumerable<HttpPostedFileBase> Images, IEnumerable<HttpPostedFileBase> Thumbs, IEnumerable<HttpPostedFileBase> ImagesAr, IEnumerable<HttpPostedFileBase> ThumbsAr) { ViewBag.MainNav = Navigator.Main.INPHOGRAPHIC; ViewBag.SubNav = Navigator.Sub.INFOGRAPHIC; FillCategories(infographic.InfographicCategoryID); if (ModelState.IsValid) { string cleaned_url = URLValidator.CleanURL(infographic.FriendlyURL); string cleaned_url_ar = URLValidator.CleanURL(infographic.FriendlyURLAr); if(infographicService.IsURLExist(cleaned_url, infographic.ID)) { ModelState.AddModelError("FriendlyURL", new Exception()); TempData["ErrorMessage"] = "Infographic Failed To Add, Friendly URL Already Exist"; } else if (infographicService.IsURLExist(cleaned_url_ar, infographic.ID)) { ModelState.AddModelError("FriendlyURL", new Exception()); TempData["ErrorMessage"] = "Infographic Failed To Add, Arabic Friendly URL Already Exist"; } else if (!URLValidator.IsValidURLPart(infographic.FriendlyURL)) { ModelState.AddModelError("FriendlyURL", new Exception()); TempData["ErrorMessage"] = "Infographic Failed To Add, Friendly URL Not Valid"; } else if (!URLValidator.IsValidURLPart(infographic.FriendlyURLAr)) { ModelState.AddModelError("FriendlyURL", new Exception()); TempData["ErrorMessage"] = "Infographic Failed To Add, Arabic Friendly URL Not Valid"; } else if (Images != null && Images.Count() > 0 && !ImageService.IsValid(Images)) { TempData["ErrorMessage"] = "Infographic Failed To Add, Invalid Image File"; } else if (Thumbs != null && Thumbs.Count() > 0 && !ImageService.IsValid(Thumbs)) { TempData["ErrorMessage"] = "Infographic Failed To Add, Invalid Thumb File"; } else if (ImagesAr != null && ImagesAr.Count() > 0 && !ImageService.IsValid(ImagesAr)) { TempData["ErrorMessage"] = "Infographic Failed To Add, Invalid Arabic Image File"; } else if (ThumbsAr != null && ThumbsAr.Count() > 0 && !ImageService.IsValid(ThumbsAr)) { TempData["ErrorMessage"] = "Infographic Failed To Add, Invalid Arabic Thumbs File"; } else { infographic.FriendlyURL = cleaned_url; int new_id = infographicService.UpdateInfographic(infographic, Images, Thumbs, ImagesAr, ThumbsAr); if (new_id > 0) { TempData["SuccessMessage"] = "Infographic Updated Successfully"; return RedirectToAction("EditInfographic", new { id = new_id }); } else TempData["ErrorMessage"] = "Infographic Failed To Update"; } } return View(infographic); }