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 void DeleteBanner(int id) { if (BannerExists(id)) { BannerInformation bannerToRemove = _ctx.BannersInformation.Find(id); try { _ctx.BannersInformation.Remove(bannerToRemove); _ctx.SaveChanges(); } catch (Exception ex) { _logger.LogInformation($"Banner record not deleted: {ex.Message}"); } } }
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(); }