public IActionResult ChangeRecipeBanner([Bind("ExistingImageUrl")] BannerImageViewModel model) { if (!string.IsNullOrEmpty(model.ExistingImageUrl)) { BannerImages bannerSelected = _context.BannerImages.Where(bi => bi.BannerUrl == model.ExistingImageUrl).FirstOrDefault(); if (bannerSelected != null) { List <BannerImages> existingBanners = _context.BannerImages.Where(b => b.BannerType == BannerType.Recipe).ToList(); if (existingBanners.Count > 0) { existingBanners.ForEach(b => b.BannerStatus = BannerStatus.Inactive); foreach (BannerImages images in existingBanners) { _context.Update(images); } } bannerSelected.BannerStatus = BannerStatus.Active; _context.Update(bannerSelected); _context.SaveChanges(); TempData["SuccessMessage"] = "Succesfully Editing Recipe Banner"; return(RedirectToAction("RecipeBanner")); } } TempData["ErrorMessage"] = "Failed to Edit Recipe Banner"; return(RedirectToAction("RecipeBanner")); }
/// <summary> /// Convert BannerImage Entity into BannerImage Object /// </summary> ///<param name="BannerImageEntity">DataAccess.BannerImage</param> ///<returns>BannerImageViewModel</returns> public static BannerImageViewModel ToViewModel( this DataAccess.BannerImage entity, BannerImageViewModel model) { model.SessionUserId = entity.CreatedUserId; model.Id = entity.Id; model.DocumentId = entity.DocumentId; model.DocumentNameGuId = entity.Document.DocumentNameGuid; model.DocumentName = entity.Document.Name; model.IsActive = entity.IsActive; model.Ordinal = entity.Ordinal; return(model); }
/// <summary> /// Convert BannerImage Object into BannerImage Entity /// </summary> ///<param name="model">BannerImage</param> ///<param name="BannerImageEntity">DataAccess.BannerImage</param> ///<returns>DataAccess.BannerImage</returns> public static DataAccess.BannerImage ToEntity( this BannerImageViewModel model, DataAccess.BannerImage entity ) { if (entity.Id == 0) { entity.CreatedUserId = model.SessionUserId; } else { entity.IsActive = model.IsActive; entity.UpdatedUserId = model.SessionUserId; entity.UpdatedTimestamp = DateTime.Now; } entity.DocumentId = model.DocumentId; entity.Ordinal = model.Ordinal; return(entity); }
public IActionResult RecipeBanner() { List <BannerImages> bannerRecipes = _context.BannerImages.Where(b => b.BannerType == BannerType.Recipe).ToList(); BannerImageViewModel model = new BannerImageViewModel(); model.BannerImages = bannerRecipes; model.SetAsMainBanner = true; BannerImages currentBanner = bannerRecipes.Where(bp => bp.BannerStatus == BannerStatus.Active).FirstOrDefault(); if (currentBanner != null) { model.ExistingImageUrl = currentBanner.BannerUrl; } ViewData["ExistingBanners"] = new SelectList(bannerRecipes, "BannerUrl", "BannerName"); return(View(model)); }
public async Task <IActionResult> Banner(BannerImageViewModel model) { var shop = this._context.Shops.FirstOrDefault(s => s.Id == model.ShopId); var fileSize = model.Banner.Length; if ((fileSize / 1048576.0) > 5) { ModelState.AddModelError("", "The file you uploaded is too large. Filesize limit is 5mb."); return(View(model)); } if (model.Banner.ContentType != "image/jpeg" && model.Banner.ContentType != "image/png") { ModelState.AddModelError("", "Please upload a jpeg or png file for the banner."); return(View(model)); } var dirPath = _env.WebRootPath + "/image/shops/banner/" + model.ShopId.ToString(); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var filePath = dirPath + "/banner.png"; if (model.Banner.Length > 0) { byte[] bytes = await FileBytes(model.Banner.OpenReadStream()); using (Image <Rgba32> image = Image.Load(bytes)) { image.Mutate(x => x.Resize(750, 300)); image.Save(filePath); } } shop.Banner = true; this._context.Shops.Update(shop); this._context.SaveChanges(); return(RedirectToAction("Banner", new { ShopId = model.ShopId })); }
public IActionResult AddRecipeBanner([Bind("BannerImage", "SetAsMainBanner")] BannerImageViewModel model) { BannerImages banner = new BannerImages(); if (ModelState.IsValid) { banner.BannerUrl = ProcessUploadedFile(model.BannerImage); banner.BannerType = BannerType.Recipe; banner.BannerStatus = model.SetAsMainBanner ? BannerStatus.Active : BannerStatus.Inactive; banner.BannerName = model.BannerImage.FileName; if (banner.BannerStatus == BannerStatus.Active) { List <BannerImages> existingBanners = _context.BannerImages.Where(b => b.BannerType == BannerType.Recipe).ToList(); if (existingBanners.Count > 0) { existingBanners.ForEach(b => b.BannerStatus = BannerStatus.Inactive); foreach (BannerImages images in existingBanners) { _context.Update(images); } } } _context.Add(banner); _context.SaveChanges(); TempData["SuccessMessage"] = "Succesfully Added Recipe Banner"; } else { TempData["ErrorMessage"] = "Failed to Add Recipe Banner"; } return(RedirectToAction("RecipeBanner")); }