// GET: Admin/Pictures/Create public async Task <IActionResult> Create(int screenId, bool isBackgroundImage) { var vm = new PictureCreateViewModel { IsBackgroundPicture = isBackgroundImage, ScreenId = screenId }; if (!isBackgroundImage) { vm.PromotionSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(false)); var screen = await _bll.Screens.FindAsync(screenId); await _bll.SaveChangesAsync(); if (screen.ShowScheduleSeconds != null) { vm.ScheduleSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(true), screen.ShowScheduleSeconds ); } else { vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true)); } } return(View(vm)); }
// GET: Admin/Screens/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var screen = await _bll.Screens.FindAsync(id); if (screen == null) { return(NotFound()); } var vm = new ScreenCreateEditViewModel { Screen = screen, ScheduleAlwaysShown = false, PictureInScreens = new List <PictureInScreen>(), PromotionSecondsSelectListDictionary = new Dictionary <int, SelectList>(), ScheduleSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(true), screen.ShowScheduleSeconds ), ShowScheduleSecondsString = screen.ShowScheduleSeconds, ScreenOldPrefix = screen.Prefix, ShowPromotionSecondsStringDictionary = new Dictionary <int, string>() }; var promotions = (await _bll.PictureInScreens.GetAllPromotionsForScreenAsync((int)id)).ToList(); await _bll.SaveChangesAsync(); foreach (var promotion in promotions) { vm.PictureInScreens.Add(promotion); vm.PromotionSecondsSelectListDictionary[promotion.Id] = new SelectList( SecondsValueManager.GetDictionaryKeysList(false), promotion.ShowAddSeconds); vm.ShowPromotionSecondsStringDictionary.Add(promotion.Id, promotion.ShowAddSeconds); } if (vm.Screen.ShowScheduleSeconds.Equals(SecondsValueManager.GetSelectedValue(null, true)) && vm.PictureInScreens.TrueForAll(p => p.ShowAddSeconds.Equals(SecondsValueManager.GetSelectedValue(null, false)))) { vm.ScheduleAlwaysShown = true; } return(View(vm)); }
public async Task <IActionResult> Edit(int id, ScreenCreateEditViewModel vm) { if (id != vm.Screen.Id) { return(NotFound()); } vm.Screen.ChangedAt = DateTime.Now; vm.Screen.ChangedBy = _userManager.GetUserId(User); // If there is no promotions - show schedule always vm.Screen.ShowScheduleSeconds = vm.ShowPromotionSecondsStringDictionary == null ? SecondsValueManager.GetSelectedValue(null, true) : vm.ShowScheduleSecondsString; if (vm.PictureInScreens == null) { vm.PictureInScreens = new List <PictureInScreen>(); } if (vm.PromotionSecondsSelectListDictionary == null) { vm.PromotionSecondsSelectListDictionary = new Dictionary <int, SelectList>(); } if (vm.ScheduleSecondsSelectList == null) { vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true)); } if (vm.ShowPromotionSecondsStringDictionary != null) { foreach (var promotionId in vm.ShowPromotionSecondsStringDictionary.Keys) { var promotion = await _bll.PictureInScreens.FindAsync(promotionId); promotion.ShowAddSeconds = vm.ShowPromotionSecondsStringDictionary[promotionId]; vm.PictureInScreens.Add(promotion); } foreach (var pictureInScreen in vm.PictureInScreens) { vm.PromotionSecondsSelectListDictionary[pictureInScreen.Id] = new SelectList( SecondsValueManager.GetDictionaryKeysList(false), vm.ShowPromotionSecondsStringDictionary[pictureInScreen.Id]); } } else { var promotions = (await _bll.PictureInScreens.GetAllPromotionsForScreenAsync(id)).ToList(); await _bll.SaveChangesAsync(); foreach (var promotion in promotions) { promotion.ShowAddSeconds = SecondsValueManager.GetSelectedValue(0, false); _bll.PictureInScreens.Update(promotion); } } // Before model validation set values to the following parameters to pass model validation. vm.ShowPromotionSecondsStringDictionary ??= new Dictionary <int, string>(); vm.ShowScheduleSecondsString ??= SecondsValueManager.GetSelectedValue(null, true); ModelState.Clear(); TryValidateModel(vm.Screen); if (ModelState.IsValid) { try { foreach (var pictureInScreen in vm.PictureInScreens) { _bll.PictureInScreens.Update(pictureInScreen); } _bll.Screens.Update(vm.Screen); await _bll.SaveChangesAsync(); if (vm.ScreenOldPrefix != vm.Screen.Prefix) { await ScheduleUpdateService.GetAndSaveScheduleForScreen(_bll, _userManager.GetUserId(User), vm.Screen); } return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException) { if (!ScreenExists(vm.Screen.Id)) { return(NotFound()); } else { throw; } } } return(RedirectToAction(nameof(Edit))); }
public async Task <IActionResult> Create(PictureCreateViewModel vm, IFormFile file) { if (!vm.PictureFromUrl || (vm.PictureFromUrl && string.IsNullOrEmpty(vm.Picture.Path) && file != null && file.Length > 0)) { vm.Picture.Path = ""; ModelState.Clear(); TryValidateModel(vm.Picture); if (file == null || file.Length < 0) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileMissing); } else { if (file.Length > 20971520) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileTooBigError); } var fileExtension = Path.GetExtension(file.FileName); if (!_imageExtensions.Contains(fileExtension.ToUpper())) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileFormatError + string.Join(',', _imageExtensions) + "."); } } if (ModelState.ErrorCount > 0) { if (!vm.IsBackgroundPicture) { vm.PromotionSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(false), vm.ShowPromotionSecondsString); var screen = await _bll.Screens.FindAsync(vm.ScreenId); await _bll.SaveChangesAsync(); if (screen.ShowScheduleSeconds != null) { vm.ScheduleSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(true), screen.ShowScheduleSeconds ); } else { vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true)); } } return(View(vm)); } var innerDirName = vm.IsBackgroundPicture ? BackgroundDirectory : PromotionsDirectory; var directoryPath = Path.Combine(_appEnvironment.ContentRootPath, "wwwroot", "Images", innerDirName); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var path = Path.Combine(directoryPath, file.FileName); vm.Picture.Path = path.Substring(path.IndexOf("Images", StringComparison.Ordinal) - 1).Replace("\\", "/"); try { await using var fileStream = new FileStream(path, FileMode.Create); await file.CopyToAsync(fileStream); } catch (Exception e) { throw new Exception(e.Message); } } var userId = _userManager.GetUserId(User); vm.Picture.CreatedAt = vm.Picture.ChangedAt = DateTime.Now; vm.Picture.CreatedBy = vm.Picture.ChangedBy = userId; var guid = await _bll.Pictures.AddAsync(vm.Picture); await _bll.SaveChangesAsync(); var picture = _bll.Pictures.GetUpdatesAfterUowSaveChanges(guid); string?showAddSeconds = SecondsValueManager.GetSelectedValue(null, false); // Promotion was added and ShowScheduleSeconds and ShowAddSeconds values were selected if (vm.IsBackgroundPicture == false && vm.ShowPromotionSecondsString != null && vm.ShowScheduleSecondsString != null) { showAddSeconds = vm.ShowPromotionSecondsString; var screen = await _bll.Screens.FindAsync(vm.ScreenId); screen.ShowScheduleSeconds = vm.ShowScheduleSecondsString; _bll.Screens.Update(screen); } await _bll.PictureInScreens.AddAsync(new PictureInScreen { CreatedAt = DateTime.Now, ChangedAt = DateTime.Now, ChangedBy = userId, CreatedBy = userId, IsBackgroundPicture = vm.IsBackgroundPicture, ScreenId = vm.ScreenId, PictureId = picture.Id, ShowAddSeconds = showAddSeconds }); await _bll.SaveChangesAsync(); return(RedirectToAction("Index", "ScreenSettings")); }