public async Task AddBanner(BannerInformationDto banner) { if (banner.File != null) { BannerInformation bannerToAdd = null; string path = null; try { path = _img.CreateImage(banner.File); } catch (Exception ex) { _logger.LogInformation($"Banner image not created: {ex.Message}"); } if (!string.IsNullOrEmpty(path)) { bannerToAdd = new BannerInformation { BannerContent = banner.BannerContent, ImageUrl = path, CategoryForTabId = banner.CategoryForTabId, ExtraInformation = banner.ExtraInformation, ShowBannerOnHome = banner.ShowBannerOnHome }; } try { _ctx.BannersInformation.Add(bannerToAdd); await _ctx.SaveChangesAsync(); } catch (Exception ex) { _logger.LogInformation($"Banner record not created: {ex.Message}"); } } }
public async Task <IActionResult> Create(BannerInformationDto banner) { if (!ModelState.IsValid) { return(RedirectToAction(nameof(Index))); } try { await _ban.AddBanner(banner); return(RedirectToAction(nameof(Index))); } catch { return(RedirectToAction("Error", "Home")); } }
public async Task <IActionResult> Edit(int id, BannerInformationDto banner) { if (!ModelState.IsValid) { return(RedirectToAction("Edit", new { id })); } if (id != banner.BannerId) { return(RedirectToAction("Edit", new { id })); } try { await _ban.UpdateBannerWithId(banner); return(RedirectToAction(nameof(Index))); } catch { return(RedirectToAction("Error", "Home")); } }
public async Task UpdateBannerWithId(BannerInformationDto banner) { if (BannerExists(banner.BannerId)) { BannerInformation bannerToUpdate = null; string path = null; try { path = _img.EditImage(banner.File, banner.OldImage); } catch (Exception ex) { _logger.LogInformation($"Banner image not created: {ex.Message}"); } bannerToUpdate = await _ctx.BannersInformation.Where(b => b.BannerId == banner.BannerId).Select(b => new BannerInformation { BannerContent = !string.IsNullOrEmpty(banner.BannerContent) ? banner.BannerContent : b.BannerContent, ImageUrl = !string.IsNullOrEmpty(path) ? path : b.ImageUrl, CategoryForTabId = banner.CategoryForTabId, BannerId = b.BannerId, ExtraInformation = banner.ExtraInformation, ShowBannerOnHome = banner.ShowBannerOnHome }).SingleOrDefaultAsync(); try { _ctx.BannersInformation.Update(bannerToUpdate); await _ctx.SaveChangesAsync(); return; } catch (Exception ex) { _logger.LogInformation($"Banner record not created: {ex.Message}"); } } throw new Exception(); }